(轉)@ContextConfiguration註解說明

場景:學習spring實戰中相關的單元測試spring

1 正常使用

@ContextConfiguration Spring整合JUnit4測試時,使用註解引入多個配置文件app

1.1 單個文件

@ContextConfiguration(locations="../applicationContext.xml")單元測試

@ContextConfiguration(classes = SimpleConfiguration.class)學習

1.2 多個文件

可用{}測試

@ContextConfiguration(locations = { "classpath*:/spring1.xml", "classpath*:/spring2.xml" })spa

1.3 默認不寫

能夠根據測試的類名,去找到與之對應的配置文件。code

 異常狀況:xml

異常信息blog

四月 12, 2017 9:38:10 上午 org.springframework.test.context.support.AbstractContextLoader generateDefaultLocations
信息: Could not detect default resource locations for test class [soundsystem.CNamespaceValueTest]: class path resource [soundsystem/CNamespaceValueTest-context.xml] does not exist
四月 12, 2017 9:38:10 上午 org.springframework.test.context.support.AnnotationConfigContextLoaderUtils detectDefaultConfigurationClasses
信息: Could not detect default configuration classes for test class [soundsystem.CNamespaceValueTest]: CNamespaceValueTest does not declare any static, non-private, non-final, inner classes annotated with @Configuration.

 

 

1it

相關文章
相關標籤/搜索