Arquillian BeforeClass and AfterClass events fired for every test method in a class
paul.holding Oct 22, 2015 9:17 AMHi
I'm developing an Arquillian extension and need to observe the BeforeClass and AfterClass events and I have found that these events are fired for every method annotated with @Test.
The JavaDoc for these events states that the event is fired Before/After the Class execution, so my understanding is these events will only be fired once for the entire class, however this is not what I'm seeing.
To reproduce this issue I've created the following very simplified test class.
@RunWith(Arquillian.class)
public class BeanStalkIt {
@Deployment
public static WebArchive createDeployment() {
WebArchive war = ShrinkWrap.create(WebArchive.class);
return war;
}
@Test
public void testFee() {
assertTrue(true);
}
@Test
public void testFi() {
assertTrue(true);
}
@Test
public void testFo() {
assertTrue(true);
}
@Test
public void testFum() {
assertTrue(true);
}
The class for observing the events is as follows:
public class TestLifecycle {
public void beforeClass(@Observes BeforeClass beforeClassEvent) {
System.out.println("BeforeClass event fired! " + beforeClassEvent.getTestClass().getName());
}
public void beforeTest(@Observes Before beforeTestEvent) {
System.out.println("Before event fired! " + beforeTestEvent.getTestMethod().getName());
}
public void afterTest(@Observes After afterTestEvent) {
System.out.println("After event fired! " + afterTestEvent.getTestMethod().getName());
}
public void afterClass(@Observes AfterClass afterClassEvent) {
System.out.println("AfterClass event fired! " + afterClass.getTestClass().getName());
}
}
The output from when the events are fired is as follows:
BeforeClass event fired! integrationtest.BeanStalkIt
Before event fired! testFee
After event fired! testFee
AfterClass event fired! integrationtest.BeanStalkIt
BeforeClass event fired! integrationtest.BeanStalkIt
Before event fired! testFum
After event fired! testFum
AfterClass event fired! integrationtest.BeanStalkIt
BeforeClass event fired! integrationtest.BeanStalkIt
Before event fired! testFi
After event fired! testFi
AfterClass event fired! integrationtest.BeanStalkIt
BeforeClass event fired! integrationtest.BeanStalkIt
Before event fired! testFo
After event fired! testFo
AfterClass event fired! integrationtest.BeanStalkIt
Have I correctly understood that the BeforeClass and AfterClass events should only be fired once for the class? If so, what I am doing wrong that is causing the events to be fired for every test method? If not, does Arquillian provide any events which can be observed to achieve this purpose. I've tried looking at the BeforeSuite and AfterSuite events but they are also fired for every test method.
I'm using the Maven Arquillian BOM version 1.1.10.Final and the container is a remote instance of GlassFish 4.1 with Weld version 2.2.2.
Kind Regards
Paul