To unit test the method doSomething(String name) in ClassA below, what things should I test on the return value?
My first thought is to test
- that the name attribute is set properly on Wrapper
- that the formattedName attribute on Wrapper is properly formatted
But on second thought, should I instead test the value of formattedName in the unit test for UtilClass.format(String name)? Or should I do it in both places?
public class ClassA {
public Wrapper doSomething(String name) {
Wrapper wrapper = new Wrapper();
wrapper.setName(name);
wrapper.setFormattedName(UtilClass.format(name));
return wrapper;
}
}
I would have tests that validate that name and formatted name have the appropriate values. To decouple the method from
UtilClassI’d use injection and also validate, using mocks, that theUtilClass.formatmethod is called. Your test for the formatted name, then would check that the value is equal to the result returned from your mock. I’d also have (probably the first test to write) a test that verifies that you get a non-null result when you call the method. TestUtilClassmethods separately.EDIT: I’d agree with @Bill K that you should definitely write the tests first.