[英]JUnit, Spring (SpringJUnit4ClassRunner) and Spring wiring is not working properly (tried running from Eclipse and command line Maven)?
我遇到一个问题,我尝试通过弹簧配置接线的bean无法正常工作? 在我的测试用例中,bean访问器在检查期间显示为空(但是我确实在日志中观察到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*/
}
16-Jan 13:21:09 - INFO - main - org.springframework.context.support.GenericApplicationContext - Refreshing org.springframework.context.support.GenericApplicationContext@2bfe605c: startup date [Wed Jan 16 13:21:09 MST 2013]; root of context hierarchy 16-Jan 13:21:09 - INFO - main - org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring 16-Jan 13:21:09 - INFO - main - org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@578bb31a: defining beans [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]; root of factory hierarchy 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 - setting compression archive: integration.global.helpers.compression.impl.CompressionArchiveZip@6a367507, name: zip 16-Jan 13:21:09 - INFO - main - integration.testcases.file.history.HistoryZipCompress - test 16-Jan 13:21:09 - INFO - Thread-0 - org.springframework.context.support.GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@2bfe605c: startup date [Wed Jan 16 13:21:09 MST 2013]; root of context hierarchy 16-Jan 13:21:09 - INFO - Thread-0 - org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@578bb31a: defining beans [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]; root of factory hierarchy
运行此命令时,我发现HistoryZipCompress实例化了3次,这是在HistoryZipCompress上运行getArchiveTypeName的尾巴:
\n 1月16日13:21:09-信息-主-org.springframework.context.support.GenericApplicationContext-刷新org.springframework.context.support.GenericApplicationContext@2bfe605c:启动日期[2013年1月16日星期三13:21:09] ; 上下文层次结构的根\n 1月16日13:21:09-信息-主-org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor-JSR-330'javax.inject.Inject'注释已找到并支持自动装配\n 1月16日13:21:09-信息-主-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]; 工厂层次结构的根\n 1月16日13:21:09-信息-主-Integration.testcases.file.history.HistoryZipCompress--=-=-=-=-=-=-=-=-=-=-=-=-=-=- = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - \n 1月16日13:21:09-信息-主-Integration.testcases.file.history.HistoryZipCompress--=-=-=-=-C'Tor HistoryZipCompress\n 1月16日13:21:09-信息-主-Integration.testcases.file.history.HistoryZipCompress--=-=-=-=-=-=-=-=-=-=-=-=-=-=- = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - \n 1月16日13:21:09-信息-主-Integration.testcases.file.history.HistoryZipCompress-设置压缩存档:integration.global.helpers.compression.impl.CompressionArchiveZip@6a367507,名称:zip\n 1月16日13:21:09-信息-主-Integration.testcases.file.history.HistoryZipCompress-测试\n 1月16日13:21:09-信息-线程-0-org.springframework.context.support.GenericApplicationContext-关闭org.springframework.context.support.GenericApplicationContext@2bfe605c:启动日期[MST Jan 16 13:21:09 MST 2013]; 上下文层次结构的根\n 1月16日13:21:09-信息-线程-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]; 工厂层次结构的根\n
用@Autowired注释字段:
@Autowired
private CompressionArchive compressArchive;
将historyZipCompress放到xml配置中-在那里做什么? 您不需要创建测试类的另一个实例,运行器将为您完成此操作。
您需要测试对象来注入CompressionArchive实例。 SpringRunner管理您的测试对象(HistoryCompress),但不知道您要注入CompressionArchive实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.