|
|
# Regular Unit Test
|
|
|
# Writing a Unit Test for a 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.
|
|
|
|
|
|
```java
|
|
|
@Test
|
|
|
public void shouldExecutePipelineCorrectly() throws Exception {
|
... | ... | @@ -12,7 +19,14 @@ |
|
|
}
|
|
|
```
|
|
|
|
|
|
# Unit Test with our Stage Testing Framework
|
|
|
## 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](src/main/java/teetime/stage/InitialElementProducer.java) per input port and one [CollectorSink](src/main/java/teetime/stage/CollectorSink.java) per output port of your stage under test.
|
|
|
Just start with the entry method [teetime.framework.test.StageTester.test(..)](src/main/java/teetime/framework/test/StageTester.java) and enjoy the fluent API.
|
|
|
Examples are given below.
|
|
|
|
|
|
### Testing a Usual Stage
|
|
|
|
|
|
```java
|
|
|
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
|
|
|
import static org.hamcrest.core.Is.is;
|
... | ... | @@ -30,11 +44,47 @@ public class CounterTest { |
|
|
|
|
|
@Test
|
|
|
public void counterValueShouldBeCorrect() {
|
|
|
test(counter).and().send(1, 2, -5, 10, 9).to(counter.getInputPort()).start();
|
|
|
List<Integer> results = new ArrayList<Integer>();
|
|
|
|
|
|
test(counter).and()
|
|
|
.send(1, 2, -5, 10, 9).to(counter.getInputPort()).and()
|
|
|
.receive(results).from(counter.getOutputPort()).and()
|
|
|
.start();
|
|
|
|
|
|
assertThat(results, contains(1, 2, -5, 10, 9));
|
|
|
assertThat(counter.getNumElementsPassed(), is(5));
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
### Testing a Producer Stage
|
|
|
```java
|
|
|
@Test
|
|
|
public void testProducer() throws Exception {
|
|
|
InitialElementProducer<Integer> producer = new InitialElementProducer<>(1, 2, 3);
|
|
|
|
|
|
List<Integer> outputElements = new ArrayList<>();
|
|
|
|
|
|
test(producer)
|
|
|
.receive(outputElements).from(producer.getOutputPort())
|
|
|
.start();
|
|
|
|
|
|
assertThat(outputElements, contains(1, 2, 3));
|
|
|
}
|
|
|
```
|
|
|
|
|
|
### Testing a Sink Stage:
|
|
|
```java
|
|
|
@Test
|
|
|
public void testSink() throws Exception {
|
|
|
CollectorSink<Integer> consumer = new CollectorSink<>();
|
|
|
|
|
|
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](https://build.se.informatik.uni-kiel.de/teetime/teetime/tree/master/src/test/java/teetime/stage). |
|
|
\ No newline at end of file |