简体   繁体   English

Java 单元测试与 Mockito NullPointer 异常错误

[英]Java Unittest with Mockito NullPointer Exception Error

I'm trying to write a unittest ( test_myMethod ) for one of my methods ( myMethod ) but I can't seem to get it to work.我正在尝试为我的一种方法( myMethod )编写一个单元测试( test_myMethod ),但我似乎无法让它工作。 I get a NullPointer exception error with the code below.下面的代码出现NullPointer 异常错误 In my test it seems that the line myClass.otherMethod("hostName") in the unittest evaluates to Null so it can't do .getOSRevision() .在我的测试中,单元测试中的myClass.otherMethod("hostName")行似乎评估为 Null 所以它不能做.getOSRevision() Anyone know how I can get my unittest to pass?任何人都知道我怎样才能让我的单元测试通过?

MyClass.java MyClass.java

public class MyClass {
    public String myMethod(final String hostname) {
        return otherMethod(hostname).getOSRevision();
    }

    public OtherMethod otherMethod(final String hostname) {
        OtherMethod response = myClient.newMyMethodRevisionCall().call(
            someObject.builder()
                .withHostName(hostname)
                .build()
        );
        return response; 
    }
   
}

MyClassTest.java MyClassTest.java

public class MyClassTest {
    @Mock
    private MyClient myClient;

    @Mock
    private MyMethodRevisionCall myMethodRevisionCall;

    @Mock
    private OtherMethod otherMethod;
    
    private MyClass myClass;

    @BeforeEach
        void setUp() {
            MockitoAnnotations.initMocks(this);
            when(myClient.newMyMethodRevisionCall()).thenReturn(myMethodRevisionCall);
            myClass = new MyClass(myClient);
    }

    // My failing unittest attempt
    @Test
    public void test_myMethod() {
        when(myClass.otherMethod("hostName")
        .getOSRevision())    //java.lang.NullPointerException happens on this line.
        .thenReturn("TestString");

        final String result = myClass.myMethod("TestString");
        verify(myMethodRevisionCall, times(1)
    }
}

You cannot use Mockito.when() on classes that are not mocked by Mockito.您不能在未被 Mockito 模拟的类上使用 Mockito.when()。 MyClass is not a mock. MyClass 不是模拟。 Instead you create an actual instance of it.相反,您创建它的实际实例。 Therefore, you need to actually run the code and not mock it.因此,您需要实际运行代码而不是模拟它。 However, all dependencies of your class under test (MyClient in your case) you can mock.但是,您可以模拟您正在测试的 class 的所有依赖项(在您的情况下为 MyClient)。

This is a working example:这是一个工作示例:

public class MyClass {
    private MyClient myClient;

    public MyClass(MyClient myClient) {
        this.myClient = myClient;
    }

    public String myMethod(final String hostname) {
        return otherMethod(hostname).getOSRevision();
    }

    public OtherMethod otherMethod(final String hostname) {
        return myClient.newMyMethodRevisionCall().call(new SomeObject(hostname));
    }
}
class MyClassTest {
    private MyClient myClient;
    private MyClass myClass;

    @BeforeEach
    void setUp() {
        myClient = mock(MyClient.class);
        myClass = new MyClass(myClient);
    }

    @Test
    public void test_myMethod() {
        MyMethodRevisionCall myMethodRevisionCall = mock(MyMethodRevisionCall.class);
        OtherMethod otherMethod = mock(OtherMethod.class);
        when(myClient.newMyMethodRevisionCall()).thenReturn(myMethodRevisionCall);
        when(myMethodRevisionCall.call(any())).thenReturn(otherMethod);
        when(otherMethod.getOSRevision()).thenReturn("revision");

        final String result = myClass.myMethod("TestString");

        assertEquals("revision", result);
        verify(myMethodRevisionCall, times(1));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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