java - Parallel JUnit tests don't execute shutdown hook -
i running multiple junit tests in parallel classes using maven. have clean tasks need happen after junit tests run. handle clean up, adding shut down hook few tests. shut down hook correctly executes when not running parallel, don't see output (see example) shut down hook when run parallel. there doing wrong? jvm exit using system.exit
when executing in junit tests in parallel?
according surfire documentation documentation, parallel threads executed in same process, expect runtime.getruntime same process if called between different tests , threads. maven surfire plugin - fork options , parallel test execution
the important thing remember parallel option is: concurrency happens within same jvm process. efficient in terms of memory , execution time, may more vulnerable towards race conditions or other unexpected , hard reproduce behavior.
here example unit test:
@test public void test() { runtime.getruntime().addshutdownhook(new thread() { @override public void run() { system.out.println("test clean triggered"); } }); }
here relevant portion of pom:
<plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-surefire-plugin</artifactid> <version>2.18.1</version> <configuration> <parallel>classes</parallel> <threadcount>10</threadcount> </configuration> </plugin>
edited: problem fixed when add shut down hook in @beforeclass
method. when add in @test
method, have problem. able add hook @ time.
your shutdown hooks called, issue believe comes system.out.println
call associated shutdown hooks maven.
testing directly eclipse, have output:
test clean triggered
and trying different version of test:
@test public void test() { system.out.println("testing"); runtime.getruntime().addshutdownhook(new thread() { @override public void run() { try { files.createfile(paths.get("test1")); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } }); asserttrue(true); }
i can see @ root of director file "test1" created after calling mvn clean test
tests made with:
- openjdk version "1.8.0_121"
- junit 4.12
- maven 3.3.9
- maven surefire-plugin 2.17
Comments
Post a Comment