-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
automatically detect class to mock #2779
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
Merged
TimvdLippe
merged 6 commits into
mockito:main
from
asolntsev:automatically-detect-class-to-mock
Nov 14, 2022
+109
−1
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ac2c9f3
automatically detect class to mock
asolntsev adf07fe
add javadoc Section 5.4 describing new method `mock()`.
asolntsev 2fa44f7
add negative test for passing arguments to new mock() method
asolntsev 27807bc
add parameterless method `Mockito.spy()`
asolntsev d903bd6
Update src/main/java/org/mockito/Mockito.java
asolntsev 7232399
Update src/main/java/org/mockito/Mockito.java
asolntsev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -33,7 +33,11 @@ | |||||
| import org.mockito.stubbing.OngoingStubbing; | ||||||
| import org.mockito.stubbing.Stubber; | ||||||
| import org.mockito.stubbing.VoidAnswer1; | ||||||
| import org.mockito.verification.*; | ||||||
| import org.mockito.verification.After; | ||||||
| import org.mockito.verification.Timeout; | ||||||
| import org.mockito.verification.VerificationAfterDelay; | ||||||
| import org.mockito.verification.VerificationMode; | ||||||
| import org.mockito.verification.VerificationWithTimeout; | ||||||
|
|
||||||
| import java.util.function.Function; | ||||||
|
|
||||||
|
|
@@ -107,6 +111,7 @@ | |||||
| * <a href="#51">51. New API for marking classes as unmockable (Since 4.1.0)</a><br/> | ||||||
| * <a href="#52">52. New strictness attribute for @Mock annotation and <code>MockSettings.strictness()</code> methods (Since 4.6.0)</a><br/> | ||||||
| * <a href="#53">53. Specifying mock maker for individual mocks (Since 4.8.0)</a><br/> | ||||||
| * <a href="#54">54. Mocking/spying without specifying class (Since 4.9.0)</a><br/> | ||||||
| * </b> | ||||||
| * | ||||||
| * <h3 id="0">0. <a class="meaningful_link" href="#mockito2" name="mockito2">Migrating to Mockito 2</a></h3> | ||||||
|
|
@@ -1639,6 +1644,23 @@ | |||||
| * Foo mock = Mockito.mock(Foo.class, withSettings().mockMaker(MockMakers.SUBCLASS)); | ||||||
| * </code></pre> | ||||||
| * | ||||||
| * <h3 id="54">54. <a class="meaningful_link" href="#mock_without_class" name="mock_without_class"> | ||||||
| * Mocking/spying without specifying class</a> (Since 4.9.0)</h3> | ||||||
| * | ||||||
| * Instead of calling method {@link Mockito#mock(Class)} or {@link Mockito#spy(Class)} with Class parameter, you can now | ||||||
| * now call method {@code mock()} or {@code spy()} <strong>without parameters</strong>: | ||||||
| * | ||||||
| * <pre class="code"><code class="java"> | ||||||
| * Foo foo = Mockito.mock(); | ||||||
| * Bar bar = Mockito.spy(); | ||||||
| * </code></pre> | ||||||
| * | ||||||
| * Mockito will automatically detect the needed class. | ||||||
| * <p> | ||||||
| * It works only if you assign result of {@code mock()} or {@code spy()} to a variable or field with an explicit type. | ||||||
| * With an implicit type, the Java compiler is unable to automatically determine the type of a mock and you need | ||||||
| * to pass in the {@code Class} explicitly. | ||||||
| * </p> | ||||||
| */ | ||||||
| @CheckReturnValue | ||||||
| @SuppressWarnings("unchecked") | ||||||
|
|
@@ -1901,6 +1923,23 @@ public class Mockito extends ArgumentMatchers { | |||||
| */ | ||||||
| public static final Answer<Object> RETURNS_SELF = Answers.RETURNS_SELF; | ||||||
|
|
||||||
| /** | ||||||
| * Creates mock object of requested class or interface. | ||||||
| * <p> | ||||||
| * See examples in javadoc for {@link Mockito} class | ||||||
| * | ||||||
| * @param reified don't pass any values to it. It's a trick to detect the class/interface you want to mock. | ||||||
| * @return mock object | ||||||
| * @since 4.9.0 | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Too late, I know, but shouldn't it have been:
Suggested change
? |
||||||
| */ | ||||||
| public static <T> T mock(T... reified) { | ||||||
| if (reified.length > 0) { | ||||||
| throw new IllegalArgumentException( | ||||||
| "Please don't pass any values here. Java will detect class automagically."); | ||||||
szpak marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| } | ||||||
| return mock(getClassOf(reified), withSettings()); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Creates mock object of given class or interface. | ||||||
| * <p> | ||||||
|
|
@@ -2115,6 +2154,25 @@ public static <T> T spy(Class<T> classToSpy) { | |||||
| classToSpy, withSettings().useConstructor().defaultAnswer(CALLS_REAL_METHODS)); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Please refer to the documentation of {@link #spy(Class)}. | ||||||
| * | ||||||
| * @param reified don't pass any values to it. It's a trick to detect the class/interface you want to mock. | ||||||
| * @return spy object | ||||||
| * @since 4.9.0 | ||||||
| */ | ||||||
| public static <T> T spy(T... reified) { | ||||||
| if (reified.length > 0) { | ||||||
| throw new IllegalArgumentException( | ||||||
| "Please don't pass any values here. Java will detect class automagically."); | ||||||
| } | ||||||
| return spy(getClassOf(reified)); | ||||||
| } | ||||||
|
|
||||||
| private static <T> Class<T> getClassOf(T[] array) { | ||||||
| return (Class<T>) array.getClass().getComponentType(); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Creates a thread-local mock controller for all static methods of the given class or interface. | ||||||
| * The returned object's {@link MockedStatic#close()} method must be called upon completing the | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.