简体   繁体   中英

spring boot junit mockito null pointer exception

Version Info :

Java : 11
Eclipse : 2021-03
Maven : Embedded 3.6

Service Class :

public class BusinessService {

private DataService dataService;

public BusinessService(DataService dataService) {
    super();
    this.dataService = dataService;
}

public int findTheGreatestFromAllData() {
    int[] data = dataService.retrieveAllData();
    int greatest = Integer.MIN_VALUE;

    for (int value : data) {
        if (value > greatest) {
            greatest = value;
        }
    }
    return greatest;
}

}

Data Retrieval :

public class DataService {
    
    public int[] retrieveAllData() {
        return new int[] { 1, 2, 3, 4, 5 };
    }

}

Junit test case:

@RunWith(MockitoJUnitRunner.class)
public class BusinessServicesMockTest {
    
    @Mock
    DataService dataServiceMock;

    @InjectMocks
    BusinessService businessImpl;
    
    @Test
    public void testFindTheGreatestFromAllData() {
        when(dataServiceMock.retrieveAllData()).thenReturn(new int[] { 24, 15, 3 });
        assertEquals(24, businessImpl.findTheGreatestFromAllData());
    }
}

dependencies in pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

Now when I am running the app as Junit test I got the below result:

java.lang.NullPointerException 
    at BusinessServicesMockTest.testFindTheGreatestFromAllData(BusinessServicesMockTest.java:31)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)

From this line 31:

    when(dataServiceMock.retrieveAllData()).thenReturn(new int[] { 24, 15, 3 });

Imports

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

How to resolve this error?

You mixed up JUnit 4 and JUnit 5 annotations.

  • org.junit.jupiter.api.Test comes from JUnit 5.
  • org.mockito.junit.MockitoJUnitRunner (and runners in general) come from JUnit 4
  • In JUnit 5, Runners have been replaced with Extensions

Thus, your runner is ignored, and mocks are not initialized. To have them initalized under JUnit5, use MockitoExtension instead.

@ExtendWith(MockitoExtension.class)
public class BusinessServicesMockTest {
//...
}

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