forked from mockito/mockito
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes mockito#2399 : Adds defaultAnswer to the MockitoMockKey to dist…
…inguish the mock types, i.e. to separate mocks from spies otherwise spy type is reused for a mock or vice versa.
- Loading branch information
1 parent
b04c703
commit 50bf5c6
Showing
4 changed files
with
150 additions
and
6 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
51 changes: 51 additions & 0 deletions
51
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,51 @@ | ||
/* | ||
* 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); | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
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,49 @@ | ||
/* | ||
* 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); | ||
} | ||
} | ||
} |