java - Running code before and after all tests in a surefire execution -



java - Running code before and after all tests in a surefire execution -

i have grizzly httpserver want run entire duration of test grouping execution. additionally, want interact global httpserver instance @rule within tests themselves.

since i'm using maven surefire rather using junit test suites, can't utilize @beforeclass/@afterclass on test suite itself.

right now, can think of lazily initialising static field , stopping server runtime.addshutdownhook() -- not nice!

there 2 options, maven solution , surefire solution. to the lowest degree coupled solution execute plugin in pre-integration-test , post-integration-test phase. see introduction build lifecycle - lifecycle reference. i'm not familiar grizzly, here illustration using jetty:

<build> <plugins> <plugin> <groupid>org.mortbay.jetty</groupid> <artifactid>maven-jetty-plugin</artifactid> <configuration> <contextpath>/xxx</contextpath> </configuration> <executions> <execution> <id>start-jetty</id> <phase>pre-integration-test</phase> <goals> <goal>run</goal> </goals> <configuration> </configuration> </execution> <execution> <id>stop-jetty</id> <phase>post-integration-test</phase> <goals> <goal>stop</goal> </goals> </execution> </executions> </plugin>

note phase start pre-integration-test , stop post-integration-test. i'm not sure if there grizzly maven plugin, utilize maven-antrun-plugin instead.

the sec alternative utilize junit runlistener. runlistener listens test events, such test start, test end, test failure, test success etc.

public class runlistener { public void testrunstarted(description description) throws exception {} public void testrunfinished(result result) throws exception {} public void teststarted(description description) throws exception {} public void testfinished(description description) throws exception {} public void testfailure(failure failure) throws exception {} public void testassumptionfailure(failure failure) {} public void testignored(description description) throws exception {} }

so hear runstarted , runfinished. these start/stop services want. then, in surefire, can specify custom listener, using:

<plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-surefire-plugin</artifactid> <version>2.10</version> <configuration> <properties> <property> <name>listener</name> <value>com.mycompany.myresultlistener,com.mycompany.myresultlistener2</value> </property> </properties> </configuration> </plugin>

this maven surefire plugin, using junit, using custom listeners , reporters

java junit maven-surefire-plugin grizzly

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -