【问题标题】:Null @Autowired beans in unit test case单元测试用例中的空 @Autowired bean
【发布时间】:2016-09-06 19:00:30
【问题描述】:

我正在尝试自动连接一个 spring 管理的 bean 以在我的单元测试用例中使用。但自动装配的 bean 始终为空。以下是我的设置。

我的单元测试类

@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 托管 bean

package com.myproject.business;

@Component("mySMSImpl")
public class MySMSImpl implements ISMSGateway {

    @Override
    public Boolean sendText(TextMessage textMessage ) throws VtsException {
          //do something
    }

}

单元测试用例的上下文

<?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>

我看到了很多问题,并且都给出了我已经包含在我的代码中的答案。有人可以告诉我我错过了什么。我正在使用 intellij 14 运行我的测试用例。

【问题讨论】:

    标签: java spring unit-testing intellij-idea junit


    【解决方案1】:

    Bean 名称中有一个额外的空格

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

    在这种情况下,为什么要使用 Qualifier 注释? ISMSGateway 接口是否有多种实现方式?

    【讨论】:

    • 是的,有多种实现。已删除该空间。
    • 是的,这是一个错字。我的错。
    • @ManinGreen 尝试将&lt;bean class="path.to.your.package.SMSGatewayTest" /&gt; 添加到您的上下文配置中。不确定 Spring 是否知道该类
    • 在我的测试特定上下文文件中?
    • @ManinGreen 是的,你在运行测试时使用的那个
    【解决方案2】:

    你有这样的代码:

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

    你真的想使用 MockitoJUnitRunner,因为我在课堂上看不到任何模拟。

    请尝试使用 like 运行 JUnit

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

    编辑 -

    @RunWith(SpringJUnit4ClassRunner.class) 使使用 @ContextConfiguration(..) 声明的所有依赖项可用到使用它的类。

    例如,在您的情况下,您在类 - SMSGateway 上有 @RunWith(SpringJUnit4ClassRunner.class)。因此,它使使用 @ContextConfiguration(locations = "classpath*:business-context-test.xml") 配置的 SMSGateway 的所有依赖项都可用

    这有助于在您的类 SMSGateway 中自动连接“smsGateway”。当您使用 @RunWith(MockitoJUnitRunner.class) 时,此依赖项不可用于自动装配,因此 spring 抱怨。如果您在应用程序中使用 Mockito,您将需要 MockitoJUnitRunner.class。由于您没有模拟任何课程,因此您现在不需要 MockitoJUnitRunner。

    请看-Mockito, JUnit and Spring 以获得更多说明。

    看看http://www.alexecollins.com/tutorial-junit-rule/。这将帮助您了解“@Runwith”和“@ContextConfiguration”注解在幕后的工作原理。

    【讨论】:

    • 如果您能在答案中添加更多详细信息,例如实际问题是什么,为什么以及如何更改为 SpringJUnit4ClassRunner 解决问题,我将不胜感激?我在这里有点困惑。 :)
    猜你喜欢
    • 2020-11-04
    • 2018-06-15
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 2018-07-19
    • 2023-03-27
    相关资源
    最近更新 更多