Junit abstract

I want to run a GUI application 2 times from a Java test. How to 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 run it a second time, which annotation should I use? @afterclass ?

+7
junit4
source share
2 answers

A method annotated with @BeforeClass means that it runs once before any of the test methods runs in the test class. The method annotated with @Before is run once before each testing method in the class. For them are @AfterClass and @After .

Perhaps 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 do not need to explicitly call the @BeforeClass method in your testing methods, JUnit does this for you.

+18
source share

The @BeforeClass annotation is used to run something once before the test actually runs.

So, depending on what you want to get (and why), you can simply wrap the startup code in a loop, move the startup code in another method and call it from another place, or write a separate test case.

0
source share

All Articles