Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing different Java Versions in Junit

is there a way to specify the JVM version in a junit test, when multiple java types are available on an OS ?

I'd like to do something like this:

@JVM=1.5
public void test1(){
   run();
}

@JVM=1.7
public void test2(){
   run();
}

etc..

like image 656
jayunit100 Avatar asked Jan 28 '13 22:01

jayunit100


2 Answers

No, you'll have to run the JUnit tests multiple times with multiple VMs. You can then use Assume to only run the tests for the correct VM:

public void test1(){
   // not compiled or tested
   Assume.assumeThat(System.getProperty("java.version"), CoreMatchers.startsWith("1.5"));
   run();
}

public void test2(){
   Assume.assumeThat(System.getProperty("java.version"), CoreMatchers.startsWith("1.7"));
   run();
}

Please note that I haven't compiled this, so there may be errors. Then, in your build tool, run these tests twice in two different VMs.

like image 94
Matthew Farwell Avatar answered Oct 12 '22 21:10

Matthew Farwell


JUnit test cases are just a bunch of Java classes, not a standalone program. So they would still be confined within the same JVM. AFAIK there is no simple way to do what you intend to do. What you can do is separate the test cases into test suites for each java version you want to test for. Then change the java version in the project settings and run the appropriate test suites.

Also there is a junit.jvm property, but I am not sure if you can change it mid-execution. Have you taken a look at it?

like image 45
FirstName LastName Avatar answered Oct 12 '22 21:10

FirstName LastName