1

I have a Java program which I want to run multiple times in order to get some test results. This program has some global static fields that control certain parameters (e.g. number of neighbours). One of the answers here (How to run a Java program multiple times without JVM exiting?) suggests to call the main method of this program from a wrapper class, but a subsequent comment mentions that this approach wouldn't work with static initialisers. What's the best way to go about this?

Is it also possible to change the values of these static fields depending on which iteration is being performed? Example, for the first 10 iterations I'd like the number of neighbours to be 5 and for the next 10 I'd want to make them 20 and so on.

Community
  • 1
  • 1
  • 1
    You could use a separate classloader... for each iteration - but fundamentally this doesn't sound like a great idea. – Jon Skeet Feb 21 '15 at 23:11
  • If you have control over the source code, consider modifying it. Static variables are evil. http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil – dnault Feb 21 '15 at 23:30

1 Answers1

0

If you are making performance tests, I'd suggest you to try Japex, which it is a benchmark framework to configure and perform tests. Once you have learned how to work with it, you'll find it very easy and interesting.

If you are making functional tests, I'd suggest Junit, another benchmark for unitary tests, mainly used across the Java Community.

And another way is to perform the calls to the JVM from a system script (or an Ant script, if you like it), and pass the initial values through system properties:


    java -Dtest.myProperty=value MyClass


    public class MyClass
    {
    final static String MY_VAR=System.getProperty("test.myProperty");
    ...
    }

Either one way or the other, you'll have to change the way the program gets the value for the static variables.

Little Santi
  • 8,563
  • 2
  • 18
  • 46