unit testing - Cannot mock grails domain class -


someone, please, explain why domain class behaves different when mocked new mockfor(..)? use grails 2.4.1. have create domain class gtest:

class gtest {     static constraints = {     }      string amethod() {         return "gtest.amethod"     } } 

and groovy class dtest:

class dtest {      string amethod() {         return "dtest.amethod"     } } 

and here groovy test:

class groovyjunittest {      @test     void testdtestamethod() {         def mock = new mockfor(dtest)         mock.demand.amethod {             return "mock dtest"         }          mock.use {             def dtest = new dtest()             // here dtest.metaclass=groovy.mock.interceptor.mockproxymetaclass@...             def ret = dtest.amethod()             // assertation successes             assertequals "mock dtest", ret         }     }      @test     void testgtestamethod() {         def mock = new mockfor(gtest)         mock.demand.amethod {             return "mock gtest"         }          mock.use {             def gtest = new gtest()             // here dtest.metaclass=groovy.lang.expandometaclass@...             def ret = gtest.amethod()             // assertation fails             assertequals "mock gtest", ret         }     } } 

so, question how should domain class mocked programmatically? thank explanation.

you should annotation test grails.test.mixin.testfor.

@grails.test.mixin.testfor(mydomainclass) class mydomainclasstest {     // ... } 

see http://grails.org/doc/latest/guide/testing.html#unittestingdomains more details.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -