How to implement criteria queries in unit test cases
Posted By : Shiv Kumar | 18-Jun-2015
Intrdouction to Plastic criteria
As the name suggest Plastic Criteria means somehow it is related to criteria.It is grails plugin to mock Grails Criteria for Unit Test cases.
Understand the need of Plastic Criteria : Why Plastic criteria plugin is required ?
1. Criteria queries and String-based queries like [executeQuery] are currently not supported in unit tests.
2. There's no support for HQL queries in unit tests yet.
3. Criteria queries and String-based queries aren't provided by mockDomain also. We have to mock your criteria queries itself like :
In case of Criteria :
mockFor(domianName).demand.static.createCriteria = { -> [list: { closure -> [] } ] }
In case of ExceuteQuery : Suppose this one is the query we have to test through test cases
UserCompanyRoleMapping.executeQuery("select distinct user from UserCompanyRoleMapping u where u.company = :company and :admin in elements(u.roles)", [user: user, company: company, admin: companyAdminRole])
When we have to implement such type of string based query then do something like this :
List userList = [new User(params), new User(params)]
UserCompanyRoleMapping.metaClass.static.findAllUsersByCompanyAndRoles = { String company, def role -> userList }
and put in domain UserCompanyRoleMapping.groovy
static List findAllUsersByCompanyAndRoles(String company, def role) {
executeQuery('from UserCompanyRoleMapping u where ...') // put your required query here
}
Conclusion of above :
As we can see in both the above approaches, the response of Criteria queries and String-based queries is not the actual one but it is defined earlier and hard coded(userList).
Solution :
To overcome such situations and to implement Criteria queries upto certain extent we require Plastic Criteria plugin.
Installation :
Put the following dependecy in your buildConfig :
plugins{
test ":plastic-criteria:1.5.1"
}
How to use in test cases :
1. Import in the test case in which you want to mock criteria like
import static plastic.criteria.PlasticCriteria.*
2. Use the following syntax to mock required domains criteria in test case setup :
mockCriteria([user,UserCompanyRoleMapping....])
Pros :
1. With the help of this plugin we are able to implement criteria(createCriteria and withCriteria) in our test cases.
2. It provide support for maximum nodes defined in criteria documentation.
3. Easy to use and understand.
Cons :
1. This plugin does not provide support for following :
>> count distinct
>> count
>> firstResult and some more.
For more detailed information visit the link : https://github.com/fabiooshiro/plastic-criteria
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Shiv Kumar
Shiv is an experienced Java Developer with a strong background in multiple technologies. He specializes in defining system architectures to ensure reliable and resilient solutions. He possesses a comprehensive understanding of the latest technologies and has hands-on experience in Core Java, Spring Boot, Hibernate, Apache Kafka messaging queue, Redis, as well as relational databases like MySQL and PostgreSQL, and non-relational databases like MongoDB. He excels in API implementations, Microservices, Web Services development, testing, and deployments. Shiv actively contributes to code enhancements and consistently delivers valuable contributions to various client projects, including Fabtrack, Pando, Pandojo, Digikam, WhatsApp Integration, Croniz, Punchin Application, Script TV, Bhaasha, and more. He demonstrates strong analytical skills and a creative mindset. In addition, he has a passion for reading books and exploring new technologies and innovations.