Replace TestInputRowHandler with mocking object (#11529)

* Replace TestInputRowHandler with mocking object

* Change EasyMock object to Mockito object. Make test logic concise

* correct code format
This commit is contained in:
wx930910 2021-08-04 19:45:22 -04:00 committed by GitHub
parent 361bfdcaa5
commit 578625b771
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 38 deletions

View File

@ -19,22 +19,28 @@
package org.apache.druid.data.input;
import org.apache.druid.data.input.HandlingInputRowIterator.InputRowHandler;
import org.apache.druid.java.util.common.CloseableIterators;
import org.apache.druid.java.util.common.parsers.CloseableIterator;
import org.easymock.EasyMock;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(Enclosed.class)
public class HandlingInputRowIteratorTest
{
@ -55,8 +61,7 @@ public class HandlingInputRowIteratorTest
{
throw new NoSuchElementException();
}
}
);
});
private HandlingInputRowIterator target;
@ -81,18 +86,25 @@ public class HandlingInputRowIteratorTest
public static class PresentRowTest
{
private static final InputRow INPUT_ROW1 = EasyMock.mock(InputRow.class);
private static final InputRow INPUT_ROW2 = EasyMock.mock(InputRow.class);
private static final InputRow INPUT_ROW1 = mock(InputRow.class);
private static final InputRow INPUT_ROW2 = mock(InputRow.class);
private static final List<InputRow> INPUT_ROWS = Arrays.asList(INPUT_ROW1, INPUT_ROW2);
private TestInputRowHandler successfulHandler;
private TestInputRowHandler unsuccessfulHandler;
private InputRowHandler successfulHandler;
private InputRowHandler unsuccessfulHandler;
@Before
public void setup()
{
successfulHandler = new TestInputRowHandler(true);
unsuccessfulHandler = new TestInputRowHandler(false);
// Construct mock object
successfulHandler = mock(HandlingInputRowIterator.InputRowHandler.class);
// Method Stubs
when(successfulHandler.handle(any(InputRow.class))).thenReturn(true);
// Construct mock object
unsuccessfulHandler = mock(HandlingInputRowIterator.InputRowHandler.class);
// Method Stubs
when(unsuccessfulHandler.handle(any(InputRow.class))).thenReturn(false);
}
@Test
@ -100,7 +112,7 @@ public class HandlingInputRowIteratorTest
{
HandlingInputRowIterator target = createInputRowIterator(unsuccessfulHandler, unsuccessfulHandler);
Assert.assertTrue(target.hasNext());
Assert.assertFalse(unsuccessfulHandler.invoked);
Mockito.verify(unsuccessfulHandler, Mockito.never()).handle(INPUT_ROW1);
}
@Test
@ -108,7 +120,7 @@ public class HandlingInputRowIteratorTest
{
HandlingInputRowIterator target = createInputRowIterator(unsuccessfulHandler, unsuccessfulHandler);
Assert.assertEquals(INPUT_ROW1, target.next());
Assert.assertTrue(unsuccessfulHandler.invoked);
Mockito.verify(unsuccessfulHandler, Mockito.times(2)).handle(INPUT_ROW1);
}
@Test
@ -116,8 +128,8 @@ public class HandlingInputRowIteratorTest
{
HandlingInputRowIterator target = createInputRowIterator(successfulHandler, unsuccessfulHandler);
Assert.assertNull(target.next());
Assert.assertTrue(successfulHandler.invoked);
Assert.assertFalse(unsuccessfulHandler.invoked);
Mockito.verify(successfulHandler, Mockito.times(1)).handle(INPUT_ROW1);
Mockito.verify(unsuccessfulHandler, Mockito.never()).handle(INPUT_ROW1);
}
@Test
@ -125,14 +137,13 @@ public class HandlingInputRowIteratorTest
{
HandlingInputRowIterator target = createInputRowIterator(unsuccessfulHandler, successfulHandler);
Assert.assertNull(target.next());
Assert.assertTrue(unsuccessfulHandler.invoked);
Assert.assertTrue(successfulHandler.invoked);
Mockito.verify(unsuccessfulHandler, Mockito.times(1)).handle(INPUT_ROW1);
Mockito.verify(successfulHandler, Mockito.times(1)).handle(INPUT_ROW1);
}
private static HandlingInputRowIterator createInputRowIterator(
HandlingInputRowIterator.InputRowHandler firstHandler,
HandlingInputRowIterator.InputRowHandler secondHandler
)
HandlingInputRowIterator.InputRowHandler secondHandler)
{
CloseableIterator<InputRow> iterator = CloseableIterators.withEmptyBaggage(
new Iterator<InputRow>()
@ -151,29 +162,9 @@ public class HandlingInputRowIteratorTest
{
return delegate.next();
}
}
);
});
return new HandlingInputRowIterator(iterator, Arrays.asList(firstHandler, secondHandler));
}
private static class TestInputRowHandler implements HandlingInputRowIterator.InputRowHandler
{
boolean invoked = false;
private final boolean successful;
TestInputRowHandler(boolean successful)
{
this.successful = successful;
}
@Override
public boolean handle(InputRow inputRow)
{
invoked = true;
return successful;
}
}
}
}