Null Pointer Exception in mocking Interface method

匿名 (未验证) 提交于 2019-12-03 01:44:01

问题:

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.

回答1:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!