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.
-
In StreamBase Studio, load the Best Bids and Asks sample:
-
From the top menu, select → .
-
Select
bestbidsandasksfrom the Applications category. -
Click OK.
Studio creates a project folder named
sample_bestbidsandasks. If you loaded the sample previously, Studio createssample_bestbidsandasks1. -
-
If you make edits, make sure the application is free of typecheck errors.
-
Click the
Run button. This opens the SB
Test/Debug perspective and runs the application.
-
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 -
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:
-
In the Application Output view, right-click and disable Show time column.
-
Select the BestAsks line in the view, right-click, and select Copy as CSV. Paste this lines into a text editor and save it.
-
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.
-
-
Press F9 or click the
Stop Running Application button. This returns you
to the SB Authoring perspective.
-
Select the
BestBidsAsks.sbappEventFlow module in the Package Explorer view. Right-click, and select → from the context menu. -
Fill in the fields of the New StreamBase Unit Test Class dialog:
Source folder The sample_bestbidsandasks/java-srcfolder 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 file name is entered for you. (If not, use to select it.)BestBidsAsks.sbapp -
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.
-
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 .
-
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.)
-
-
Click . Studio generates the JUnit test file named
BBA_test1.java, and opens the file in Studio. -
Inspect the
BBA_test1.javatest 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
Expecterobject for either of the module's output streams. Instead, it generated a genericExpecterfor an output stream with default nameOutputStream1, and a"REPLACE THIS"reminder that you must name your output streams and provide the expected output tuple for each.
-
-
In the
BBA_test1.javatest file, replace the generated values in theinputTupleAsJSONStringstring 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.
-
In the
BBA_test1.javatest file, locate the line that begins withExpecter. ReplaceOutputStream1in this line with the name of the application's first output stream,BestAsks. -
Replace the
REPLACE THISstring for theObject[]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. -
Save the
BBA_test1.javatest file, and click the
Run button in the Studio toolbar.
-
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 .
-
Studio runs the test file. The test starts StreamBase Server, loads the
BestBidsAsks.sbappmodule, sends the specified tuple to input streamNYSE_Feed, and compares the output emitted from output streamBestAsksto the tuple you specified for theExpecterobject.The JUnit view is brought to the foreground in the bottom pane. Look for the long green bar that indicates that the test passed.
-
Continue editing the
BBA_test1.javatest file. Add anotherExpecterfor 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 }); -
Save and re-run the test file. Studio reports another success.
-
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 }); -
Save and re-run the test. This time, Studio reports an error and shows the expected and received values:
-
Click the Compare Actual button (circled in red in the image above) to hone in on the exact failure:
-
Edit the Java test file again and correct the error.
-
Now, run the same JUnit test from the command prompt with these steps:
-
Create a server configuration file for the project with → → → → . Do not select the Populate with ... option. Name the file
sbd.sbconf. -
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> -
Open a UNIX terminal window or a StreamBase command prompt, and navigate to the project folder
sample_bestbidsandasksin your Studio workspace.Tip
On Windows, select the project folder in the Package Explorer view. Right-click, then select → from the context menu.
-
Run the test with the following command:
sbunit -f sbd.sbconf com.example.sbjunit.BBA_test1
-
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)
-
-
Continue to experiment with different settings in the Java test file. Use the Javadoc documentation for the
com.streambase.sb.unittestpackage as a guide to the test features available. See Java API Documentation.
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
inputTupleAsJSONStringstring around line 50. -
Set another breakpoint on the first
Expecter expecterline.
In the BestBidsAsks.sbapp EventFlow file, set an
EventFlow breakpoint on the arc exiting the input stream NYSE_Feed.
Now, follow these steps:
-
Switch back to the
BBA_test1Java file and run it using the Debug button (
)instead of the Run button.
-
This opens the Eclipse Debug perspective. Execution pauses at the first Java breakpoint.
-
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.
-
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.
