Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add annotation to mark a type as DoNotMock (#1833)
Mocking types that users not own [1] or are severely complicating test logic [2] leads to brittle or wrong tests. In particular, the StackOverflow answer is wrong, as the contract of java.util.Map is violated. When a new key is added to the Map, the stubbed return would be wrong. In Google we have used the DoNotMock annotation via ErrorProne [3] to annotate these types, as well as an internal list of types that can't be mocked (this includes several java.util types). We are using a custom Mockmaker to enforce this on run-time. Based on our successful experience with DoNotMock (we have seen a large reduction in bad/broken tests for types involved), we are proposing to open source this into Mockito itself. The DoNotMock annotation can be added to any type, e.g. classes and interfaces. If, in the type hierarchy of the class-to-be-mocked, there is a type that is annotated with DoNotMock, Mockito will throw a DoNotMockException. This would help preventing issues such as #1827 and #1734 which is in-line with the guidance on our wiki [1]. A follow-up change would allow us to define external types (like the java.util types) that can't be mocked. (We can't add the annotation to the types, as they live in the JDK instead.) This PR also introduces the DoNotMockEnforcer interface which users can override to implement their special handling of types annotated with DoNotMock. [1]: https://github.com/mockito/mockito/wiki/How-to-write-good-tests#dont-mock-a-type-you-dont-own [2]: https://stackoverflow.com/a/15820143 [3]: https://errorprone.info/api/latest/com/google/errorprone/annotations/DoNotMock.html
- Loading branch information
1 parent
102cc38
commit ebc1685
Showing
14 changed files
with
405 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (c) 2019 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockito; | ||
|
||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Annotation representing a type that should not be mocked. | ||
* <p>When marking a type {@code @DoNotMock}, you should always point to alternative testing | ||
* solutions such as standard fakes or other testing utilities. | ||
* | ||
* Mockito enforces {@code @DoNotMock} with the {@link org.mockito.plugins.DoNotMockEnforcer}. | ||
* | ||
* If you want to use a custom {@code @DoNotMock} annotation, the {@link org.mockito.plugins.DoNotMockEnforcer} | ||
* will match on annotations with a type ending in "org.mockito.DoNotMock". You can thus place | ||
* your custom annotation in {@code com.my.package.org.mockito.DoNotMock} and Mockito will enforce | ||
* that types annotated by {@code @com.my.package.org.mockito.DoNotMock} can not be mocked. | ||
* | ||
* <pre class="code"><code class="java"> | ||
* @DoNotMock(reason = "Use a real instance instead") | ||
* class DoNotMockMe {} | ||
* </code></pre> | ||
*/ | ||
@Target({TYPE}) | ||
@Retention(RUNTIME) | ||
@Documented | ||
public @interface DoNotMock { | ||
/** | ||
* The reason why the annotated type should not be mocked. | ||
* | ||
* <p>This should suggest alternative APIs to use for testing objects of this type. | ||
*/ | ||
String reason() default "Create a real instance instead."; | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/org/mockito/exceptions/misusing/DoNotMockException.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright (c) 2019 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockito.exceptions.misusing; | ||
|
||
import org.mockito.exceptions.base.MockitoException; | ||
|
||
/** | ||
* Thrown when attempting to mock a class that is annotated with {@link org.mockito.DoNotMock}. | ||
*/ | ||
public class DoNotMockException extends MockitoException { | ||
public DoNotMockException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains 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
30 changes: 30 additions & 0 deletions
30
src/main/java/org/mockito/internal/configuration/DefaultDoNotMockEnforcer.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (c) 2019 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockito.internal.configuration; | ||
|
||
import java.lang.annotation.Annotation; | ||
|
||
import org.mockito.DoNotMock; | ||
import org.mockito.plugins.DoNotMockEnforcer; | ||
|
||
public class DefaultDoNotMockEnforcer implements DoNotMockEnforcer { | ||
|
||
@Override | ||
public String checkTypeForDoNotMockViolation(Class<?> type) { | ||
for (Annotation annotation : type.getAnnotations()) { | ||
if (annotation.annotationType().getName().endsWith("org.mockito.DoNotMock")) { | ||
String exceptionMessage = | ||
type + " is annotated with @org.mockito.DoNoMock and can't be mocked."; | ||
if (DoNotMock.class.equals(annotation.annotationType())) { | ||
exceptionMessage += " " + type.getAnnotation(DoNotMock.class).reason(); | ||
} | ||
|
||
return exceptionMessage; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright (c) 2019 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockito.plugins; | ||
|
||
/** | ||
* Enforcer that is applied to every type in the type hierarchy of the class-to-be-mocked. | ||
*/ | ||
public interface DoNotMockEnforcer { | ||
|
||
/** | ||
* If this type is allowed to be mocked. Return an empty optional if the enforcer allows | ||
* this type to be mocked. Return a message if there is a reason this type can not be mocked. | ||
* | ||
* Note that Mockito performs traversal of the type hierarchy. Implementations of this class | ||
* should therefore not perform type traversal themselves but rely on Mockito. | ||
* | ||
* @param type The type to check | ||
* @return Optional message if this type can not be mocked, or an empty optional if type can be mocked | ||
*/ | ||
String checkTypeForDoNotMockViolation(Class<?> type); | ||
} |
Oops, something went wrong.