Commit
@Mock
annotation and `Mo…
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (c) 2022 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockitousage.strictness; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.exceptions.misusing.PotentialStubbingProblem; | ||
import org.mockito.junit.MockitoJUnit; | ||
import org.mockito.junit.MockitoRule; | ||
import org.mockito.quality.Strictness; | ||
import org.mockitousage.IMethods; | ||
|
||
import static org.mockito.Mockito.when; | ||
|
||
public class StrictnessMockAnnotationTest { | ||
|
||
public @Rule MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
@Mock(strictness = Strictness.LENIENT) | ||
IMethods lenientMock; | ||
|
||
@Mock IMethods regularMock; | ||
|
||
@Test | ||
public void mock_is_lenient() { | ||
when(lenientMock.simpleMethod("1")).thenReturn("1"); | ||
|
||
// then lenient mock does not throw: | ||
ProductionCode.simpleMethod(lenientMock, "3"); | ||
} | ||
|
||
@Test | ||
public void mock_is_strict() { | ||
when(regularMock.simpleMethod("2")).thenReturn("2"); | ||
|
||
Assertions.assertThatThrownBy(() -> ProductionCode.simpleMethod(regularMock, "4")) | ||
.isInstanceOf(PotentialStubbingProblem.class); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (c) 2022 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockitousage.strictness; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.mockito.exceptions.misusing.PotentialStubbingProblem; | ||
import org.mockito.junit.MockitoJUnit; | ||
import org.mockito.junit.MockitoRule; | ||
import org.mockito.quality.Strictness; | ||
import org.mockitousage.IMethods; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
public class StrictnessWithSettingsTest { | ||
|
||
public @Rule MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS); | ||
|
||
IMethods lenientMock; | ||
IMethods regularMock; | ||
IMethods strictMock; | ||
|
||
@Before | ||
public void before() { | ||
lenientMock = mock(IMethods.class, withSettings().strictness(Strictness.LENIENT)); | ||
regularMock = mock(IMethods.class); | ||
strictMock = mock(IMethods.class, withSettings().strictness(Strictness.STRICT_STUBS)); | ||
} | ||
|
||
@Test | ||
public void mock_is_lenient() { | ||
when(lenientMock.simpleMethod("1")).thenReturn("1"); | ||
|
||
// lenient mock does not throw | ||
ProductionCode.simpleMethod(lenientMock, "3"); | ||
} | ||
|
||
@Test | ||
public void mock_is_strict_with_default_settings() { | ||
when(regularMock.simpleMethod("3")).thenReturn("3"); | ||
|
||
Assertions.assertThatThrownBy(() -> ProductionCode.simpleMethod(regularMock, "4")) | ||
.isInstanceOf(PotentialStubbingProblem.class); | ||
} | ||
|
||
@Test | ||
public void mock_is_strict_with_explicit_settings() { | ||
when(strictMock.simpleMethod("2")).thenReturn("2"); | ||
|
||
Assertions.assertThatThrownBy(() -> ProductionCode.simpleMethod(strictMock, "5")) | ||
.isInstanceOf(PotentialStubbingProblem.class); | ||
} | ||
} |
3 comments
on commit 70cf2d2
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.
Hello!
It looks like this commit changes the default behavior when using MockitoJUnit.rule()
with unspecified strictness and @Mock
with unspecified strictness from WARN
:
return new JUnitRule(Plugins.getMockitoLogger(), Strictness.WARN); |
STRICT_STUBS
(as "defined" in the annotation).
Is there a way to adjust the annotation so there is a sentinel default of "unspecified strictness", to allow rule runners to do the specification of a default strictness?
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 should be fixed with #2659
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.
Ah, thanks, should've checked the Issues first. Thanks for the quick fix!
ex: If you remove the fixed strictness setting here, the test will still pass, even though one would expect that the regularMock would not have otherwise been considered strict.