Affects: both latest 2.7.9 and 3.0.4
If properties are defined in WebFluxTest and nested test classes are used, mocks are not reset between tests:
@WebFluxTest(controllers = DataController.class, properties = "property=value")
@TestClassOrder(ClassOrderer.OrderAnnotation.class)
class FailingTest {
@Autowired
private WebTestClient webTestClient;
@MockBean
private DataService service;
@BeforeEach
void setUp() {
given(service.get()).willReturn("test");
}
@Nested
@Order(1)
class WhenExecutingFirstRequest {
@BeforeEach
void setUp() {
webTestClient.get().uri("/data1").exchange();
}
@Test
void thenVerifyServiceCall() {
verify(service).get();
}
@Test
void thenVerifyServiceCallAgainInSameNestedClass() {
verify(service).get(); //fails because invocation from earlier test is counted as well
}
}
@Nested
@Order(2)
class WhenExecutingSecondRequest {
@BeforeEach
void setUp() {
webTestClient.get().uri("/data2").exchange();
}
@Test
void thenVerifyServiceCallInSiblingNestedClass() {
verify(service).get(); //fails because invocations from earlier tests are counted as well
}
}
}
It only affects nested test classes. There is no issue if used without nested classes like this:
@WebFluxTest(controllers = DataController.class, properties = "property=value")
class NotNestedTest {
@Autowired
private WebTestClient webTestClient;
@MockBean
private DataService service;
@BeforeEach
void setUp() {
given(service.get()).willReturn("test");
}
@Test
void shouldCallServiceOnFirstRequest() {
webTestClient.get().uri("/data1").exchange();
verify(service).get();
}
@Test
void shouldCallServiceOnSecondRequest() {
webTestClient.get().uri("/data2").exchange();
verify(service).get();
}
}
Example project: run with mvn clean package.
The only difference between WorkingTest and FailingTest is that FailingTest defines one property for tests whereas WorkingTest does not:
@WebFluxTest(controllers = DataController.class, properties = "property=value") vs @WebFluxTest(controllers = DataController.class)
webflux-nested-test-with-properties.zip
Comment From: ianbrandt
I believe I just hit this same issue:
https://github.com/sdkotlin/sd-kotlin-spring-talks/blob/213f5745bee7543b7ff26ffebd610100230bbc30/subprojects/child-context/rest-api/src/it/kotlin/org/sdkotlin/springdemo/childcontext/rest/ChildContextControllerIT.kt#L42
If I run test create for service exception()—which stubs a service exception—before test create(), the latter fails on that service exception. They pass the other way around.
Seems to impact both Mockito and Ninja-Squad/springmockk.
Possibly related: #23984 and #12470.
Comment From: wilkinsona
Thanks for the report and minimal sample, @denvir. This is a duplicate of #33317 that I have just fixed in 2.7.x onwards.