Reputation: 1351
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
?
Upvotes: 9
Views: 21909
Reputation: 20934
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.
Upvotes: 22
Reputation: 459
The @BeforeClass annotation is used to run something once, before test actually runs.
So, depending on what do you want to get (and why), you can simply wrap launch code in a cycle, move launch code in other method and call it from somewhere else or write separate test case.
Upvotes: 0