Adding Unit Test Suite for the BusinessEvents Project

TIBCO BusinessEvents Studio provides a wizard to create a unit test suite for the project using new wizard dialog.

    Procedure
  1. In BusinessEvents Studio explorer, select the BusinessEvents project for which you want to create the BEUnit test suite and in the menu bar, select File > New > Other.
  2. In the wizard, select TIBCO BusinessEvents > BEUnit Test Suite and click Next.
    The New BusinessEvents Unit Test Wizard is displayed.
  3. In the New BusinessEvents Unit Test Wizard, enter the values of the following fields and click Next.
    Field Description
    Parent Folder Select the Java source folder of the project as the parent folder in the project.
    Note: The test suite should be placed in an existing Java source folder. If the parent folder is not a Java source folder then after test suite creation, add the folder as Java source folder.
    File Name Name of the unit test suite Java file.
    EAR File The EAR archive for the selected project. The path can be a relative path as well.
    CDD File Select the CDD file of the project. This populates the Processing Unit and Agent Name options. The path can be a relative path as well.
    Processing Unit Processing unit for the project.
    TRA File (Optional) Filepath to the TRA file of the project. The path can be a relative path as well.
    Agent Name (Optional) Agent name for the selected CDD.

    If there are multiple agents, then the recommendation is to provide agent names separated by commas.

    Description (Optional) Short description for the test suite.
    Concept TestData (Optional) Select the concept test data for the project.
    Event TestData (Optional) Select the event test data for the project.

    If the project does not have JUnit library in its classpath, the wizard displays a note in the bottom. Click the "here" link in the note to add the JUnit library automatically to the project. See the following figure for a sample screen of the wizard.

    New BusinessEvents Unit Test Wizard

  4. In the next page of the wizard, select the test that you want the unit test suite to perform and click Finish.

    The test suite Java file with name specified in File Name is created with code required for the test suite. Edit the file according to your requirement.
  5. If you are using Legacy ActiveSpaces, then you have to enter the environment variable in the JUnit test Run Configurations in BusinessEvents Studio to identify the class definitions based on your operating system.
    1. In BusinessEvents Studio, click Run > Run Configurations.
    2. In the Run Configurations window, right-click JUnit and select New.
    3. Select the Environment tab, and click New.
    4. Now, based on your platform, enter the name and value of the environment variable and click OK.
      • For Windows, add PATH environment variable.
      • For Linux, add LD_LIBRARY_PATH environment variable.
      • For Mac OSX, add DYLD_LIBRARY_PATH environment variable.
      You can also copy the environment variable value from the launch configuration of the BusinessEvents application. See Adding and Working with Launch (Debug or Run) Configurations for more details.
      If you use Apache Maven for build and deployment of the BusinessEvents application with Legacy ActiveSpaces, perform similar steps for setting environment variables in Maven build Run Configuration for JUnit test suite to work in Maven.

Sample Unit Test Suite File

The following is a sample unit test suite .java file generated for the CreditCardApplication project to test if a particular event has been asserted.

Copy

Sample Unit Test Code

package;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.List;
import com.tibco.cep.runtime.model.event.SimpleEvent;
import com.tibco.cep.runtime.service.tester.beunit.BETestEngine;
import com.tibco.cep.runtime.service.tester.beunit.Expecter;
import com.tibco.cep.runtime.service.tester.beunit.TestDataHelper;
import com.tibco.cep.runtime.session.RuleServiceProvider;

/**
 * @description 
 */
public class BEUnitTestSuite2 {
    private static BETestEngine engine;
    private static TestDataHelper helper;
    private static Expecter expecter;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        engine = new BETestEngine("C:/tibco/BE54v95/be/5.4/examples/standard/WebStudio/CreditCardApplication.ear", "C:/tibco/BE54v95/be/5.4/bin/be-engine.tra", "Deployments/CreditCardApplication.cdd", "default", "inference-class", true);

        // Start the test engine
        engine.start();
        
        // Create a helper to work with test data
        helper = new TestDataHelper(engine);
        
        // Create an Expecter object to test rule execution, modifications, assertions, etc.
        expecter = new Expecter(engine);
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        try {
            engine.shutdown();
        } catch (Exception localException) {
        }
    }

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }
    
    /**
    * Test whether a particular Event was asserted by the engine during rule execution
    */
    @Test
    public void testEventAsserted() throws Exception {
        engine.resetSession(); // (optional) reset the rule session, which will clear working memory, restart timers, and clear the data from any previous tests

        // TODO : Change test data path here to create events to be asserted from a test data file
        List<SimpleEvent> events = helper.createEventsFromTestData("/TestData/<test data file name>");
        if (events.size() > 0) {
            engine.assertEvent(events.get(0), false);
        }
        engine.executeRules()
        expecter.expectEventAsserted("/Events/<event name>");
    }
    
}
What to do next

Right-click the new BEUnit test suite .java file and select Run as > JUnit Test to execute the test suite.