OFBiz JUnit test cases for Entity Query builder methods

Arun PatidarPublished: Updated:

DIY Apache OFBIz JUnit test case for entity query builder methodsTech 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.

Arun Patidar About Arun Patidar
Arun Patidar is a Manager of Enterprise Software Development with HotWax Systems. Arun is an e-commerce ERP enthusiast and full time developer with expertise on OFBiz. He enjoys participating in the OFBiz community and sharing his thoughts on new interventions in the OFBiz community.

Comments 1

  1. Avatar for Arun Patidar

    with 13.07.03 i flowed the 3 step. the framewok/entity/build/classes/org/ofbiz/entity/test/EntityQueryTestSuite.class have alreay existed,but it seems that the ant run-test can’t find it.how can i reolve it?
    ➜ apache-ofbiz-13.07.03 ant run-test -Dtest.component=entity -Dtest.case=entity–query–tests

    console:
    [java] org.ofbiz.base.start.StartupException: Cannot start() org.ofbiz.testtools.TestRunContainer (No tests found (entity / null / entity?query?tests))
    [java] at org.ofbiz.base.container.ContainerLoader.start(ContainerLoader.java:237)
    [java] at org.ofbiz.base.start.Start.startStartLoaders(Start.java:353)
    [java] at org.ofbiz.base.start.Start.start(Start.java:379)
    [java] at org.ofbiz.base.start.Start.main(Start.java:135)
    [java] org.ofbiz.base.container.ContainerException: No tests found (entity / null / entity?query?tests)
    [java] at org.ofbiz.testtools.TestRunContainer.start(TestRunContainer.java:121)
    [java] at org.ofbiz.base.container.ContainerLoader.start(ContainerLoader.java:235)
    [java] at org.ofbiz.base.start.Start.startStartLoaders(Start.java:353)
    [java] at org.ofbiz.base.start.Start.start(Start.java:379)
    [java] at org.ofbiz.base.start.Start.main(Start.java:135)
    [java] org.ofbiz.base.container.ContainerException: No tests found (entity / null / entity?query?tests)
    [java] at org.ofbiz.testtools.TestRunContainer.start(TestRunContainer.java:121)
    [java] at org.ofbiz.base.container.ContainerLoader.start(ContainerLoader.java:235)
    [java] at org.ofbiz.base.start.Start.startStartLoaders(Start.java:353)
    [java] at org.ofbiz.base.start.Start.start(Start.java:379)
    [java] at org.ofbiz.base.start.Start.main(Start.java:135)
    [java] java.lang.Exception: Error during start.
    [java] at org.ofbiz.base.start.Start.start(Start.java:383)
    [java] at org.ofbiz.base.start.Start.main(Start.java:135)

Leave a Reply

Your email address will not be published. Required fields are marked *