I have a method that looks similar to the following:
public void myMethod(MyClass c)
{
if (c == null)
{
return;
}
try
{
c.someMethod();
}
catch (SomeException e)
{
// log the exception, possibly re-throw
}
}
I am trying to find a way to set up a mock instance of the MyClass parameter c such that it returns a value of null for itself, and that c.someMethod() is never called. My unit test looks like this:
@Test
public void Test_myMethod_With_Null_MyClass_Does_Not_Call_someMethod()
{
Mockery mockery = new Mockery()
{{
setImposteriser(ClassImposteriser.INSTANCE);
}};
final MyClass mockMyClass = mockery.mock(MyClass.class);
try
{
mockery.checking(new Expectations()
{{
oneOf(mockMyClass).equals(null);
will(returnValue(true));
never(mockMyClass).someMethod();
}});
}
catch (Exception e)
{
logger.fatal(e);
fail("Exception thrown in test.");
}
Util.myMethod(mockMyClass);
}
Basically, i'm setting up a mock instance of MyClass, and setting the expectations on it that when its value is tested against the null value, it will return true, and that the method someMethod() is never called.
Right now, the test is failing, as jMock says that it's not possible to override methods provided by the Object class (the equals(null) part).
Is there a way to do this with jMock? Does this pattern make sense? Is this a valid test? If not, does anyone have any suggestions on how to test this?