OFBiz JUnit test cases for Entity Query builder methods
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:

    1. 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.)
    2. Next, we add one Java file named EntityQueryTestSuite.java into /framework/entity/src/org/ofbiz/entity/test/ directory.
    3. 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.
    4. Commands to run JUnit test cases:
    5. First, we build the tests by using:
    1. After building the code, we are ready to execute the test cases. We can execute them by using:
    1. The above command will run only the test cases which are designed in ‘entity-query-test’ test case.
    2. 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“.
    3. After running all the test cases the  following output will be seen on the  console if all the JUnit tests are working correctly:

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.

DATE: Dec 16, 2014
AUTHOR: Arun Patidar
OFBiz Tutorials, OFBiz, OFBiz Tutorials for Beginners