Null @Autowired beans in unit test case

10,544

You are having code like this :

@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(locations = "classpath*:business-context-test.xml")
public class SMSGatewayTest {
   ..
   ..
}

Do you really wannna use MockitoJUnitRunner as I am not seeing any more mocks in the class.

Please try running the JUnit with like

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:business-context-test.xml")
public class SMSGatewayTest {
   ..
   ..
}

Edit -

@RunWith(SpringJUnit4ClassRunner.class) makes all those dependencies available which are declared using @ContextConfiguration(..) to the class on which it is used.

For example, in your case you have @RunWith(SpringJUnit4ClassRunner.class) on the class - SMSGateway. So it makes available all those dependencies to SMSGateway which are configured using @ContextConfiguration(locations = "classpath*:business-context-test.xml")

This helps to autowire 'smsGateway' inside your class SMSGateway. As you was using @RunWith(MockitoJUnitRunner.class), this dependency was not available for autowiring and hence spring was complaining. You will need MockitoJUnitRunner.class if you are using Mockito in your application. As you are not mocking any of your classes, you don't need MockitoJUnitRunner as of now.

Please take a look at - Mockito, JUnit and Spring for more clarifications.

Take a look at http://www.alexecollins.com/tutorial-junit-rule/ . This will help you to understand how '@Runwith' and '@ContextConfiguration' annotations work behind the scenes.

Share:
10,544
ihaider
Author by

ihaider

Updated on June 17, 2022

Comments

  • ihaider
    ihaider almost 2 years

    I am trying to auto-wire a spring managed bean to use in my unit test case. But autowired bean is always null. Below are my setting.

    my unit test class

    @RunWith(MockitoJUnitRunner.class)
    @ContextConfiguration(locations = "classpath*:business-context-test.xml")
    public class SMSGatewayTest {
    
        @Autowired
        @Qualifier("mySMSImpl")
        private ISMSGateway smsGateway;
    
            @Test
            public void testSendTextMessage() throws Exception {
              smsGateway.sendText(new TextMessage("TEST")); 
              //  ^___________ this is null, even though I have set ContextConfiguration 
            }
    
    }
    

    spring managed bean

    package com.myproject.business;
    
    @Component("mySMSImpl")
    public class MySMSImpl implements ISMSGateway {
    
        @Override
        public Boolean sendText(TextMessage textMessage ) throws VtsException {
              //do something
        }
    
    }
    

    context for unit test case

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    
    
           <context:annotation-config/>
           <context:component-scan base-package="com.myproject.business"/>
    </beans>
    

    I have seen many questions and all are giving the answers that I've already incorporated in my code. Can some one tell me what I am missing. I am using intellij 14 to run my test case.

  • ihaider
    ihaider almost 8 years
    Yes there are multiple implementation. Already removed that space.
  • ihaider
    ihaider almost 8 years
    Yup, it was a typo. My bad.
  • Tomas Kralik
    Tomas Kralik almost 8 years
    @ManinGreen Try adding <bean class="path.to.your.package.SMSGatewayTest" /> into your context configuration. Not sure Spring is aware of that class
  • ihaider
    ihaider almost 8 years
    in my test specific context file ?
  • Tomas Kralik
    Tomas Kralik almost 8 years
    @ManinGreen Yes, the one you use when you run your tests
  • ihaider
    ihaider almost 8 years
    I would appreciate if you could add more details in your answer like what was the issue actually, why and how changing to SpringJUnit4ClassRunner solved the problem ? I am bit confused here. :)