-
Notifications
You must be signed in to change notification settings - Fork 0
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
Add example failing test for issue #2656 #1
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,6 @@ | |
import java.lang.annotation.Target; | ||
|
||
import org.mockito.junit.MockitoJUnitRunner; | ||
import org.mockito.quality.Strictness; | ||
import org.mockito.stubbing.Answer; | ||
|
||
/** | ||
|
@@ -117,10 +116,44 @@ | |
boolean lenient() default false; | ||
|
||
/** | ||
* Mock will have custom strictness, see {@link MockSettings#strictness(Strictness)}. | ||
* Mock will have custom strictness, see {@link MockSettings#strictness(org.mockito.quality.Strictness)}. | ||
* For examples how to use 'Mock' annotation and parameters see {@link Mock}. | ||
* | ||
* @since 4.6.0 | ||
*/ | ||
Strictness strictness() default Strictness.STRICT_STUBS; | ||
Strictness strictness() default Strictness.NOT_SET; | ||
|
||
// Todo: unit test to ensure all values covered | ||
enum Strictness { | ||
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. I can add a test to ensure the two enums are kept in sync. |
||
|
||
/** | ||
* Default value used to indicate the mock does not override the inherited strictness. | ||
*/ | ||
NOT_SET(null), | ||
|
||
/** | ||
* see {@link org.mockito.quality.Strictness#LENIENT} | ||
*/ | ||
LENIENT(org.mockito.quality.Strictness.LENIENT), | ||
|
||
/** | ||
* see {@link org.mockito.quality.Strictness#WARN} | ||
*/ | ||
WARN(org.mockito.quality.Strictness.WARN), | ||
|
||
/** | ||
* see {@link org.mockito.quality.Strictness#STRICT_STUBS} | ||
*/ | ||
STRICT_STUBS(org.mockito.quality.Strictness.STRICT_STUBS); | ||
|
||
private final org.mockito.quality.Strictness outer; | ||
|
||
Strictness(org.mockito.quality.Strictness outer) { | ||
this.outer = outer; | ||
} | ||
|
||
public org.mockito.quality.Strictness outer() { | ||
return outer; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,10 +45,12 @@ public static Object processAnnotationForMock( | |
if (annotation.stubOnly()) { | ||
mockSettings.stubOnly(); | ||
} | ||
mockSettings.strictness(annotation.strictness()); | ||
if (annotation.lenient()) { | ||
mockSettings.lenient(); | ||
} | ||
if (annotation.strictness() != Mock.Strictness.NOT_SET) { | ||
mockSettings.strictness(annotation.strictness().outer()); | ||
} | ||
Comment on lines
+51
to
+53
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. Now we can not set strictness on the settings... |
||
|
||
// see @Mock answer default value | ||
mockSettings.defaultAnswer(annotation.answer()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -247,10 +247,10 @@ public MockSettings lenient() { | |
|
||
@Override | ||
public MockSettings strictness(Strictness strictness) { | ||
this.strictness = strictness; | ||
if (strictness == null) { | ||
throw strictnessDoesNotAcceptNullParameter(); | ||
} | ||
this.strictness = strictness; | ||
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. Moved as we shouldn't be mutating state on a bad input... |
||
return this; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,7 @@ public class CreationSettings<T> implements MockCreationSettings<T>, Serializabl | |
private boolean useConstructor; | ||
private Object outerClassInstance; | ||
private Object[] constructorArgs; | ||
protected Strictness strictness = Strictness.STRICT_STUBS; | ||
protected Strictness strictness = null; | ||
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. And we change the default to null... |
||
|
||
public CreationSettings() {} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ public class StrictnessMockAnnotationTest { | |
|
||
public @Rule MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS); | ||
|
||
@Mock(strictness = Strictness.LENIENT) | ||
@Mock(strictness = Mock.Strictness.LENIENT) | ||
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. This is an example of the breaking change... |
||
IMethods lenientMock; | ||
|
||
@Mock IMethods regularMock; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.mockitousage; | ||
|
||
import java.util.function.Predicate; | ||
|
||
public class ProductionCode { | ||
|
||
@SuppressWarnings("ReturnValueIgnored") | ||
public static void simpleMethod(Predicate<String> mock, String argument) { | ||
mock.test(argument); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method now returns local
Strictness
enum with aNOT_SET
default.(This is a breaking change for any code written using the
strictness
value of the annotation though)