简体   繁体   中英

NullPointerException when stubbing using Mockito

I'm fairly new to Mockito but I'm getting a NullPointerError when attempting to stub the Texture class. 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); )

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 . The @InjectMocks is used to inject mock fields into the tested object automatically.

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);
    }
}

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