Skip to content
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

Restored spy equals/hashcode fix #2583

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -44,6 +44,7 @@
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.attribute.MethodAttributeAppender;
import net.bytebuddy.matcher.ElementMatcher;
import org.mockito.Answers;
import org.mockito.codegen.InjectionBase;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.creation.bytebuddy.ByteBuddyCrossClassLoaderSerializationSupport.CrossClassLoaderSerializableMock;
Expand Down Expand Up @@ -240,11 +241,21 @@ public <T> Class<? extends T> mockClass(MockFeatures<T> features) {
.serialVersionUid(42L)
.defineField("mockitoInterceptor", MockMethodInterceptor.class, PRIVATE)
.implement(MockAccess.class)
.intercept(FieldAccessor.ofBeanProperty())
.method(isHashCode())
.intercept(hashCode)
.method(isEquals())
.intercept(equals);
.intercept(FieldAccessor.ofBeanProperty());

if (features.defaultAnswer != Answers.CALLS_REAL_METHODS) {
builder =
builder.method(isHashCode())
.intercept(hashCode)
.method(isEquals())
.intercept(equals);
} else {
builder =
builder.method(isHashCode())
.intercept(dispatcher)
.method(isEquals())
.intercept(dispatcher);
}
if (features.serializableMode == SerializableMode.ACROSS_CLASSLOADERS) {
builder =
builder.implement(CrossClassLoaderSerializableMock.class)
Expand Down
Expand Up @@ -5,12 +5,14 @@
package org.mockito.internal.creation.bytebuddy;

import java.lang.ref.ReferenceQueue;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import net.bytebuddy.TypeCache;
import org.mockito.mock.SerializableMode;
import org.mockito.stubbing.Answer;

class TypeCachingBytecodeGenerator extends ReferenceQueue<ClassLoader>
implements BytecodeGenerator {
Expand Down Expand Up @@ -43,7 +45,8 @@ public <T> Class<T> mockClass(final MockFeatures<T> params) {
params.mockedType,
params.interfaces,
params.serializableMode,
params.stripAnnotations),
params.stripAnnotations,
params.defaultAnswer),
() -> bytecodeGenerator.mockClass(params),
BOOTSTRAP_LOCK);
} catch (IllegalArgumentException exception) {
Expand Down Expand Up @@ -83,15 +86,18 @@ private static class MockitoMockKey extends TypeCache.SimpleKey {

private final SerializableMode serializableMode;
private final boolean stripAnnotations;
private final Answer defaultAnswer;

private MockitoMockKey(
Class<?> type,
Set<Class<?>> additionalType,
SerializableMode serializableMode,
boolean stripAnnotations) {
boolean stripAnnotations,
Answer defaultAnswer) {
super(type, additionalType);
this.serializableMode = serializableMode;
this.stripAnnotations = stripAnnotations;
this.defaultAnswer = defaultAnswer;
}

@Override
Expand All @@ -107,14 +113,16 @@ public boolean equals(Object object) {
}
MockitoMockKey that = (MockitoMockKey) object;
return stripAnnotations == that.stripAnnotations
&& serializableMode.equals(that.serializableMode);
&& serializableMode.equals(that.serializableMode)
&& Objects.equals(defaultAnswer, that.defaultAnswer);
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (stripAnnotations ? 1 : 0);
result = 31 * result + serializableMode.hashCode();
result = 31 * result + Objects.hashCode(defaultAnswer);
return result;
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/mockitousage/spies/SpyingOnRealObjectsTest.java
Expand Up @@ -9,6 +9,7 @@
import static org.junit.Assume.assumeTrue;
import static org.mockito.Mockito.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -192,4 +193,13 @@ public void shouldSayNiceMessageWhenSpyingOnPrivateClass() throws Exception {
"Most likely it is due to mocking a private class that is not visible to Mockito");
}
}

@Test
public void spysHashCodeEqualsDelegatedToActualMethods() {
List<String> real = new ArrayList<>();
real.add("one");
List<String> spy = spy(real);
assertEquals(real.hashCode(), spy.hashCode());
assertTrue(spy.equals(real));
}
}