开发者

Cannot perform unit test with function which have createCriteria() statement

开发者 https://www.devze.com 2023-02-19 07:53 出处:网络
I want to write a unit test (by JUnit) to test value of this function in Groovy: String getPeopleNamesById(int[] peopleIds) {

I want to write a unit test (by JUnit) to test value of this function in Groovy:

    String getPeopleNamesById(int[] peopleIds) {
        List<String> names = People.createCriteria().list{
            projections { property("name") }
            'in' ("id", peopleIds)
        }
        return names ? names.join(", ") : "";
    }

But the unit test always fails when reading this statement: List names = People.createCriteria().list{...} groovy.lang.MissingMethodException: No signature of method: People.createCriteria() is applicable for argument types: () values: []. I guess it because of ca开发者_如何学编程lling to functions which execute some DB connections and queries? Could you please help me write this test? Thank you so much!


Criteria queries aren't available in unit tests, and aren't provided by mockDomain. You can either mock your criteria queries yourself, e.g. with mockFor, or make your test an integration test, where you have access to a full database environment.

Here's an example of how you could mock your query:

mockFor(People).demand.static.createCriteria = { ->
    [list: { closure -> [ <some mock objects> ] } ]
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号