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.

Support @Mock injection in JUnit 5 method parameters

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:21 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
TimvdLippecommented, Mar 26, 2018

Okay, to write down the reason I think mock sharing by parameter is not a good idea:

First of all, there is a lot of duplicate code. For every test method, you need to list the mocks, which results in duplicate. Rather, using fields for these mocks removes the duplication.

Secondly, by relying on fields, the compiler will complain when you make a typo (or change a type). With parameters, you rely on the name (String-based) and the type declared. Both are not compiler safe.

Lastly, because you can rely on fields, refactoring is a lot easier, as IDE’s support renaming by field. This is not possible for parameters.

For these 3 reasons, I see field mock sharing superior to parameter mock sharing and am therefore against introducing this logic in the parameter resolution.

2reactions
TimvdLippecommented, Mar 26, 2018

Opened #1350 with a proposal implementation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mockito and JUnit 5 - Using ExtendWith - Baeldung
First, we'll show how to create an extension that automatically creates mock objects for any class attribute or method parameter annotated with @Mock....
Read more >
Using Mockito With JUnit 5 | Code With Arho
Learn how to use the Mockito mocking framework with JUnit 5. ... we can also inject mock objects to method parameters.
Read more >
JUnit 5 Tutorial: Writing Parameterized Tests - Petri Kainulainen
This blog post describes how we can write parameterized tests with JUnit 5. After we have finished this blog post, we: Can get...
Read more >
Why You Should Start Injecting Mocks as Method Arguments
One of the big improvements that came in JUnit 5 was support for dependency injection via constructors and methods.
Read more >
Dependency Injection and Testing in JUnit 5 - Manning
If a test class constructor, a test method, or a lifecycle method accepts a parameter, the parameter must be resolved at runtime by...
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