Skip to content

NPE while running test in Spring application (JUnit 5, Mockito 3, Spring JPA repositories)

I’m running a basic Spring App with Mockito 3.1.0 and and Junit 5.5.2. I have a Service call that includes two Spring Data JPA repositories. These are passed into the constructor for DI (along with two others that are immaterial – I’m including them in case they could also, potentially, cause errors.) I see no issues with the service when the app runs.

When I run my test, I get a NPE for myService. Stepping through the stack trace, hasn’t really shown me anything that relates to the error. I have also tried (following this Article: updating my test class to look like this:

@RunWith(JUnitPlatform.class) // This dependency doesn't seem to exist
public class MyServiceTest {
    // ...

    // not the JUnit4 @Before annotation.
    // Interestingly, this gives me NPEs for the repositories, not the service.
    public void setup(){
        // ...

to no avail. What I suspect is happening is that something about my setup isn’t properly wired up – either as dependencies or syntax for DI.

How do I debug this? What am I missing? Thanks in advance!


import org.springframework.stereotype.Service;

public class MyService {
    private final Repository1 repository1;
    private final Repository2 repository2;
    private final Repository3 repository3;
    private final Repository4 repository4;

  public MyService(Repository1 repository1,
      Repository2 repository2,
      Repository3 repository3,
      Repository4 repository4) {
    this.repository1 = repository1;
    this.repository2 = repository2;
    this.repository3 = repository3;
    this.repository4 = repository4;

    public Boolean computeValue(String someInput) {
        // does computations with repository1, repository2.


import static org.mockito.Mockito.when;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

public class MyServiceTest {

    private Repository1 repository1;

    private Repository2 repository2;

    private Repository3 repository3;

    private Repository4 repository4;

    private MyService myService;

    public void setup {
        when(repository1.findAll()).thenReturn(new ArrayList<>());
        when(repository1.findAllByInput(anyString())).thenReturn(new ArrayList<>());
        // Yes; I'm aware that this could also be a call to
        // MockitoAnnotations.initMocks(this). I've tried it:
        // it doesn't work. Also, I've intentionally not taken this
        // approach due to reasons:
        //   -

  void callMyService() {

Sample Repository:

import org.springframework.stereotype.Repository;

// This is just an example, but this pattern is repeated against all
// repositories in the project.
public interface Repository1 extends JpaRepository<Repository1, String> {


  • I forgot to mention that I have other files in this project that are using exactly these annotations (@RunWith(MockitoJUnitRunner.class), @Mock, @InjectMocks, @Before) that are not failing.
  • I updated the files with the relevant imports, and added an example of RepositoryN.
  • I update the MyService class to better reflect the parameters.



For anybody else who encounters this in the future, we were able to fix this problem by changing one of the imports from:

import org.junit.jupiter.api.Test;


import org.junit.Test;


  • This had to do with differing versions of JUnit. There’s a good long-form explanation as to why here.
9 People found this is helpful