@Autowired annotation not able to inject bean in JUnit class

11,015

You probably need to decorate your tests with these annotations:

@ContextConfiguration(locations = {/* your xml locations here */})
@RunWith(SpringJUnit4ClassRunner.class)

Or, if you use JUnit 3.x, you should extend from AbstractJUnit38SpringContextTests

Reference: TestContext support classes

Update: The problem seems to be that the context file can't be found (see discussion in comments).

in plain terms.. beanClass is an interface which has certain methods.. i have tagged that beanClass with @Service("beanObject") annotation..that banClass is implemented by beanClassImpl class which has the method implementations.. i need to use those implementations in my testClass to get the data to be compared.. for that i am doing @Autowired beanClass beanObject in my testClass.. m i going terribly wrong somewhere?

More Updates:

Don't annotate the interface, annotate the implementing class. Annotating the interface with @Service has no effect!

Share:
11,015
alpesh003
Author by

alpesh003

Updated on June 15, 2022

Comments

  • alpesh003
    alpesh003 almost 2 years

    my test class:

    public class myTest extends TestCase{
    @Autowired
    BeanClass beanObject
    public void beanTest()
    {
    Classdata data = beanObject.getMethod();
    }
    }
    

    I am getting a null pointer exception at line:

    Classdata data = beanObject.getMethod();
    

    the beanObject.getMethod(); precisely gives nullpointer exception

    How should i make possible the autowiring of the field beanObject in my Junit class so that i can use the methods from the "BeanClass" class?


    Copied from Comments:

    in plain terms.. beanClass is an interface which has certain methods.. i have tagged that beanClass with @Service("beanObject") annotation..that banClass is implemented by beanClassImpl class which has the method implementations.. i need to use those implementations in my testClass to get the data to be compared.. for that i am doing @Autowired beanClass beanObject in my testClass.. m i going terribly wrong somewhere?

  • alpesh003
    alpesh003 over 13 years
    @ContextConfiguration(locations = {spring-servlet.xml}) @RunWith(SpringJUnit4ClassRunner.class)
  • Sean Patrick Floyd
    Sean Patrick Floyd over 13 years
    @user582811 OK, then you should edit the question and show the relevant parts of spring-servlet.xml
  • Sean Patrick Floyd
    Sean Patrick Floyd over 13 years
    @user582811 under WEB-INF? And the test finds the context file?
  • alpesh003
    alpesh003 over 13 years
    the test is not able to find the context file
  • Sean Patrick Floyd
    Sean Patrick Floyd over 13 years
    @user582811: thought so. try this: locations = "classpath:WEB-INF/spring-servlet.xml"
  • alpesh003
    alpesh003 over 13 years
    since the test was not able to find the context file...i tried putting the spring-servlet.xml directly under root folder that is src.and then the two statements @ContextConfiguration(locations = {"spring-servlet.xml") @RunWith(SpringJUnit4ClassRunner.class) ..but in vain\
  • Sean Patrick Floyd
    Sean Patrick Floyd over 13 years
    but if WEB-INF is inside src/main/webapp that won't help either, then you'll have to use "file:src/main/webapp/WEB-INF/spring-servlet.xml"
  • Sean Patrick Floyd
    Sean Patrick Floyd over 13 years
    @user582811 use prefixes, either classpath: or file:. Reference: static.springsource.org/spring/docs/3.0.x/…
  • alpesh003
    alpesh003 over 13 years
    i tried this:@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:WEB-INF/liiraWeb‌​-servlet.xml"}) and public class myTest extends TestCase{ @Autowired BeanClass beanObject public void beanTest() { Classdata data = beanObject.getMethod(); } }..... still it gives nullpointer... m sorry m a newbie to springs... but m giving my efforts... and really appreciate ur help in it.. this autowiring thing does not seems to work in a junit class...
  • alpesh003
    alpesh003 over 13 years
    also tried to manually autowire the bean like--->ApplicationContext context = new ClassPathXmlApplicationContext("spring-servlet.xml"); context.getAutowireCapableBeanFactory().autowireBean(bean‌​Object);.. this attempt too it is not able to find the context file!!
  • Sean Patrick Floyd
    Sean Patrick Floyd over 13 years
    @user582811 autowiring is not the problem, but spring contexts usually don't reside under WEB-INF, and WEB-INF is usually not on the class path during development
  • alpesh003
    alpesh003 over 13 years
    thats what i did.. i relocated the spring-servlet.xml file under src folder outside of the web-inf folder and then tried autowiring.. still no progress!
  • alpesh003
    alpesh003 over 13 years
    in plain terms.. beanClass is an interface which has certain methods.. i have tagged that beanClass with @Service("beanObject") annotation..that banClass is implemented by beanClassImpl class which has the method implementations.. i need to use those implementations in my testClass to get the data to be compared.. for that i am doing @Autowired beanClass beanObject in my testClass.. m i going terribly wrong somewhere?
  • alpesh003
    alpesh003 over 13 years
    yes i did that.. annotated the implementing class with @Service("beanObject")... and tried using beanClassImpl autowiring... but still the same.. nullpointerexception
  • Sean Patrick Floyd
    Sean Patrick Floyd over 13 years
    @alpesh003 yes, obviously, if the context file isn't found then the bean isn't wired either. Sorry, I don't mean to be rude, but the info I get from you is too vague and I actually have work to do :-)
  • alpesh003
    alpesh003 over 13 years
    Thanx a lot... actually the run configurations were picking up JUnit 3 and also had to make some connection factory related settings in the spring-servlet.xml file... its solved... thanx for ur help and valuable time! :)
  • Salman S
    Salman S over 5 years
    @SeanPatrickFloyd how to solve this above issue if am not using servlet.xml in my project... Please Help