-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Regression? Strictness set in @MockitoSettings
ignored after upgrade from 4.5.1 to 4.6.0
#2656
Comments
Example failing test for mockito#2656
I was coming to the same conclusion and basic re-prod steps. Thanks for raising this! mockito/src/main/java/org/mockito/internal/stubbing/StrictnessSelector.java Lines 28 to 39 in 70cf2d2
mockito/src/main/java/org/mockito/internal/creation/settings/CreationSettings.java Line 48 in 70cf2d2
Which I think means it's impossible for it to fall back to The weird thing is you wont get failures due to The secondary problem we have which obscured this one is that tracking stubbings like this seems to cause issues for our Groovy-based tests since there are NPEs thrown in mockito/src/main/java/org/mockito/internal/junit/DefaultStubbingLookupListener.java Lines 78 to 81 in 6f9108b
|
That indeed sounds like a regression, apologies for that. Since we already have a PR with a regression test, can one of you also supply the fix for it? If all other tests in our suite pass, we should be good to go. |
No worries! ;) I'm happy to submit a PR, but before I do I think its worth discussing what the fix should be. It seems to me that the issue is the default values for strictness introduced by @sivaprasadreddy. By defaulting the new strictness to I'd therefore suggest removing the defaults, i.e. setting them to a 'not set' value (which would mean extending I'll have a quick hack about to see how things turn out, but any thoughts on this appreciated. |
The intent of the PR was to introduce a new API surface for configuring strictness, not to change the default. So I consider it a regression that the new default became STRICT_STUBS. Not sure how I missed that in the review. Setting it to an unset value (e.g. null) sounds like the appropriate solution to me. |
Cool. Though the challenge here is that I can switch it to use a local Code written against 4.6.0 release: public class StrictnessMockAnnotationTest {
public @Rule MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
@Mock(strictness = Strictness.LENIENT)
IMethods lenientMock; Would need to change to: public class StrictnessMockAnnotationTest {
public @Rule MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
@Mock(strictness = Mock.Strictness.LENIENT)
IMethods lenientMock; (Note the additional prefix of |
Looking at the original PR again, I indeed completely overlooked the default values. These should have been |
I think changing defaults won't fix the issue that the defaults stop the test level settings ever taking affect. |
If I understand the new code of the |
I don't think so. The Give me one sec and I'll dump an example into my PR, |
Done. See big-andy-coates#1 |
What if we add a new value to It's not ideal to ship this to users, but I think it's the most explicit and also will fix this issue? |
Yeah, I had thought of that. It would fix the issue and wouldn't be a breaking change for 4.6.0 -> 4.6.1, but... It leaves this For example, it wouldn't make sense to set the test level strictness to For example, what would be the behaviour if someone used @MockitoSettings(strictness = Strictness.TEST_DEFAULT_VALUE)
class MyWeirdTest {
...
} I guess we could update all the code to check for this new value and default to As I see it the two options are:
I'm happy to go with either. I can even knock up PRs for both if you want. I'm not sure what the policy is on breaking changes on newly released features that are broken. What's the time frame for 4.6.1? |
In that case, I think option 1 is the cleanest. Since we just shipped the new feature, I would be okay wit changing that. It is unfortunate, but something that we can patch over after a day. |
OK, PR coming soon... (FYI, I've just been trying to raise the PR for option 2 and its not pretty, so I think this is the right call) |
Example failing test for mockito#2656
fixes: mockito#2656
@TimvdLippe PR raised: #2659 |
Thanks to both of you! Seems to work fine for us now on |
I encountered the NPE by @chadlwilson in java (not groovy) should a new ticket created for it - or just bump the already existing one #2667 |
If the root cause is the same (sourceFile location not known for a |
Upgrading from Mockito 4.5.1 to 4.6.0 and it looks to me as though #2650 or something around there has introduced a regression.
I'm seeing
PotentialStubbingProblem
exceptions where previously there were none, because the test class is annotated with@MockitoSettings(strictness = Strictness.LENIENT)
.The issue seems to be that
StrictnessSelector
prefers the strictness set in the mock to the test level strictness, and the mock defaults toSTRICT_STUBS
, so always overrides the strictness set in@MockitoSettings
.I've added a failing example test that demonstrates the issue here: big-andy-coates#1
The above test fails with:
Note that some configuration are impossible to mock via Mockito
(same as any question on stackoverflow.com)
The text was updated successfully, but these errors were encountered: