-
-
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
Sporadic mock verification failures related to hashCode/equals on 3.12.1 #2399
Comments
We seem to be getting some strange instability on Mockito 3.12.0 and 3.12.1. Reverting to see if things stabilise or whether this is a red herring. mockito/mockito#2399
I have the same issue after upgrade from
The issue is only reproducible in CI and only on Linux but is green locally or on Windows in CI. |
Hm, the fallout of #2331 is indeed unfortunate. Apologies for the inconvenience. Looking at the problems listed in this issue, the With the second problem (that @vyazelenko also appears to run into), seems to be an issue with While I think the new behavior is the way to go, especially in the Would that be a workable solution for you? |
Hm, wait. I just realized that both linked tests are using mocks, not spies. Then this behavior is unexpected and is a regression indeed. We should probably update the code from #2369 to only apply to spies. Would one of you be willing to contribute a potential fix for this? |
@TimvdLippe Here is a reproducer for this issue: public class TestMockito
{
private final long time = System.currentTimeMillis();
public boolean equals(final Object o)
{
if (!(o instanceof TestMockito))
{
return false;
}
return time == ((TestMockito)o).time;
}
public int hashCode()
{
return Long.hashCode(time);
}
public static void main(final String[] args)
{
final TestMockito test = new TestMockito();
final TestMockito spy = Mockito.spy(test);
final TestMockito mock = Mockito.mock(TestMockito.class);
if (!spy.equals(test) || spy.hashCode() != test.hashCode())
{
throw new Error("Spy is broken!");
}
if (!mock.equals(mock) || mock.hashCode() != mock.hashCode())
{
throw new Error("Mock is broken!");
}
}
} If you run this code as is with 3.12.1 you'll get the following exception: Exception in thread "main" java.lang.Error: Mock is broken!
at org.agrona.TestMockito.main(TestMockito.java:34) However if you swap Exception in thread "main" java.lang.Error: Spy is broken!
at org.agrona.TestMockito.main(TestMockito.java:30) So depending on what is called first the result changes. |
Aha. So the issue is that the second call breaks things. That's very unexpected. I presume that's happening because we are leaving some invalid state somewhere. However, from the code changes, it is not obvious as to how that could be the case. @raphw do you mind doing a post-commit review of 123beb8 and see if we accidentally did something wrong there? |
@TimvdLippe The problem is with the org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator#mockClass which does not include I'll add a PR to fix it. |
Thanks for digging @vyazelenko - seems to explain the semantics I observe. After digging around, I believe the cases we had all had both spies and mocks created across different tests for the same class. |
…inguish the mock types, i.e. to separate mocks from spies otherwise spy type is reused for a mock or vice versa.
PR that fixes this issue #2400. |
3.12.2 is currently being pushed to Maven Central and should be available in a couple of hours. |
@TimvdLippe Thanks for a quick release! |
Since updating from
3.11.2
to3.12.0
(and getting similar errors on3.12.1
), I have seen some strange sometimes-reproducible verification failures on Mockito for tests which previously passed consistently.I can't yet establish a pattern or simple reproduction; it feels like it is somehow dependent on the order of test evaluation/usage with respect to other tests within the same JVM instance, and I have been unable to replicate locally.
There are two main classes I am seeing
mocks with interactions being tracked (and thus failing
verifyNoInteractions(mock)
after being put intoHashSet
s (these did not seem to previously count as an interaction, and usually do not)https://github.com/gocd/gocd/blob/b7bba59201c857efc010c3493664802f218d2bae/server/src/test-fast/java/com/thoughtworks/go/server/materials/postcommit/git/GitPostCommitHookImplementerTest.java#L81-L94
mocks that are not considered equal to themselves when put inside a
Collection
and then searched forhttps://github.com/gocd/gocd/blob/b7bba59201c857efc010c3493664802f218d2bae/server/src/test-fast/java/com/thoughtworks/go/server/materials/postcommit/git/GitPostCommitHookImplementerTest.java#L41-L65
I have a handful of other examples too in different classes.
This could be confirmation bias, however since I saw some stuff was done with respect to hashCodes in #2331 I wondered if it could be related. Logging it here to see if anyone else has seen any similar weirdness or might know what is going on.
Environment
15.0.2
) even where it is replicable on build/CI automation environment.15.0.1
) but I cannot distinguish a pattern, and sometimes a different combination/ordering of tests does not seem to yield the same failure on exactly the same test code.The text was updated successfully, but these errors were encountered: