1

I have some own components, which I start before my Java application. It takes about 30 seconds to start this. 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

bilal
  • 103
  • 1
  • 1
  • 5

3 Answers3

3

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:

@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
     }
 }
dkatzel
  • 31,188
  • 3
  • 63
  • 67
2

Use the @BeforeClass annotation.

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.");
}
pablosaraiva
  • 2,343
  • 1
  • 27
  • 38
  • i mean whole integration tests, not a test class. i am using already before class, but i have several test classes, i want to run once for all test classes. – bilal Dec 04 '14 at 13:03
  • If all those classes need very the same preparation for testing, this is a sign of too much interdependency. Are you using mocks or stubs? – pablosaraiva Dec 04 '14 at 19:01
0

If you are using maven you could use the 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

Community
  • 1
  • 1
Drunix
  • 3,313
  • 8
  • 28
  • 50