I have a data transformation class which I want to test. The interface ITransformer is given. The class looks like this:
public class MyDataTransformer implements ITranformer {
public MyDataTransformer() {}
@Override
public void init(String[] args) throws Exception {
if (args.length == 0)
throw (new Exception("bad input"));
// do initialization
}
@Override
public byte[] transform(byte[] input) {
try {
return transform_internal(input);
} catch (Exception e) {
return null;
}
}
private static byte[] transform_internal(byte[] input) throws Exception {
// do something
}
}
And this is the test class I currently have:
public class TransformerTest {
private MyDataTransformer transformer;
@BeforeClass
public void setUp() {
String[] args = new String[4];
// set args
try {
transformer = new MyDataTransformer();
transformer.init(args);
} catch (Exception e) {
fail(e.getMessage());
}
}
@Test
public void testTransform() {
byte[] input = read_input(); // omitted here for brevity
byte[] output = transformer.transform(input);
// this only tests if "valid" data was returned, but does not look into it
assertNotNull("Transformation failed", output);
// these 2 test some properties of the result data and should be separate tests
assertTrue("transformation step 1 failed", test_transformation_1(output);
assertTrue("transformation step 2 failed", test_transformation_2(output);
}
}
There are two things I struggle with when it comes to testing.
First, how do I properly test the init method only once? Semantically, init belongs into the @BeforeClass block, but this block is not a proper test.
Second, how can I separate test_transformation_1 out into its own test, yet make sure that this is run only after the testTransform completed successfully (otherwise it is not sensible to run this test on invalid input).
To me, it feels like nesting tests would solve both problems, hence the title of the question. This example however may grow and I might add independent tests which do not need nesting and I'd like to keep it as simple as possible.
Aucun commentaire:
Enregistrer un commentaire