Skip to content

Commit

Permalink
Merge pull request #2405 from mockito/fix-proxy-mock-maker
Browse files Browse the repository at this point in the history
Fix implementation of proxy mock maker for toString and add additional unit tests.
  • Loading branch information
raphw authored Aug 24, 2021
2 parents 03ae33a + cb5e3a6 commit 2576950
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 {}
}
Original file line number Diff line number Diff line change
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

0 comments on commit 2576950

Please sign in to comment.