From 415d6630dad45d97b134334f2d4e46535d39fc16 Mon Sep 17 00:00:00 2001 From: Xiao Date: Sun, 5 Sep 2021 21:45:32 -0400 Subject: [PATCH] Replace SinkHandler by mocking object and improve test logic --- .../qpid/proton/reactor/ReactorTest.java | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/proton-j/src/test/java/org/apache/qpid/proton/reactor/ReactorTest.java b/proton-j/src/test/java/org/apache/qpid/proton/reactor/ReactorTest.java index fde9219bb..34f1809ec 100644 --- a/proton-j/src/test/java/org/apache/qpid/proton/reactor/ReactorTest.java +++ b/proton-j/src/test/java/org/apache/qpid/proton/reactor/ReactorTest.java @@ -55,6 +55,9 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; +import org.mockito.Mockito; +import org.mockito.ArgumentCaptor; +import static org.mockito.Mockito.*; @RunWith(Parameterized.class) public class ReactorTest { @@ -385,19 +388,6 @@ public void checkNoVhost() throws IOException { } - private static class SinkHandler extends BaseHandler { - protected int received = 0; - - @Override - public void onDelivery(Event event) { - Delivery dlv = event.getDelivery(); - if (!dlv.isPartial()) { - dlv.settle(); - ++received; - } - } - } - private static class SourceHandler extends BaseHandler { private int remaining; @@ -446,7 +436,20 @@ private void transfer(int count, int window) throws IOException { // XXX: a window of 1 doesn't work unless the flowcontroller is // added after the thing that settles the delivery sh.add(new FlowController(window)); - SinkHandler snk = new SinkHandler(); + // Create variables for tracking behaviors of mock object + int[] snkReceived = new int[] { 0 }; + // Construct mock object + BaseHandler snk = spy(BaseHandler.class); + // Method Stubs + doAnswer((stubInvo) -> { + Event event = stubInvo.getArgument(0); + Delivery dlv = event.getDelivery(); + if (!dlv.isPartial()) { + dlv.settle(); + ++snkReceived[0]; + } + return null; + }).when(snk).onDelivery(any(Event.class)); sh.add(snk); SourceHandler src = new SourceHandler(count); @@ -454,7 +457,7 @@ private void transfer(int count, int window) throws IOException { src); reactor.run(); reactor.free(); - assertEquals("Did not receive the expected number of messages", count, snk.received); + assertEquals("Did not receive the expected number of messages", count, snkReceived[0]); checkForLeaks(); }