I'm using TestNG for testing and JMockit for mocking mockMethod(). Here's the test case:
@Test
public void testClass1() {
new MockUp<MyInterface>() {
@Mock
public int myMethod(final MyObject someObject ){
return 0;
}
};
MyObject obj = new MyObject();
Assert.assertEquals(obj.mockMethod(someObject),0);
}
}
The mockMethod() I'm calling for assertEquals() looks like this:
public class Class1 {
MyInterface my;
public int mockMethod(final MyObject someObject ){
...... //processing steps
return my.myMethod(someObject);
}
}
Test case throws a Null pointer exception. What am I doing wrong? Do I mock the implementation of mockMethod()? I tried that too, but it hasn't worked.
JMockit created a mock instance of MyInterface
, but your test never used it. It can be obtained through the MockUp#getInstance()
method. Then of course the test also needs to pass the instance to the class under test.