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