mirror of https://github.com/grpc/grpc-java.git
core: Use real ByteBuffer instead of mock (#9523)
My motivation for making this change is that [`ByteBuffer` is becoming `sealed`](https://download.java.net/java/early_access/loom/docs/api/java.base/java/nio/ByteBuffer.html) in new versions of Java. This makes it impossible for Mockito's _current_ default mockmaker to mock it. That said, Mockito will likely [switch its default mockmaker](https://github.com/mockito/mockito/issues/2589) to an alternative that _is_ able to mock `sealed` classes. However, there are downside to that, such as [slower performance](https://github.com/mockito/mockito/issues/2589#issuecomment-1192725206), so it's probably better to leave our options open by avoiding mocking at all. And in this case, it's equally easy to use real objects. As a bonus, I think that real objects makes the code a little easier to follow: Before, we created mocks that the code under test never interacted with in any way. (The code just passed them through to a delegate.) When I first read the tests, I was confused, since I assumed that the mock we were creating was the same mock that we then passed to `verify` at the end of the method. That turned out not to be the case.
This commit is contained in:
parent
5946eb0d6a
commit
6bafca93a7
|
|
@ -36,13 +36,10 @@ import org.mockito.Mock;
|
||||||
import org.mockito.junit.MockitoJUnit;
|
import org.mockito.junit.MockitoJUnit;
|
||||||
import org.mockito.junit.MockitoRule;
|
import org.mockito.junit.MockitoRule;
|
||||||
|
|
||||||
/**
|
/** Tests for {@link ForwardingReadableBuffer}. */
|
||||||
* Tests for {@link ForwardingReadableBuffer}.
|
|
||||||
*/
|
|
||||||
@RunWith(JUnit4.class)
|
@RunWith(JUnit4.class)
|
||||||
public class ForwardingReadableBufferTest {
|
public class ForwardingReadableBufferTest {
|
||||||
@Rule
|
@Rule public final MockitoRule mocks = MockitoJUnit.rule();
|
||||||
public final MockitoRule mocks = MockitoJUnit.rule();
|
|
||||||
|
|
||||||
@Mock private ReadableBuffer delegate;
|
@Mock private ReadableBuffer delegate;
|
||||||
private ForwardingReadableBuffer buffer;
|
private ForwardingReadableBuffer buffer;
|
||||||
|
|
@ -55,10 +52,7 @@ public class ForwardingReadableBufferTest {
|
||||||
@Test
|
@Test
|
||||||
public void allMethodsForwarded() throws Exception {
|
public void allMethodsForwarded() throws Exception {
|
||||||
ForwardingTestUtil.testMethodsForwarded(
|
ForwardingTestUtil.testMethodsForwarded(
|
||||||
ReadableBuffer.class,
|
ReadableBuffer.class, delegate, buffer, Collections.<Method>emptyList());
|
||||||
delegate,
|
|
||||||
buffer,
|
|
||||||
Collections.<Method>emptyList());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -99,7 +93,7 @@ public class ForwardingReadableBufferTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void readBytes_overload1() {
|
public void readBytes_overload1() {
|
||||||
ByteBuffer dest = mock(ByteBuffer.class);
|
ByteBuffer dest = ByteBuffer.allocate(0);
|
||||||
buffer.readBytes(dest);
|
buffer.readBytes(dest);
|
||||||
|
|
||||||
verify(delegate).readBytes(dest);
|
verify(delegate).readBytes(dest);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue