简体   繁体   English

Junit为整个集成测试设置了一次(所有测试类)

[英]Junit set up once for whole integration tests(all test classes)

I have some own components, which I start before my Java application. 我有一些自己的组件,这些组件是在Java应用程序之前启动的。 It takes about 30 seconds to start this. 开始大约需要30秒。 In my integration tests, I start my components before the class and all test cases run. 在集成测试中,我在类和所有测试用例运行之前启动组件。 My question is, is it possible to run my components not before the test class, but rather before the whole test? 我的问题是,是否可以不在测试类之前而是在整个测试之前运行组件?

kind regards, bilal 亲切的问候

If you use JUnit suites you can use a @BeforeClass to execute a method before the entire suite runs and an @AfterClass after the entire suite runs: 如果使用JUnit套件,则可以在整个套件运行之前使用@BeforeClass来执行方法,而在整个套件运行之后可以使用@AfterClass

@RunWith(Suite.class)
@SuiteClasses(
{
    //list your test classes here

}
)
 public class IntegrationSuite{

     @BeforeClass
     public static void setupSuite(){
        //do your initialization here
     }

     @AfterClass
     public static void tearDownSuite(){
       //...if needed
     }
 }

Use the @BeforeClass annotation. 使用@BeforeClass批注。

Please note that the annotated method has to be static. 请注意,带注释的方法必须是静态的。

@BeforeClass
public static void oneTimeInit() {
    System.out.println("It runs only once for all tests in this class.");
}

If you are using maven you could use the maven-failsafe-plugin. 如果使用的是maven,则可以使用maven-failsafe-plugin。 It has a pre-integration-test goal intended for test setup. 它具有用于测试设置的集成前测试目标。 For an example take a look at Maven Failsafe Plugin: how to use the pre- and post-integration-test phases 例如,请看一下Maven故障安全插件:如何使用集成前和集成后测试阶段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM