I have written a test method in which there is a mocked object(say mockA). I am able to expect mockA's method calls for the actual program logic. But, part of my program also has logging which require objects' information in the form of string. While creating the string message, there are some unnecessary methods being called on the objects. So, while running the test, these method calls are causing the test to fail. Here's an example.
public class Example {
public int method(Foo foo) {
int a = foo.doSomething(); //required for program.
String logMessage = "foo did something." + foo.getA() + foo.getB().getC();
logger.log(logFile, logMessage);
return a;
}
}
Here's the example test method.
@Test
public void testMethod() {
int something = 0;
Foo mockFoo = EasyMock.createMock(Foo.class);
expect(mockFoo.doSomething()).andReturn(something);
EasyMock.replay(mockFoo);
assertEquals(new Example().method(mockFoo), something);
EasyMock.verify(mockFoo);
}
This is giving unexpected method calls for foo.getA(). If I create a nice mock instead for Foo.class, it gives me null pointer exception for foo.getB().getC() since B is an object inside foo. It's not possible for me to create nice mocks of all objects inside foo.
Is there anyway to prevent such string operations which are used for logging? Or alternatively, what could be done?
Thanks