我创建了一个java组件,供其他人使用。 我想进行单元测试以验证许多弹簧配置。
我今天所做的:按配置文件进行一次课堂测试
config 03:Config03Test.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring/config01.xml" })
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class Config01Test{
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring/config02.xml" })
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class Config02Test{
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring/config03.xml" })
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class Config03Test{}
但是我想知道我们是否可以在一个java类中完成它。 例如,为每个测试加载不同的弹簧上下文: (使用Junit Annotation ...就像@LoadSpringContext(blablabla)
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class ConfigTest{
@LoadSpringContext(locations = {"/spring/config01.xml")
public testConfig01(){
// implement test
}
@LoadSpringContext(locations = {"/spring/config02.xml")
public testConfig02(){
// implement test
}
@LoadSpringContext(locations = {"/spring/config03.xml")
public testConfig03(){
// implement test
}
}
谢谢,
答案 0 :(得分:0)
您可以删除Spring注释:@RunWith
,@DirtiesContext
,@LoadSpringContext
并在每个测试用例中手动加载弹簧上下文。
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/application-context.xml");