-
Notifications
You must be signed in to change notification settings - Fork 38.6k
Open
Labels
in: testIssues in the test moduleIssues in the test moduletype: documentationA documentation taskA documentation task
Milestone
Description
I've provided a minimal JUnit Jupiter test ConcreteServiceTest
reproducing the problem. Please check the code first. The issue is that the mock is not reset so the second test fails since there are 2 calls instead of the expected 1. The use case has a common base service and similarly base test class.
Some observations:
- If the
@Import(ConcreteService.class)
is moved on top of the base class the tests pass. - If the tests are flattened (not using
@Nested
test class) then the tests pass. @AfterEach void clearMocks() { reset(dependency); }
would make the tests pass as well. I've chosen this as the simplest workaround for the time being (assuming the issue will be fixed and then the method will be simply deleted instead of altering the structure of my code).
Spring Boot version: 2.6.2
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.mockito.Mockito.verify;
@Import(ConcreteService.class)
class ConcreteServiceTest extends BaseServiceTest {
}
@ExtendWith(SpringExtension.class)
abstract class BaseServiceTest {
@MockBean
private Runnable dependency;
@Autowired
private BaseService service;
@Nested
class Suite {
@RepeatedTest(2)
void test() {
service.serve();
verify(dependency).run();
}
}
}
class ConcreteService extends BaseService {
}
abstract class BaseService {
@Autowired
private Runnable dependency;
public void serve() {
dependency.run();
}
}
cerberus777 and vslavov
Metadata
Metadata
Assignees
Labels
in: testIssues in the test moduleIssues in the test moduletype: documentationA documentation taskA documentation task