【问题标题】:JUnit, Spring (SpringJUnit4ClassRunner) and Spring wiring is not working properly (tried running from Eclipse and command line Maven)?JUnit、Spring (SpringJUnit4ClassRunner) 和 Spring 接线工作不正常(尝试从 Eclipse 和命令行 Maven 运行)?
【发布时间】:2012-12-31 07:51:04
【问题描述】:

我遇到了一个问题,即我试图通过 spring 配置连接的 bean 不工作?在我的测试用例中进行检查期间,bean 访问器显示为 null(但我确实在日志中观察到 setter 实际上将其设置为有效值。真的可以使用一些帮助......

代码如下:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="/test-context.xml")
public abstract class HistoryCompress extends TestBase {

    private CompressionArchive compressArchive;
    public void setCompressionArchive(CompressionArchive value)
    {
        this.logInfo("setting compression archive: %s, name: %s",value,value.getcompressionFormat());
        this.compressArchive=value;
    }

    protected CompressionArchive getCompressionArchive()
    {
        return this.compressArchive;
    }

    @Test
    public void getArchiveTypeName()
    {
        logInfo("test");
        this.assertNotNull(this.getCompressionArchive(),"compression archive is null");     
        this.assertNotNull(this.getCompressionArchive().getcompressionFormat(), "format name is null");
        logInfo("format: %s",this.getCompressionArchive().getcompressionFormat());
    }

}

public class HistoryZipCompress extends HistoryCompress 
{
    public HistoryZipCompress()
    {
        this.logInfo("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
        this.logInfo("-=-=-=--=- C'Tor HistoryZipCompress");
        this.logInfo("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
    }

}

public interface CompressionArchive {
    public String getcompressionFormat();
    public Map<String,String> getArchiveHashes(InputStream stream,String password) throws Exception;
}
public class CompressionArchiveZip extends IntegrationBase implements CompressionArchive
{
   /* implementation methods*/
}


然后我试图用下面的配置来连接它
<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"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:util="http://www.springframework.org/schema/util" 
    default-autowire="byName"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <context:annotation-config />       
    <bean class="gdg.scc.integration.testcases.file.history.HistoryZipCompress">
        <property name="compressionArchive"  ref="zipArchive" />
    </bean>     
    <bean id="zipArchive" class="gdg.scc.integration.global.helpers.compression.impl.CompressionArchiveZip" />
</beans>

当我运行它时,我观察到 HistoryZipCompress 被实例化了 3 次,这是在 HistoryZipCompress 上运行 getArchiveTypeName 的结果:

16-Jan 13:21:09 - INFO - main - org.springframework.context.support.GenericApplicationContext - 刷新 org.springframework.context.support.GenericApplicationContext@2bfe605c:启动日期 [2013 年 1 月 16 日星期三 13:21:09 MST] ;上下文层次的根 16-Jan 13:21:09 - INFO - main - org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' 发现并支持自动装配注释 16-Jan 13:21:09 - INFO - main - org.springframework.beans.factory.support.DefaultListableBeanFactory - 在 org.springframework.beans.factory.support.DefaultListableBeanFactory@578bb31a 中预实例化单例:定义 bean [fileSystemAPIUrl,defaultShopperAccount ,shopperAccounts,orionAccountCredential,SCC.FileTypeMappings.js,fileTypeGenerator,regexToExtensionMap,activeMQMessageProperties,automatorServiceInfo,activeMQAutomtorPublisher,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor ,org.springframework.context.annotation.internalCommonAnnotationProcessor,logResultsToAutomator,orionAccountHelper,gdg.scc.integration.testcases.file.history.HistoryZipCompress#0,zipArchive,extensionToMimeType];工厂层次结构的根 16-Jan 13:21:09 - INFO - main - integration.testcases.file.history.HistoryZipCompress - -=-=-=-=-=-=-=-=-=-=-=-=-=- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 16-Jan 13:21:09 - INFO - main - integration.testcases.file.history.HistoryZipCompress - -=-=-=--=- C'Tor HistoryZipCompress 16-Jan 13:21:09 - INFO - main - integration.testcases.file.history.HistoryZipCompress - -=-=-=-=-=-=-=-=-=-=-=-=-=- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 16-Jan 13:21:09 - INFO - main - integration.testcases.file.history.HistoryZipCompress - 设置压缩存档:integration.global.helpers.compression.impl.CompressionArchiveZip@6a367507,名称:zip 16-Jan 13:21:09 - 信息 - 主要 - integration.testcases.file.history.HistoryZipCompress - 测试 16-Jan 13:21:09 - INFO - Thread-0 - org.springframework.context.support.GenericApplicationContext - 关闭 org.springframework.context.support.GenericApplicationContext@2bfe605c:启动日期 [1 月 16 日星期三 13:21:09 MST 2013];上下文层次的根 16-Jan 13:21:09 - INFO - Thread-0 - org.springframework.beans.factory.support.DefaultListableBeanFactory - 破坏 org.springframework.beans.factory.support.DefaultListableBeanFactory@578bb31a 中的单例:定义 bean [fileSystemAPIUrl,fileTypeGenerator ,regexToExtensionMap,activeMQMessageProperties,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,integration.testcases.file .history.HistoryZipCompress#0,zipArchive,extensionToMimeType];工厂层次结构的根

【问题讨论】:

  • 我能够执行测试,它运行并加载测试用例,但是当测试用例到达“this.assertNotNull(this.getCompressionArchive()”行时,压缩存档为空” );"它失败了,因为 getCompressionArchive 返回 null
  • 您是否按照我的建议尝试过@Autowired?它应该真的有效:)

标签: java spring unit-testing junit spring-test


【解决方案1】:

使用@Autowired 注释字段:

@Autowired
private CompressionArchive compressArchive;

在 xml 配置中抛弃 HistoryZipCompress - 它在那里做什么?您不需要创建测试类的另一个实例,运行器会为您完成。

您需要将测试对象注入 CompressionArchive 实例。 Spring runner 管理您的测试对象(HistoryCompress),但不知道您要注入 CompressionArchive 实例。

【讨论】:

    猜你喜欢
    • 2015-03-22
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    相关资源
    最近更新 更多