Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null @Autowired beans in unit test case

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.

like image 705
ihaider Avatar asked May 11 '16 11:05

ihaider


People also ask

Can Autowired beans be null?

Also note that if, for whatever reason, you make a method in a @Service as final , the autowired beans you will access from it will always be null .

Why is Autowired null?

If the application starts and your field appears to be null it is generally due to one of the following issues: Using @Autowired on a static field. Omitted @Autowired on a field. Instance of bean not visible to Spring.

What will happen if I make @autowired as false?

By default, the @Autowired annotation implies that the dependency is required. This means an exception will be thrown when a dependency is not resolved. You can override that default behavior using the (required=false) option with @Autowired .

Can we use Autowired in test class?

To check the Service class, we need to have an instance of the Service class created and available as a @Bean so that we can @Autowire it in our test class. We can achieve this configuration using the @TestConfiguration annotation.


1 Answers

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.

like image 54
asg Avatar answered Oct 08 '22 06:10

asg