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

Fix implementation of proxy mock maker for toString and add additional unit tests. #2405

Merged
merged 1 commit into from Aug 24, 2021
Merged
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 @@ -107,7 +107,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
case "equals":
return proxy == args[0];
case "toString":
return "";
break;
default:
throw new MockitoException(
join(
Expand Down
Expand Up @@ -8,6 +8,7 @@
import org.mockito.internal.creation.AbstractMockMakerTest;

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -47,5 +48,54 @@ public void should_discover_mockable_input() {
assertThat(mockMaker.isTypeMockable(SomeInterface.class).mockable()).isTrue();
}

@Test
public void can_compute_hash_code() throws Throwable {
SomeInterface proxy =
mockMaker.createMock(settingsFor(SomeInterface.class), dummyHandler());

InvocationHandler handler = Proxy.getInvocationHandler(proxy);

assertThat(handler.invoke(proxy, Object.class.getMethod("hashCode"), new Object[0]))
.isEqualTo(System.identityHashCode(proxy));
}

@Test
public void can_compute_equality() throws Throwable {
SomeInterface proxy =
mockMaker.createMock(settingsFor(SomeInterface.class), dummyHandler());

InvocationHandler handler = Proxy.getInvocationHandler(proxy);

assertThat(
handler.invoke(
proxy,
Object.class.getMethod("equals", Object.class),
new Object[] {proxy}))
.isEqualTo(true);
assertThat(
handler.invoke(
proxy,
Object.class.getMethod("equals", Object.class),
new Object[] {null}))
.isEqualTo(false);
assertThat(
handler.invoke(
proxy,
Object.class.getMethod("equals", Object.class),
new Object[] {new Object()}))
.isEqualTo(false);
}

@Test
public void can_invoke_toString() throws Throwable {
SomeInterface proxy =
mockMaker.createMock(settingsFor(SomeInterface.class), dummyHandler());

InvocationHandler handler = Proxy.getInvocationHandler(proxy);

assertThat(handler.invoke(proxy, Object.class.getMethod("toString"), new Object[0]))
.isNull();
}

interface SomeInterface {}
}
Expand Up @@ -5,8 +5,8 @@
package org.mockito.kotlin

import kotlinx.coroutines.runBlocking
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.core.IsEqual
import org.junit.Assert.assertThat
import org.junit.Test
import org.mockito.Mockito.*

Expand Down