Editing StreamBase JUnit Tests

Overview

Java test files generated by the StreamBase JUnit wizard are incomplete and must be edited to become a useful test. This topic provides guidelines for completing the edit of your generated StreamBase JUnit test files.

Required Edits

A generated JUnit test file includes:

  • One example tuple using generated test data for one input stream of the module to be tested.

  • A placeholder for an Expecter object that defines a tuple you expect to see on the module's output ports.

You must make the following edits:

  1. Fill in the generated example tuple for the first input stream with actual data. Run your module in Studio to send in one or more tuples, and record the output expected from those test tuples. Then fill in the exact same test tuple data to replace the generated data in the test file.

  2. The wizard generated an example input tuple only for the first input stream of your module. First means the first input stream found in a top-down scan of the XML source of an EventFlow or StreamSQL module. If your module has more than one input stream, and those streams are relevant to the test you want to run, then you must add one or more input tuples for those streams as well.

  3. One Expecter section is generated, showing a getDequeuer() method running on the generic output stream named OutputStream1. Replace OutputStream1 with the name of an actual output stream in the module to be tested.

  4. Fill in the Object array marked "[REPLACE THIS]" with a comma-separated list of the tuple data you expect to see on this output stream, given the input tuple.

  5. If the module to be tested has more than one output stream, add an Expecter section for each output stream.

Tip

When using the JSONTupleMaker class to format tuples for enqueuing and dequeuing, use the Copy as JSON feature in the context menu of the Application Output and Application Input views to quickly generate tuples in the correct format.

Similarly, use the same context menu's Copy as CSV feature with the CSVTupleMaker class.

Using Non-Default Container Names in Tests

The generated JUnit test file includes a loadApp() line like the following example. This line is responsible for loading the specified application module into the test StreamBase Server.

server.loadApp("appname.sbapp");

The line as generated loads the named module into a container named default. In this case, the StreamBase paths to the names of streams in your test code can be simple names without a container name, because StreamBase presumes a container named default if you don't specify a container. Thus, for a test of the Best Bids and Asks sample included with StreamBase, when using the default container, the following test code fragments are valid:

server.getEnqueuer("NYSE_Feed").enqueue( ...
...
new Expecter(server.getDequeuer("BestAsks"))

You may have an application-specific reason to load your test module into a container other than default. To do this in your test code, add a container name as a second argument to the loadApp method, like the following example:

server.loadApp("appname.sbapp", "testcontainer");

In this case, you must make sure that all StreamBase paths in the test file include the container name. For example:

server.getEnqueuer("testcontainer.NYSE_Feed").enqueue( ...
...
new Expecter(server.getDequeuer("testcontainer.BestAsks"))

Using Non-Default Container Names with Deployment Files

The same rule applies if you specify a non-default container name in a deployment file: you must make sure all StreamBase paths in the test file include the container name.

For example, you might generate a test file to run the following simple deployment file, and you select the module in thedeploycontainer container in the Container application field of the New Unit Test Class dialog:

<?xml version="1.0" encoding="UTF-8"?>
<deploy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:noNamespaceSchemaLocation="http://www.streambase.com/schemas/sbdeploy/">
    <runtime>
      <application container="default" module="BestBidsAsks.sbapp"/>
      <application container="deploycontainer" module="BestBidsAlt.sbapp"/>
    </runtime>
</deploy>

In this case, the JUnit wizard automatically places the container name in the generated getEnqueuer() line:

server.getEnqueuer("deploycontainer.NYSE_Feed").enqueue( ...

It is up to you to include the container name when you add further getEnqueuer() and getDequeuer() lines. For example:

...
new Expecter(server.getDequeuer("deploycontainer.BestAsks"))
...
new Expecter(server.getDequeuer("deploycontainer.BestBids"))
...

Further Options

You are not limited to the code generated by the wizard. For example, you might prefer to enqueue your input tuples constructed with ObjectArrayTupleMaker.MAKER instead of JSONSingleQuotesTupleMaker.MAKER.

If you need your test to reset the state of the module under test in preparation for further tests, your test code can include a passage like the following:

stopContainers(); startContainers();

Use the Javadoc documentation for the com.streambase.sb.unittest package as a guide to the test features available. See Java API Documentation.

Back to Top ^