test suite inside spring context

淺唱寂寞╮ 提交于 2019-12-05 00:17:48

问题


Is it possible to run test suite with loaded spring context, something like this

@RunWith(Suite.class)
@SuiteClasses({ Test1.class, Test2.class })
@ContextConfiguration(locations = { "classpath:context.xml" }) <------
public class SuiteTest {
}

The code above obviously wont work, but is there any way to accomplish such behavior?

This is currently how spring context is used in my test suite:

@BeforeClass
public static void setUp() {
    final ConfigurableApplicationContext context =
            loadContext(new String[] { "context.xml" });
    jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");
    // initialization of other beans...
}

回答1:


I have tried you code, the test suite are running with spring context loaded. Can you explain in more detail what the problem is?

here is the code:

@RunWith(Suite.class)
@SuiteClasses({ Test1.class, Test2.class })
public class SuiteTest {
}

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:context.xml" })
@Transactional
public class Test1 {}

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:context.xml" })
@Transactional
public class Test2 {}

If you want Suite class to have its own application context, try this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:context.xml" })
@Transactional
public class SuiteTest {

    @Test public void run() {
        JUnitCore.runClasses(Test1.class, Test2.class);
    }

}


来源:https://stackoverflow.com/questions/17484191/test-suite-inside-spring-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!