Reputation: 13395
I want to test this method and its conditions.
public String getType(String body) {
String type = getTypeCode(body, 1); //this
if (null == type) {
type = getTypeCode(body, 2); //this
}
return type;
}
So I've written a test
@Test
public void testTypeOldToNew() {
Parser parser = createMock(Parser.class);
expect(parser.getTypeCode(BODY_2, 1)).andReturn(null);
expect(parser.getTypeCode(BODY_2, 2)).andReturn(CODE_TWO_MESSAGE);
replay(parser);
parser.getType(BODY_2);
verify(parser);
}
But when I run it I've got an error
java.lang.AssertionError:
Unexpected method call Parser.getype("value"):
Parser.getTypeCode("value", 1): expected: 1, actual: 0
Parser.getTypeCode("value", 2): expected: 1, actual: 0
Why? What's the problem?
Upvotes: 0
Views: 6250
Reputation: 108970
You can't mock the class you are testing. You are calling parser.getType(BODY_2);
on the mock. And as you haven't defined anything for that method, nothing happens (and defining what should happen doesn't make sense, because then you would be testing the mocking framework).
You need to mock interactions of this class with other classes (or better: interfaces).
Upvotes: 3