Joseph
Joseph

Reputation: 1461

Grails Unit Test Buggy Dynamic Finder

I am in the process of writing unit tests for a service class. This service class calls MyDomain.findAllByIdNotInList. The issue I am facing is that grails does not recognize NotInList as a dynamic finder for a mocked domain. I tried Metaclass-ing this functionality out, but was having issues with it.

Any creative ways for bypassing this short of turning the Unit Test into an Integration test? I would like to avoid this for multiple reasons (time to run, Only our Unit tests run at build time, etc)

Also, it is possible my metaclassing is written poorly:

MyDomain.metaClass.findAllByIdNotInList = {ArrayList list ->
        return []
    }

Edit: Using grails 1.3.7.

also tried

 MyDomain.metaClass.findAllByIdNotInList = {deflist ->
        return []
    }

Bug report here:

http://jira.grails.org/browse/GRAILS-8593

Upvotes: 0

Views: 946

Answers (1)

Jarred Olson
Jarred Olson

Reputation: 3243

@Sagar V's comment is correct you should be able to utilize all dynamic finders when a Domain is properly mocked. If you're using a version of Grails before 2.0 you'd have to extend GrailsUnitTestCase and call MockDomain(MyDomain) before attempting to invoke the dynamic finders. As an FYI your metaClassing is not written properly (in my opinion you should use the mocking framework to get your test working I'm providing the correct syntax so you can use it properly in the future).

MyDomain.metaClass.'static'.findAllByIdNotInList = {defList ->
    []
}

When the method that you're overriding is static you need to add the .'static'. inbetween the metaClass and the method name.

Upvotes: 2

Related Questions