Writing a Unit Test for a Stage
- Regular Unit Test
- Unit Testing with our Stage Testing Framework
- Testing a Usual Stage
- Testing a Stage With Multiple Output Ports
- Testing a Producer Stage
- Testing a Sink Stage
Regular Unit Test
For a regular unit test of a stage, you need to create and execute a custom configuration which contains your stage under test (SUT), at least one producer to feed your SUT, and at least one sink to collect and test the output elements of your SUT. An example is shown below. It does not only include much boilerplate code, but also hides the actual SUT. The test does not name the SUT because it is encapsulated by the custom configuration.
@Test public void shouldExecutePipelineCorrectly() throws Exception { String[] inputElements = { "a", "b", "c" }; GlobalTaskQueueConfig<String> config = new GlobalTaskQueueConfig<>(inputElements); Execution<GlobalTaskQueueConfig<String>> execution = new Execution<>(config); execution.executeBlocking(); String[] expectedElements = { "a", "b", "c" }; assertThat(inputElements, is(equalTo(expectedElements))); }
Unit Testing with our Stage Testing Framework
Instead of building and executing a test configuration by yourself, our testing framework takes over this task. It automatically creates a configuration which contains one InitialElementProducer per input port and one CollectorSink per output port of your stage under test. Just start with the entry method teetime.framework.test.StageTester.test(..) and enjoy the fluent API. Examples are given below.
Testing a Usual Stage
import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; import static teetime.framework.test.StageTester.*; public class CounterTest { private Counter<Integer> counter; @Before public void initializeCounter() { counter = new Counter<Integer>(); } @Test public void counterValueShouldBeCorrect() { test(counter).and() .send(1, 2, -5, 10, 9).to(counter.getInputPort()).and() .start(); assertThat(counter.getOutputPort(), produces(1, 2, -5, 10, 9)); assertThat(counter.getNumElementsPassed(), is(5)); } }
Testing a Stage With Multiple Output Ports
@Before public void before() { filter = InstanceOfFilter<Object, Clazz>(Clazz.class); } @Test public void outputMatchedAndMismatchedElements() { final Clazz clazz = new Clazz(); final Integer number = 42; test(filter).and() .send(clazz, number, clazz).to(filter.getInputPort()) .start(); assertThat(filter.getMatchedOutputPort(), produces(clazz, clazz)); assertThat(filter.getMismatchedOutputPort(), produces(number)); }
Testing a Producer Stage
@Before public void before() { producer = new InitialElementProducer<Integer>(1, 2, 3); } @Test public void testProducer() throws Exception { test(producer).start(); assertThat(producer.getOutputPort(), produces(1, 2, 3)); }
Testing a Sink Stage
@Before public void before() { consumer = new CollectorSink<Integer>(); } @Test public void testSink() throws Exception { test(consumer).and() .send(1, 2, 3).to(consumer.getInputPort()).and() .start(); assertThat(consumer.getElements(), contains(1, 2, 3)); }
More example tests written with our stage testing framework can be found in the source code.