question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with Mocks initialization with JUnit 5

See original GitHub issue

I’m trying to write some tests cases on a service class, but I got NPE at runtime.

Expected Behavior

Tests should be green

Actual Behavior

Tests failed with NPE

Steps to Reproduce

Complete project -> reactor-mockito-junit5-bug-report

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class BugServiceTest {

    @Mock
    BugRepository repository;

    @InjectMocks
    BugService service;

    @BeforeEach
        void setUp() {
          MockitoAnnotations.initMocks(this);
     }

     @Test
     void emptyFluxTest() {
            Mockito.when(repository.findAll())
                    .thenReturn(Flux.empty());

            service.findAll()
                    .as(StepVerifier::create)
                    .verifyComplete();
     }

}

Possible Solution

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class AlternativeSolutionServiceTest {

    @Mock
    BugRepository repository;

    @InjectMocks
    BugService service;

    @Nested
    class NestedClass {

        @BeforeEach
        void setUp() {
            MockitoAnnotations.initMocks(this);
        }

        @Test
        void emptyFluxTest() {
            Mockito.when(repository.findAll())
                    .thenReturn(Flux.empty());

            service.findAll()
                    .as(StepVerifier::create)
                    .verifyComplete();
        }

    }
}

Your Environment

  • Reactor version used: 3.2.6.RELEASE
  • JVM version: OpenJDK 1.8.0_272
  • OS and version : Windows 10

Related to reactor-2826

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
TimvdLippecommented, Oct 28, 2021

@temp-droid Thanks for helping out! 🎉

1reaction
aoudiamoncefcommented, Oct 28, 2021

Thank you so much , I totally forget that with MockitoExtention there is no need to call openMocks ✅

feel free to close this issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mockito: 3 Ways to Init Mock in JUnit 5 - Mincong Huang
Initializes mocks annotated with @Mock , so that explicit usage of MockitoAnnotations#initMocks(Object) is not necessary. Mocks are initialized ...
Read more >
How to use Mockito with JUnit5 - java - Stack Overflow
Manually. Creating mocks manually with Mockito::mock works regardless of the JUnit version (or test framework for that matter). Annotation Based.
Read more >
Using Mockito With JUnit 5 | Code With Arho
Learn how to use the Mockito mocking framework with JUnit 5. ... Manual initialization can be a legit solution if we don't have...
Read more >
Mockito and JUnit 5 - Using ExtendWith - Baeldung
Mockito throws an UnsupportedStubbingException when an initialized mock is not called by one of the test methods during execution. We can avoid this...
Read more >
Spring Boot Tests with Mockito and JUnit 5 - HowToDoInJava
If we are not using the MockitoJUnitRunner class approach, then we can use the static method MockitoAnnotations.initMocks() . Upon initialization of junit tests ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found