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.

Note: The Object Management type of the project should be set to InMemory as the BEUnit Test Suite runs in the API mode.

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

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.
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", RuleServiceProvider.MODE_API);

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