Skip to content

Support @Mock injection in JUnit 5 method parameters #1348

@smoyer64

Description

@smoyer64

JUnit 5 has the ability to "inject" parameters into test methods via an extensible ParameterResolver interface. The JUnit 5 users guide provides an example that shows how such a ParameterResolver can supply mock objects as test parameters as follows (shamelessly copied from https://junit.org/junit5/docs/current/user-guide/#writing-tests-dependency-injection):

@ExtendWith(MockitoExtension.class)
class MyMockitoTest {

    @BeforeEach
    void init(@Mock Person person) {
        when(person.getName()).thenReturn("Dilbert");
    }

    @Test
    void simpleTestWithInjectedMock(@Mock Person person) {
        assertEquals("Dilbert", person.getName());
    }

}

The prototype MockitoExtension provided in the JUnit 5 samples project shows a simple implementation of the required supportsParameter() and resolveParameter() methods. (See: https://github.com/junit-team/junit5-samples/blob/r5.1.0/junit5-mockito-extension/src/main/java/com/example/mockito/MockitoExtension.java)

[ ] Add the ability to inject mock objects into test method parameters to the official MockitoExtension. References #445

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions