How to use execQuery in grails test array?

How to use execQuery in grails test file?

A error : groovy.lang.MissingMethodException: No signature of method ***.executeQuery() is applicable for argument types: () values: []

I already called mockDomain.

By the way, it is in unit test.

Thank!

+5
source share
3 answers

Unit tests do not yet support HQL queries, but we are working on it. But you should not do perseverance tests with mocks. Persistence tests should be performed against the database in an integration test.

I usually move HQL queries to a domain class as static query methods. Thus, they easily mock the unit test of the controller, services, etc., and then I test this method as part of the domain class integration test.

For example, I will have

class User {
   String username
   String password
   ...

   static List findAllUsersBlahBlah(String foo, boolean bar) {
      executeQuery('from User u where ...')
   }
}

unit test , unit test - , , , :

def users = [new User(...), new User(...)]
User.metaClass.static.findAllUsersBlahBlah = { String foo, boolean bar -> users }
+14

executeQuery Grails 2.0

@TestFor(BookController)
@TestMixin([DomainClassUnitTestMixin,ServiceUnitTestMixin])
@ConfineMetaClassChanges([Book])
class BookControllerSpec extends Specification{
   mockDomain(Book)
   Book.metaClass.static.executeQuery = {a,b,c-> return [Book]}
+8

In Grails 2.5.4, you can use GroovyMockfor mocking static methods implemented in Java:

 GroovyMock(Book, global: true)

I just tested - it works for _.executeQuery()

0
source

All Articles