Reputation: 1351
I am beginning with JUnit and do not understand the annotations @Test
and @BeforeClass
.
I have the following code:
public class Toto {
@BeforeClass
public static void setupOnce() {
final Thread thread = new Thread() {
public void run() {
Main.main(new String[]{"-arg1", "arg2"});
}
};
try {
thread.start();
} catch (Exception ex) {
}
}
Why @BeforeClass
? And what's the setupOnce()
and threads in this case?
Should we add @Test
before each Java test?
So if I have 30 Java tests, should I have @Test public void test()
in each Java file?
Upvotes: 2
Views: 11534
Reputation: 1035
The @BeforeClass Annotation identifies a method, that should be executed prior any tests cases contained in this implementation unit. In this special case, this test class contains some initialization of a threaded resource that is required to be executed in the background during the tests.
JUnit defines four lifecycle events:
In my applications i normally execute expensive initializations using a @BeforeClass annotated method while the really expensive ones even are executed only once for the complete test suite at whole. But this "event" is based on some hacks which speed up my developments.
Upvotes: 16