Tech Tip: Did you know that Apache OFBiz now delivers Entity Query API? It’s a feature that allows developers to build entity queries quickly and easily. To learn more about Entity Query API, check out our recent blog post.
HotWax Tech Forum: OFBiz JUnit test cases for Entity Query API
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()
Steps for creating simple JUnit test case:
OFBiz offers superior support for integrating and executing JUnit test cases. Follow these simple steps to create test cases:
-
- Add an entry of a new test case into the respective ‘{component-name}tests.xml’ file. Below, we are working on the entity component. As you can see, we make an entry of our new test into entitytests.xml. (/framework/entity/testdef/entitytests.xml). This entry should be made into an entitytests.xml file is as shown below: ( Here the case-name represents name of the test case and class-name represents the Java file in which we are going to design the test cases.)
1< test – case case – name =“ entity – query – tests“ > < junit – test – suite class – name =“ org . ofbiz . entity . test . EntityQueryTestSuite“ / > < / test – case >
- Next, we add one Java file named EntityQueryTestSuite.java into /framework/entity/src/org/ofbiz/entity/test/ directory.
- Let’s take a look at one sample JUnit test case designed for queryCount() method of EntityQuery. In the code shown below we have extended class EntityTestCase, which imports the JUnit library and delegator objects. First, we insert data into ‘TestingType’ entity. This data gets loaded at runtime into entity before running the test, and gets reverted before closing test. In the code below, you can see that count of records in the ‘TestingType’ entity is fetched by the regular Entity Engine method, and stored in a variable named ‘totalRecordsByEntityEngine’. Similarly, we retrieve the count from same entity using queryCount() method of EntityQuery API. We then store that value in ‘numberOfRecordsByEntityQuery’ variable. Finally, we assert both the variables for their equality. We love how simply we can design the JUnit for different methods of EntityQuery.
1234567891011121314151617181920public 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 ) ;}}
- Commands to run JUnit test cases:
- First, we build the tests by using:
- Add an entry of a new test case into the respective ‘{component-name}tests.xml’ file. Below, we are working on the entity component. As you can see, we make an entry of our new test into entitytests.xml. (/framework/entity/testdef/entitytests.xml). This entry should be made into an entitytests.xml file is as shown below: ( Here the case-name represents name of the test case and class-name represents the Java file in which we are going to design the test cases.)
1
|
. / ant build
|
-
- After building the code, we are ready to execute the test cases. We can execute them by using:
1
|
. / ant run – test – Dtest . component = entity – Dtest . case = entity – query – tests
|
-
- The above command will run only the test cases which are designed in ‘entity-query-test’ test case.
- To learn other commands to run test cases and load test data, reference to our previous blog post “JUnit Tests in OFBiz – Data Setup and Execution“.
- After running all the test cases the following output will be seen on the console if all the JUnit tests are working correctly:
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.
Schedule a free, expert consultation today
HotWax Media is the leading global innovator of commerce solutions powered by Apache OFBiz. Contact us today for a free, expert consultation.