6

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
  • 1,290
  • 4
  • 19
  • 38

2 Answers2

3

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.

Community
  • 1
  • 1
asg
  • 2,248
  • 3
  • 18
  • 26
  • 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. :) – ihaider May 11 '16 at 12:37
0

There is an extra whitespace in the Bean name

@Component("mySMSImpl ")
@Qualifier("mySMSImpl")

Why would you use the Qualifier annotation in this case? Are there multiple implementations of the ISMSGateway interface?

tkralik
  • 770
  • 1
  • 8
  • 18