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

Add example failing test for issue #2656 #1

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 37 additions & 4 deletions src/main/java/org/mockito/Mock.java
Expand Up @@ -13,7 +13,6 @@
import java.lang.annotation.Target;

import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.quality.Strictness;
import org.mockito.stubbing.Answer;

/**
Expand All @@ -34,12 +33,13 @@
* @Mock(name = "database") private ArticleDatabase dbMock;
* @Mock(answer = RETURNS_MOCKS) private UserProvider userProvider;
* @Mock(extraInterfaces = {Queue.class, Observer.class}) private ArticleMonitor articleMonitor;
* @Mock(strictness = Mock.Strictness.LENIENT) private ArticleConsumer articleConsumer;
* @Mock(stubOnly = true) private Logger logger;
*
* private ArticleManager manager;
*
* @Before public void setup() {
* manager = new ArticleManager(userProvider, database, calculator, articleMonitor, logger);
* manager = new ArticleManager(userProvider, database, calculator, articleMonitor, articleConsumer, logger);
* }
* }
*
Expand Down Expand Up @@ -117,10 +117,43 @@
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;
Copy link
Owner Author

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 a NOT_SET default.

(This is a breaking change for any code written using the strictness value of the annotation though)


enum Strictness {

/**
* 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;
}
}
}
Expand Up @@ -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
Copy link
Owner Author

Choose a reason for hiding this comment

The 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());
Expand Down
Expand Up @@ -247,10 +247,10 @@ public MockSettings lenient() {

@Override
public MockSettings strictness(Strictness strictness) {
this.strictness = strictness;
if (strictness == null) {
throw strictnessDoesNotAcceptNullParameter();
}
this.strictness = strictness;
Copy link
Owner Author

Choose a reason for hiding this comment

The 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;
}

Expand Down
Expand Up @@ -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;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And we change the default to null...


public CreationSettings() {}

Expand Down
28 changes: 28 additions & 0 deletions src/test/java/org/mockito/MockTest.java
@@ -0,0 +1,28 @@
package org.mockito;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(value = Parameterized.class)
public class MockTest {

public org.mockito.quality.Strictness strictness;

public MockTest(org.mockito.quality.Strictness strictness) {
this.strictness = strictness;
}

@Test
public void shouldHaveMatchingEnumInMockStrictnessEnum() {
final Mock.Strictness mockStrictness = Mock.Strictness.valueOf(this.strictness.name());
assertThat(mockStrictness.outer()).isEqualTo(strictness);
}

@Parameterized.Parameters
public static org.mockito.quality.Strictness[] data() {
return org.mockito.quality.Strictness.values();
}
}
Expand Up @@ -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)
Copy link
Owner Author

Choose a reason for hiding this comment

The 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;
Expand Down
@@ -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);
}
}
Expand Up @@ -21,7 +21,9 @@
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
Expand Down Expand Up @@ -162,6 +164,29 @@ void inherits_strictness_from_base_class() {
assertThat(result.getStatus()).isEqualTo(TestExecutionResult.Status.SUCCESSFUL);
}

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
static class LenientMockitoSettings {

@Mock
private Predicate<String> rootMock;

@Test
void should_not_throw_on_potential_stubbing_issue() {
Mockito.doReturn(true).when(rootMock).test("Foo");

ProductionCode.simpleMethod(rootMock, "Bar");
}
}

@Test
void use_strictness_from_settings_annotation() {
TestExecutionResult result = invokeTestClassAndRetrieveMethodResult(LenientMockitoSettings.class);

assertThat(result.getThrowable()).isEqualTo(Optional.empty());
assertThat(result.getStatus()).isEqualTo(TestExecutionResult.Status.SUCCESSFUL);
}

private TestExecutionResult invokeTestClassAndRetrieveMethodResult(Class<?> clazz) {
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(
Expand Down