简体   繁体   中英

Null Pointer Exception in mocking Interface method

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM