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:

    Tip

    To save typing time later, perform the following steps:

    1. In the Application Output view, right-click and disable Show time column.

    2. Select the BestAsks line in the view, right-click, and select Copy as CSV. Paste this lines into a text editor and save it.

    3. Switch to the Application Input view. Select the single input line, right-click and select Copy as JSON. Paste the results in the text editor session.

    You will use these CSV and JSON-encoded versions of the input and output later in your StreamBase JUnit code.

  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 Package 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 Enter 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.)
  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 BBA_test1.java, and opens the file in Studio.

  12. Inspect the BBA_test1.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 did not generate an Expecter object for either of the module's output streams. Instead, it generated a generic Expecter for an output stream with default name OutputStream1, and a "REPLACE THIS" reminder that you must name your output streams and provide the expected output tuple for each.

  13. In the BBA_test1.java test file, replace the generated values in the inputTupleAsJSONString string with the values you noted in step 5. The following example shows the string broken into three pieces for publication clarity. It is usually simpler to type one long JSON string , with fields separated by commas, and field names single-quoted:

    String inputTupleAsJSONString = "{'bid_price':125.00,'time_int':20100930," +
        "'ask_price':139.45,'symbol':'IBM','bid_size':3000," +
        "'sequence':467,'ask_size':2000}";
    

    Tip

    If you used the Copy as JSON feature in step 5, while the copied input strings is still in your text editor, convert all double quotes to single quotes. Then copy the JSON string from the Application Input view to your Java code, carefully copying the entire brace-delimited line and placing it between double quotes in the JUnit code.

  14. In the BBA_test1.java test file, locate the line that begins with Expecter. Replace OutputStream1 in this line with the name of the application's first output stream, BestAsks.

  15. Replace the REPLACE THIS string for the Object[] with a comma-separated list of the fields you recorded in step 5:

    Expecter expecter = new Expecter(server.getDequeuer("BestAsks"));
    expecter.expect(ObjectArrayTupleMaker.MAKER,
        new Object[] { 20100930, "IBM", 139.45 });
    

    Tip

    If you used the Copy as CSV feature in step 5, in the copied line, put double quotes around IBM. Now copy that line from your text editor to the JUnit code.

  16. Save the BBA_test1.java test file, and click the Run button in the Studio toolbar.

  17. 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.

  18. 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.

  19. Continue editing the BBA_test1.java test file. Add another Expecter for the application's second output stream:

    ...
        new Object[] { 20100930, "IBM", 139.45 });
    Expecter expecter2 = new Expecter(server.getDequeuer("BestBids"));
    expecter2.expect(ObjectArrayTupleMaker.MAKER,
        new Object[] { 20100930, "IBM", 125.07 });
    
  20. Save and re-run the test file. Studio reports another success.

  21. Edit the test file again, this time introducing a deliberate error. Change the 125.07 value to 124.07:

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

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

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

  25. Now, run the same JUnit test from the command prompt with these steps:

    1. Create a server configuration file for the project with FileNewOtherStreamBaseServer Configuration File. Do not select the Populate with ... option. Name the file sbd.sbconf.

    2. Edit the file to have the following bare minimum configuration:

      <?xml version="1.0" encoding="UTF-8"?>
      <streambase-configuration xmlns:xi="http://www.w3.org/2001/XInclude"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="http://www.streambase.com/schemas/sbconf/">
        <java-vm>
          <dir path="./java-bin"/>
        </java-vm>
      </streambase-configuration>
      
    3. Open a UNIX terminal window or a StreamBase command prompt, and navigate to the project folder sample_bestbidsandasks in your Studio workspace.

      Tip

      On Windows, select the project folder in the Package Explorer view. Right-click, then select StreamBaseOpen StreamBase Command Prompt Here from the context menu.

    4. Run the test with the following command:

      sbunit -f sbd.sbconf com.example.sbjunit.BBA_test1
      
    5. Look for output like the following that shows success:

      StreamBase unit test runner invoking JUnit...
      JUnit version 4.7
      .
      Time: 2.131
      
      OK (1 test)
      
  26. Continue to experiment with different settings in the Java test file. Use the Javadoc documentation for the com.streambase.sb.unittest package as a guide to the test features available. See Java API Documentation.

Debugging StreamBase JUnit Tests

Starting with release 7.2.2, 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 BBA_test1.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 BBA_test1 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 ^