Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation: @Mock on method parameters #1960

Closed
jamietanna opened this issue Jun 28, 2020 · 3 comments · Fixed by #1961
Closed

Documentation: @Mock on method parameters #1960

jamietanna opened this issue Jun 28, 2020 · 3 comments · Fixed by #1961

Comments

@jamietanna
Copy link
Contributor

While writing an article about using @Mock as a method parameter, I noticed that the JavaDoc for Mockito doesn't mention the ability to use @Mock as a parameter to a test method.

How would be best to add that as a PR, given it's dependent on the test runner?

I was thinking something like:

 * <h3 id="9">9. <a class="meaningful_link" href="#mock_annotation" name="mock_annotation">Shorthand for mocks creation - <code>&#064;Mock</code> annotation</a></h3>
 *
 * <ul>
 * <li>Minimizes repetitive mock creation code.</li>
 * <li>Makes the test class more readable.</li>
 * <li>Makes the verification error easier to read because the <b>field name</b>
 * is used to identify the mock.</li>
 * </ul>
 *
 * <pre class="code"><code class="java">
 *   public class ArticleManagerTest {
 *
 *       &#064;Mock private ArticleCalculator calculator;
 *       &#064;Mock private ArticleDatabase database;
 *       &#064;Mock private UserProvider userProvider;
 *
 *       private ArticleManager manager;
 *
 *       @Test
 *       void testSomething(@Mock ArticleDatabase database) {
 * </code></pre>
@TimvdLippe
Copy link
Contributor

We mention that information in https://javadoc.io/doc/org.mockito/mockito-junit-jupiter/latest/org/mockito/junit/jupiter/MockitoExtension.html as we don't support initiating Mocks on parameters with JUnit4. Therefore, we should add a parameter something like:

 *       @Test
 *       void testSomething(@Mock ArticleDatabase canOnlyBeInstantiatedWithJUnit5) {

Other than that, please open a PR 😄

@jamietanna
Copy link
Contributor Author

Oh I didn't realise that, thanks! I'll amend my article with that knowledge too.

Am I OK to add a PR for both the v2 and v3 documentation, given it works on both versions?

@TimvdLippe
Copy link
Contributor

We no longer publish v2 updates, so v3 only.

jamietanna added a commit to jamietanna/mockito that referenced this issue Jun 29, 2020
Although we've called it out in the [JUnit extension], we should also
make sure it's clear in the core documentation to make it more visible.

We can fully-quality the `@Test` annotation to make clear that it has to
be with JUnit 5.

Closes mockito#1960.

[JUnit extension]: https://javadoc.io/doc/org.mockito/mockito-junit-jupiter/latest/org/mockito/junit/jupiter/MockitoExtension.html
TimvdLippe pushed a commit that referenced this issue Jun 29, 2020
Although we've called it out in the [JUnit extension], we should also
make sure it's clear in the core documentation to make it more visible.

We can fully-quality the `@Test` annotation to make clear that it has to
be with JUnit 5.

Closes #1960.

[JUnit extension]: https://javadoc.io/doc/org.mockito/mockito-junit-jupiter/latest/org/mockito/junit/jupiter/MockitoExtension.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants