Example BDL Script for JUnit 4.x (legacy)

Script Example

The following sample JUnit test script contains several annotations for test setup and teardown and is generated by importing a sample JUnit 4.x test case and selecting the methods doFoo and doFoo2.

Note: The following script is a legacy script, which is generated within older Silk Performer versions. For Silk Performer 19.5 and newer, a JUnit import creates a slightly different script, which is documented in Example BDL Script for JUnit .
var
  ghTestObj :number;
dcluser
  user
    JavaUser
  transactions
    TInit :begin;
    TMain :1;
    TEnd :end;

dclfunc
  function JUnit4Before
  begin
    // @Before 
    JavaCallMethod(ghTestObj, "myBefore", "myBefore");
  end JUnit4Before;

  function JUnit4After
  begin
    // @After    
    JavaCallMethod(ghTestObj, "myAfter", "myAfter");
  end JUnit4After;

  function JUnit4CallFunc(hJavaObj :number; sName :string; sTimerName :string optional; sExceptionBuffer :string optional) : boolean
  begin 
    JUnit4Before();
    // @Test 
    JUnit4CallFunc := JavaCallMethod(hJavaObj, sName, sTimerName);
    if not StrIsNull(sExceptionBuffer) then 
      JavaGetLastException(hJavaObj, sExceptionBuffer);
    end;
    JUnit4After();
  end JUnit4CallFunc;

dcltrans
  transaction TInit
  var
    hPerf : number;
  begin
    JavaCreateJavaVM();
    ghTestObj := JavaLoadObject("JU4ImporterTest", "JU4ImporterTest.<init>");
    // @BeforeClass 
    JavaCallMethod(JAVA_STATIC_METHOD, "JU4ImporterTest.myBeforeClass", "myBeforeClass");
  end TInit;

  transaction TMain
  var
    sBuffer :string;
  begin
    JUnit4CallFunc(ghTestObj, "doFoo", "doFoo");
    JUnit4CallFunc(ghTestObj, "doFoo2", "doFoo2");
  end TMain;

  transaction TEnd
  var
    sBuffer :string;
  begin
    // @AfterClass
    JavaCallMethod(JAVA_STATIC_METHOD, "JU4ImporterTest.myAfterClass", "myAfterClass");
    JavaFreeObject(ghTestObj);
  end TEnd;

In the dclfunc section, the helper functions for test setup, teardown, and exception handling are defined. These functions are used in the transactions.

JavaLoadObject in the TInit transaction instantiates the JUnit test class JU4ImporterTest. All JUnit methods that use the @BeforeClass annotation are called in the TInit transaction.

JUnit4CallFunc in the TMain transaction calls all the test methods that were selected for the JUnit test import. First, the methods that use the @Before annotation are invoked, and then the test method itself, such as doFoo(), is invoked. Finally, the methods that use the @After annotation are invoked.

Timers

When an optional timer parameter is specified for a Java method call, the execution times of the constructor, test method, setup method, and teardown method are measured. For the example in this topic, you receive the following measures:

  • Constructor – JU4ImporterTest.<init>
  • For the methods – doFoo, doFoo2
  • For the test setup – myBeforeClass, myBefore
  • For the test teardown – myAfterClass, myAfter