简体   繁体   English

使用 Mockito 存根时出现 NullPointerException

[英]NullPointerException when stubbing using Mockito

I'm fairly new to Mockito but I'm getting a NullPointerError when attempting to stub the Texture class.我对 Mockito 还很陌生,但是在尝试存根 Texture 类时出现 NullPointerError 错误。 Here is my test:这是我的测试:

import com.badlogic.gdx.graphics.Texture;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.junit.MockitoJUnitRunner;
import org.testng.annotations.BeforeMethod;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
 class EntityTest {

     @InjectMocks
     public Texture mockedImg;

     @BeforeMethod
     public void setup() {
         mockedImg = mock(Texture.class);
         when(mockedImg.getWidth()).thenReturn(5);
         when(mockedImg.getHeight()).thenReturn(5);
     }

    @Test
    public void doesAnyOfMyCodeWork() {
         Assertions.assertEquals(mockedImg.getHeight(),5);
    }
}

And here is the error I'm getting:这是我得到的错误:

java.lang.NullPointerException
    at EntityTest.doesAnyOfMyCodeWork(EntityTest.java:35) <19 internal calls>
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1540) <9 internal calls>
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1540) <18 internal calls>
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

(line 35 is Assertions.assertEquals(mockedImg.getHeight(),5); ) (第 35 行是Assertions.assertEquals(mockedImg.getHeight(),5);

Any help would be much appreciated!任何帮助将非常感激!

So I now have code that seems to work as the following:所以我现在的代码似乎如下所示:

import com.badlogic.gdx.graphics.Texture;
import org.junit.Before;
import org.junit.Test;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.ValueSource;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;

import static org.mockito.Mockito.*;


@RunWith(MockitoJUnitRunner.class)
public class EntityTest {

     @Mock
     public Texture mockedImg;

     @Before
     public void setup() {
         mockedImg = mock(Texture.class);
         lenient().when(mockedImg.getWidth()).thenReturn(5);
         lenient().when(mockedImg.getHeight()).thenReturn(5);
     }


     @Test
     public void doesAnyOfMyCodeWork() {
         Assertions.assertEquals(mockedImg.getHeight(),5);
     }
}

Which is really odd as I had almost the exact same code yesterday and it was pulling up a whole load of errors (although I've moved my test to a different source folder which might have helped).这真的很奇怪,因为我昨天有几乎完全相同的代码并且它拉出了一大堆错误(尽管我已经将我的测试移到了可能有帮助的不同源文件夹)。 Thanks for helping me out with this guys.谢谢你帮我解决这些家伙。

If you want to mock mockedImg you have to annotate it with @Mock .如果你想模拟mockedImg你必须用@Mock注释它。 The @InjectMocks is used to inject mock fields into the tested object automatically. @InjectMocks用于将模拟字段自动注入到测试对象中。

So a possible solution to your problem would be the following因此,您的问题的可能解决方案如下

 class EntityTest {

     @Mock
     public Texture mockedImg;

     @BeforeMethod
     public void setup() {
         when(mockedImg.getWidth()).thenReturn(5);
         when(mockedImg.getHeight()).thenReturn(5);
     }

    @Test
    public void doesAnyOfMyCodeWork() {
         Assertions.assertEquals(mockedImg.getHeight(),5);
    }
}

A simple way of testing without annotation:一种无需注释的简单测试方法:

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class EntityTest
{
    @Test
    public void doesAnyOfMyCodeWork()
    {
        Texture mockedImg = mock(Texture.class);
        when(mockedImg.getWidth()).thenReturn(5);
        when(mockedImg.getHeight()).thenReturn(5);
        Assertions.assertEquals(mockedImg.getHeight(), 5);
    }
}

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

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