Is it possible to define some test data, for a java program, in a way that enables it to be easily human readable and programmatically parsable into the relevant function calls and data elements.
If it is impossible do achieve in Java I’m open to using Scala for this. The code under test is java code and this will not be ported to Scala.
interface someInterface {
class X {
// the member variables will always either
// be enums or intrinsic types
private int a;
public int a() { return this.a; };
public void a(int a) {this.a = a; };
private double b;
public double b() { return this.b; };
public void b(double b) {this.b = b; };
private String c;
public String c() { return this.c; };
public void c(String c) {this.c = c; };
}
enum A {
A_1,
A_2
}
class Y {
// assume setters and getters as per X above
private A a;
private double b;
private String c;
private Z[] z;
}
class Z {
private int a;
private double b;
private String c;
}
Y function1(X x, String s);
}
public void boo() {
String[] testData = {
/* how can I specify this array so that
coo(...) can be called as below
would I be better off defining this
test code in Scala?
the classes and interfaces above:
someInterface, A, X, Y and Z are in Java
and will not be ported to Scala */
};
coo(testData);
}
public void coo(String[] testData) {
/* this function will know how to:
a) parse testData
b) use reflection to call
someInterface.functionXXX with parameters
as specified in testData
c) construct the return result as specified
in testData and compare against actual
return result */
}
}
You can use
JUnit 4for this and run with aParameterizedclass.Create a test class like this:
Explenation:
@RunWith(Parameterized.class)tells the JUnit framework to run ask the test class for parameters, and run all the test in the class with those parametrs.It expects a
CollectionofObject[]. EachObject[]is passed via reflection to the constructor of this class.prepareData– provides all the scenarios you want to test.MyTest(Object[] args)populates the members of this class before running all the tests on a specific parameter set. Important to note that the ‘someInterface` is initialized only once – for each parameter set. If you add more tests for the same parameter set, you might need to re-initialize it.testSomething()runs your test. It will run one time perObject[]thatprepareDataprovides. And it is guaranteed to run after theMyTest(Object[] args)has been executed (duh, like you have any alternative here)