As a note beforehand: I think about this as opportunity to simplify tests, so it doesn't have to be cutting-edge efficient.
Lets say I have this unit test:
@Test
public void testVoiceOutput() {
Stone testStone = new Stone();
testStone.talkTo("Hey, how are you doing?");
assertNull("Stone's answer", testStone.listenForReply());
}
When I now add possibilities of conditions, the stone can be in, this gets more and more unreadable:
@Test
public void testVoiceOutput() {
Stone testStone = new Stone();
for (String currentEnvironment : new String[]{"FLOOR", "MEADOW", "STREET"}) {
testStone.setEnvironment(currentEnvironment);
testStone.talkTo("Hey, how are you doing?");
assertNull("Stone's answer, environment=" + currentEnvironment, testStone.listenForReply());
}
}
and
@Test
public void testVoiceOutput() {
Stone testStone = new Stone();
for (String timeOfDay : new String[]{"NIGHT", "NOON", "TEATIME", "DINNERTIME"}) {
for (String currentWeather : new String[]{"RAINY", "FOGGY", "SUNNY", "SNOWY"}) {
for (String currentEnvironment : new String[]{"FLOOR", "MEADOW", "STREET"}) {
testStone.setTimeOfDay(timeOfDay);
testStone.setWeather(currentWeather);
testStone.setEnvironment(currentEnvironment);
testStone.talkTo("Hey, how are you doing?");
assertNull("Stone's answer, environment=" + currentEnvironment + ", weather=" + currentWeather + ", time=" + timeOfDay, testStone.listenForReply());
}
}
}
}
Im am currently collecting ideas on how to simplify that, maybe with a functional interface that creates a Runnable? I am fairly new to the lambda functionalities of newer Java, so maybe you could give me a hint into a promising direction. Of course, in reality it wouldn't all be Strings as arguments.
I already know the @Parameterized functionalities of JUnit, but that's very unhandy if I only have one test that needs all possibilities or if I want to reuse the same Stone object over and over.