mirror of https://github.com/apache/druid.git
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:
parent
361bfdcaa5
commit
578625b771
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue