Issue
Let's say I wrote a pretty simple class called ValueFinder
that looks like this:
public class ValueFinder {
public String findValue(Object obj) {
String value = null;
value = findValueFirstWay(obj);
if (value == null) {
value = findValueSecondWay(obj);
}
return value;
}
protected String findValueFirstWay(Object obj) {
....
}
protected String findValueSecondWay(Object obj) {
....
}
}
Now I want to test that findValueFirstWay
is really the first method attempted when calling findValue
. This is a very simple class, the "algorithms" of the ways are very simple. Without doing a partial mock where findValueFirstWay
and findValueSecondWay
are mocked and findValue
is left alone, how do I test that?
I assume I'm supposed to refactor the class so that findValueFirstWay
and findValueSecondWay
are in a separate class or classes? It just seems like unnecessary work for something so simple.
Solution
You could avoid mocking entirely and test it by the visible effect with real objects.
Or you could mock the objects being passed in as values, and expect whatever calls that the findValueFirstWay(Object obj)
makes to the obj
that cause it to return a null, etc.
However, even though it seems simple, my first impulse would still be to extract the problem methods to a separate class or two, so that the interaction can be more naturally tested with mocks.
Answered By - Don Roby Answer Checked By - Robin (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.