mardi 27 janvier 2015

JUnit: testing if-else statements

I'm testing the following method:



private String getHomeStateCountry(Details aDetails, String aCountry) {
String homeState = Constants.BLANK_SPACE;
if (Constants.USA.equalsIgnoreCase((aCountry))) {
if (aDetails.getmStateCountry() != null) {
homeState = aDetails.getStateCountry().getStateName();
}
} else {
if (aDetails.getStateCountry() != null) {
homeState = aDetails.getStateCountry().getCountryName();
}
}
return homeState;
}


When writing JUnits for this method, Is it pointless to set the stateName and countryName for each test(so if something goes wrong, e.g. the state is returned instead of the country, then the fail message provides a more accurate explanation etc) or should each test only set the value under test, i.e. if I'm testing for other than USA, only set countryName and check the returned value?


Aucun commentaire:

Enregistrer un commentaire