Our technical forum series continues today with a discussion of OFBiz JUnit test cases for Entity Query API. Because outside OFBiz JUnit test cases are not yet available for this new Entity Query API we’ve designed and executed our own. Today we will share the results.
(For more information about JUnit data setup and execution please refer to “JUnit Tests in OFBiz – Data Setup and Execution“)
Strategy for implementation:
Here we compare results generated by the tried-and-true Entity Engine methods with the results generated by the our EntityQuery methods for a similar entity query. We also provide JUnit test cases for the following methods of EntityQuery API:
1. queryList()
2. queryFirst()
3. queryCount()
4. queryOne()
5. distinct()
6. filterByDate()
7. orderBy()
8. select()
9. cursorForwardOnly()
10. cursorScrollSensitive()
11. cursorScrollInSensitive()
12. fetchSize()
13. maxRows()
14. cache()
15. queryIterator()
OFBiz offers superior support for integrating and executing JUnit test cases. Follow these simple steps to create test cases:
1
|
< test – case case – name =“ entity – query – tests“ > < junit – test – suite class – name =“ org . ofbiz . entity . test . EntityQueryTestSuite“ / > < / test – case >
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public class EntityQueryTestSuite extends EntityTestCase {
public EntityQueryTestSuite ( String name ) {
super ( name ) ;
}
/*
* queryCount(): This method returns number of records found for the particular query.
* assert: Compared count of number of records found by Entity Engine method with count of number of records found by EntityQuery method.
*/
public void testQueryCount ( ) throws GenericEntityException {
List < GenericValue > testingTypes = new LinkedList < GenericValue > ( ) ;
testingTypes . add ( delegator . makeValue (“ TestingType“ , “ testingTypeId“ , “ record – 1“ , “ description“ , “ Record One“ ) ) ;
testingTypes . add ( delegator . makeValue (“ TestingType“ , “ testingTypeId“ , “ record – 2“ , “ description“ , “ Record Two“ ) ) ;
testingTypes . add ( delegator . makeValue (“ TestingType“ , “ testingTypeId“ , “ record – 3“ , “ description“ , “ Record Three“ ) ) ;
delegator . storeAll ( testingTypes ) ;
List < GenericValue > totalRecordsByEntityEngine = delegator . findList (“ TestingType“ , null , null , null , null , false ) ;
int numberOfRecordsByEntityQuery = ( int ) EntityQuery . use ( delegator ) . from (“ TestingType“ ) . queryCount ( ) ;
assertEquals (“ queryCount ( ) : Total Number of Records matched“ , totalRecordsByEntityEngine . size ( ) , numberOfRecordsByEntityQuery ) ;
}
}
|
1
|
. / ant build
|
1
|
. / ant run – test – Dtest . component = entity – Dtest . case = entity – query – tests
|
1
2
|
[ java ] 20141029150255604 | main | TestRunContainer | I | [ JUNIT ] Results for test suite : entitytests
[ java ] 20141029150255604 | main | TestRunContainer | I | [ JUNIT ] Pass : true | # Tests: 15 | # Failed: 0 # Errors: 0
|
If there are any errors in the tests, we then know that the service or method for which test case is designed is not working properly. We can then log the error and use this information to discover what is going wrong in method or service.
HotWax Media is the leading global innovator of commerce solutions powered by Apache OFBiz. Contact us today for a free, expert consultation.