Issue with Mocks initialization with JUnit 5
See original GitHub issueI’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:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@temp-droid Thanks for helping out! 🎉
Thank you so much , I totally forget that with MockitoExtention there is no need to call openMocks ✅
feel free to close this issue