The OFBiz Unit Test (Using JUnit)
How to define a unit test?
Define test-suite in ofbiz-component.xml like this:
<test-suite loader="main" location="testdef/servicetests.xml"/>
Create test-case in test-suite file.
<test-suite suite-name="servicetests"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/test-suite.xsd">
....
....
<test-case case-name="test-case-name">
...
</test-case>
....
....
</test-suite>
How to create a test case?
Define a test-case in an XML test-suite file like this:
<test-case case-name="test-case-name">
...
</test-case>
The test-case tag contains details of each test type:
1. Entity XML
Specific entity xml and its action would like to be tested in entity-xml-url attribute and action attribute respectively like this:
<test-case case-name="service-dead-lock-retry-assert-data">
<entity-xml action="assert" entity-xml-url="component://service/testdef/data/ServiceDeadLockRetryAssertData.xml"/>
</test-case>
2. JUnit
Specific class's name which will be tested, in a class-name attribute like this:
<test-case case-name="service-tests">
<junit-test-suite class-name="org.apache.ofbiz.service.test.ServiceEngineTests"/>
</test-case>
3. Service
Specific service's name which will be tested in a service-name attribute like this:
<test-case case-name="service-lock-wait-timeout-retry-test">
<service-test service-name="testServiceLockWaitTimeoutRetry"/>
</test-case>
4. Simple Method
Specific simple method's location and name which will be tested in a location and a name attribute respectively like this:
<test-case case-name="auto-accounting-transaction-tests-PoReceipt">
<simple-method-test location="component://accounting/minilang/test/AutoAcctgTransTests.xml" name="testAcctgTransOnPoReceipts"/>
</test-case>
How to run a unit test?
You can run unit test by run 'ant' with following target:
1. run-tests
Run OFBiz default tests.
2. run-test-list
Run all configured tests, stopping/starting ofbiz between each test.
3. run-single-test
Run a single test, require two arguments:
'test.component' is a name of test component.
'test.case' is a name of test case.
4. run-single-test-suite
Run a single test suite, requiring two arguments:
'test.component' is a name of test component.
'test.suiteName' is a name of test suite.
Possible error messages
Some error messages in the log are not important and often caused by the test.
1. Roll back
Roll back error message occurred when transaction roll back data. This error message will be in between starting and finished test cass line like this:
[java] 2009-12-22 16:05:28,349 (main) [ TestRunContainer.java:238:INFO ] [JUNIT] : [test case's name] starting...
[java] 2009-12-22 16:05:28,355 (main) [ TransactionUtil.java:336:ERROR]
....
....
....
[java] ---- exception report ----------------------------------------------------------
[java] [TransactionUtil.rollback]
[java] Exception: java.lang.Exception
[java] Message: Stack Trace
[java] ---- stack trace ---------------------------------------------------------------
[java] java.lang.Exception: Stack Trace
[java] org.apache.ofbiz.entity.transaction.TransactionUtil.rollback(TransactionUtil.java:335)
[java] org.apache.ofbiz.entity.transaction.TransactionUtil.rollback(TransactionUtil.java:317)
[java] org.apache.ofbiz.entity.test.EntityTestSuite.testTransactionUtilRollback(EntityTestSuite.java:437)
[java] sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[java] sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[java] java.lang.reflect.Method.invoke(Method.java:597)
[java] junit.framework.TestCase.runTest(TestCase.java:154)
[java] junit.framework.TestCase.runBare(TestCase.java:127)
[java] junit.framework.TestResult$1.protect(TestResult.java:106)
[java] junit.framework.TestResult.runProtected(TestResult.java:124)
[java] junit.framework.TestResult.run(TestResult.java:109)
[java] junit.framework.TestCase.run(TestCase.java:118)
[java] junit.framework.TestSuite.runTest(TestSuite.java:208)
[java] junit.framework.TestSuite.run(TestSuite.java:203)
[java] junit.framework.TestSuite.runTest(TestSuite.java:208)
[java] junit.framework.TestSuite.run(TestSuite.java:203)
[java] org.apache.ofbiz.testtools.TestRunContainer.start(TestRunContainer.java:146)
[java] org.apache.ofbiz.base.container.ContainerLoader.start(ContainerLoader.java:100)
[java] org.apache.ofbiz.base.start.Start.startStartLoaders(Start.java:272)
[java] org.apache.ofbiz.base.start.Start.startServer(Start.java:322)
[java] org.apache.ofbiz.base.start.Start.start(Start.java:326)
[java] org.apache.ofbiz.base.start.Start.main(Start.java:411)
[java] --------------------------------------------------------------------------------
[java]
....
....
....
[java] 2009-12-22 16:05:28,366 (main) [ TransactionUtil.java:346:INFO ] [TransactionUtil.rollback] transaction rolled back
[java] 2009-12-22 16:05:28,370 (main) [ TestRunContainer.java:234:INFO ] [JUNIT] : [test case's name] finished.
Test result
After you run unit test, you can see the result of the testing. If you use run-tests target, you can see test result web page by view runtime/logs/test-results/html/index.html file in web browser
and see JUnit Test Result files for each test suite in runtime/logs/test-results directory. If you use other target you only see JUnit Test Result file in runtime/logs/test-results.