如何有效地许多春天背景

我已经创建了一个Java组件,它将被其他人使用。 我想进行单元测试来检查许多弹簧配置。

我今天所做的:按配置文件进行一个类测试

  • 配置01:Config01Test.java
  • 配置02:Config02Test.java
  • 配置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注释...就像@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
        }
    }
    

    谢谢,


    您可以删除Spring注释: @RunWith@DirtiesContext@LoadSpringContext并在每个测试用例中手动加载spring上下文。

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/application-context.xml");
    
    链接地址: http://www.djcxy.com/p/82049.html

    上一篇: How to valid many spring context

    下一篇: Configuration to use @Mock and @InjectMocks