junit annotation

junit4

Solution

Method annotated with `@BeforeClass` means that it is run once before any of the test methods are run in the test class. Method annotated with `@Before` is run once before every test method in the class. The counterparts for these are `@AfterClass` and `@After`.

Probably you are aiming for something like the following.

@BeforeClass
public static void setUpClass() {
  // Initialize stuff once for ALL tests (run once)
}

@Before
public void setUp() {
  // Initialize stuff before every test (this is run twice in this example)
}

@Test
public void test1() { /* Do assertions etc. */ }

@Test
public void test2() { /* Do assertions etc. */ }

@AfterClass
public static void tearDownClass() {
  // Do something after ALL tests have been run (run once)
}

@After
public void tearDown() {
  // Do something after each test (run twice in this example)
}

You don't need to explicitly call the `@BeforeClass` method in your test methods, JUnit does that for you.

Problem

I wish to launch the GUI application 2 times from Java test. How should we use `@annotation` in this case? ``` public class Toto { @BeforeClass public static void setupOnce() { final Thread thread = new Thread() { public void run() { //launch appli } }; try { thread.start(); } catch (Exception ex) { } } } ``` ``` public class Test extends toto { @Test public void test() { setuptonce(); closeAppli(); } @test public void test2() { setuptonce(); } } ``` To launch it a second time, which annotation should I use? `@afterclass`?

Original source