StreamBase JUnit Test Tutorial

Steps to Generate and Run a JUnit Test

This section steps through the process of generating and running a simple StreamBase JUnit test for the Best Bids and Asks sample delivered with StreamBase.

  1. In StreamBase Studio, load the Best Bids and Asks sample:

    • From the top menu, select FileLoad StreamBase Sample.

    • Select bestbidsandasks from the Applications category.

    • Click OK.

    Studio creates a project folder named sample_bestbidsandasks. If you loaded the sample previously, Studio creates sample_bestbidsandasks1.

  2. If you make edits, make sure the application is free of typecheck errors.

  3. Click the Run button. This opens the SB Test/Debug perspective and runs the application.

  4. Use the Manual Input view to send the following input tuple:

    time_int 20100930
    symbol IBM
    bid_price 125.07
    bid_size 3000
    ask_price 139.45
    ask_size 2000
    sequence 467
  5. Keep a record of the exact input tuple you sent, and of the results you see on output streams in the Application Output view:

  6. Press F9 or click the Stop Running Application button. This returns you to the SB Authoring perspective.

  7. Select the BestBidsAsks.sbapp EventFlow module in the Project Explorer view. Right-click, and select NewStreamBase Unit Test (JUnit) from the context menu.

  8. Fill in the fields of the New StreamBase Unit Test Class dialog:

    Source folder The sample_bestbidsandasks/java-src folder is entered for you.
    Package Enter com.example.sbjunit, or use a different Java package name that conforms to your site's standards.
    Name The name BestBidsAsksTest is entered for you. BBA_test1. This serves as the name for your Java test class and the Java source file to be created.
    Application under test The BestBidsAsks.sbapp file name is entered for you. (If not, use Browse to select it.)
    Test Method Body All tuples from the Application Input/Output views.
    Tuple Maker Class JSONSingleQuotesTupleMaker.
  9. If the New StreamBase Unit Test Class dialog shows a warning that the Test Support library is not on the project's Java build path, then select the Click here link.

  10. This opens the Properties dialog for the current project, with the Libraries tab of the Java Build Path panel already open.

    • Hold the Ctrl key and select both JUnit4 and StreamBase Test Support.

    • Click OK.

    • This adds the two libraries to the project's build path, then returns you to the New StreamBase Unit Test Class dialog.

    (Alternative: if you select only JUnit4, StreamBase automatically includes the required StreamBase Client and Test Support libraries.)

  11. Click Finish. Studio generates the JUnit test file named BestBidsAsksTest.java, and opens the file in Studio.

  12. Inspect the BestBidsAsksTest.java test file. Notice that:

    • The wizard inspected the specified EventFlow file and found one input stream. It generated one example tuple for this input stream, using generated values that match the data type of each field in the input stream's schema.

    • The wizard inspected the specified EventFlow file and found two output streams. It generated two Expecter objects for the module's output streams.

  13. Save the BestBidsAsksTest.java test file, and click the Run button in the Studio toolbar.

  14. The first time the test is run, Studio prompts you to specify whether this is a standard JUnit test for Java code, or a StreamBase JUnit test for a StreamBase module. Select StreamBase Unit Test and click OK.

  15. Studio runs the test file. The test starts StreamBase Server, loads the BestBidsAsks.sbapp module, sends the specified tuple to input stream NYSE_Feed, and compares the output emitted from output stream BestAsks to the tuple you specified for the Expecter object.

    The JUnit view is brought to the foreground in the bottom pane. Look for the long green bar that indicates that the test passed.

  16. Edit the test file, introducing a deliberate error. Change the 125.07 value to 124.07:

        new Object[] { 20100930, "IBM", 124.07 });
  17. Save and re-run the test. This time, Studio reports an error and shows the expected and received values:

  18. Click the Compare Actual button (circled in red in the image above) to hone in on the exact failure:

  19. Edit the Java test file again and correct the error.

Debugging StreamBase JUnit Tests

You can set breakpoints on both JUnit test code and EventFlow arcs for a module under test. Debugging the JUnit test then automatically switches between Java debugging and EventFlow debugging of the module under test.

To illustrate this feature, set two Java breakpoints in the BestBidsAsks.java file created in the previous section:

  • Set a breakpoint on the line that assigns a value to the inputTupleAsJSONString string around line 50.

  • Set another breakpoint on the first Expecter expecter line.

In the BestBidsAsks.sbapp EventFlow file, set an EventFlow breakpoint on the arc exiting the input stream NYSE_Feed.

Now, follow these steps:

  1. Switch back to the BestBidsAsks Java file and run it using the Debug button ()instead of the Run button.

  2. This opens the Eclipse Debug perspective. Execution pauses at the first Java breakpoint.

  3. In the Debug view, click the Continue button () or press F8. This opens the EventFlow Editor canvas and pauses execution at the first EventFlow breakpoint.

  4. Press the Continue button again, or optionally step through the EventFlow module. In either case, execution returns to the Java file and pauses on the second Java breakpoint.

Not all features of Java debugging are implemented when debugging StreamBase JUnit test code. For example, those familiar with Java debugging in Eclipse might expect the following sequence to work:

  • When paused at the first Java breakpoint, click the Step Into (F5) button, which stops on the enqueue() line.

  • Click Step Return (F7) to return to the first breakpoint.

  • Click Step Into again to go back into the enqueue() implementation.

Instead, pressing Step Return takes you to the EventFlow file.

Back to Top ^