Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix cache key computation for generated mock/spy classes (#2400)
Add defaultAnswer to the `MockitoMockKey` to distinguish the mock types, i.e. to separate mocks from spies. Otherwise, spies and mocks of the same class might accidentally rely on the same generated type class, resulting in spurious test failures (depending on which mock/spy is created first). Fixes #2399
- Loading branch information
1 parent
b04c703
commit 4f81d4f
Showing
4 changed files
with
145 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/test/java/org/mockitousage/spies/MockCreationShouldNotAffectSpyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (c) 2007 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockitousage.spies; | ||
|
||
import org.junit.Test; | ||
import org.mockitoutil.TestBase; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.spy; | ||
|
||
public class MockCreationShouldNotAffectSpyTest extends TestBase { | ||
|
||
@Test | ||
public void test() { | ||
TestClass instance = new TestClass(42); | ||
|
||
TestClass mock = mock(TestClass.class); | ||
assertEquals(mock.hashCode(), mock.hashCode()); | ||
assertEquals(mock, mock); | ||
|
||
TestClass spy = spy(instance); | ||
assertEquals(instance.hashCode(), spy.hashCode()); | ||
assertEquals(spy, instance); | ||
assertEquals(instance, spy); | ||
} | ||
|
||
static class TestClass { | ||
private final long value; | ||
|
||
TestClass(final long value) { | ||
this.value = value; | ||
} | ||
|
||
public boolean equals(final Object o) { | ||
if (!(o instanceof TestClass)) { | ||
return false; | ||
} | ||
return value == ((TestClass) o).value; | ||
} | ||
|
||
public int hashCode() { | ||
return Long.hashCode(value); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/java/org/mockitousage/spies/SpyCreationShouldNotAffectMockTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (c) 2007 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockitousage.spies; | ||
|
||
import org.junit.Test; | ||
import org.mockitoutil.TestBase; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.spy; | ||
|
||
public class SpyCreationShouldNotAffectMockTest extends TestBase { | ||
|
||
@Test | ||
public void test() { | ||
TestClass instance = new TestClass(Long.MIN_VALUE); | ||
|
||
TestClass spy = spy(instance); | ||
assertEquals(instance.hashCode(), spy.hashCode()); | ||
assertEquals(spy, instance); | ||
assertEquals(instance, spy); | ||
|
||
TestClass mock = mock(TestClass.class); | ||
assertEquals(mock.hashCode(), mock.hashCode()); | ||
assertEquals(mock, mock); | ||
} | ||
|
||
static class TestClass { | ||
private final long value; | ||
|
||
TestClass(final long value) { | ||
this.value = value; | ||
} | ||
|
||
public boolean equals(final Object o) { | ||
if (!(o instanceof TestClass)) { | ||
return false; | ||
} | ||
return value == ((TestClass) o).value; | ||
} | ||
|
||
public int hashCode() { | ||
return Long.hashCode(value); | ||
} | ||
} | ||
} |