|
I am trying to test a domain class which has a method that uses dynamic finders (findBy...)
The domain class is mocked in the unit test (with @Mock, i am using grails 2.1) and if i execute the dynamic finder from within the unit test, i get the expected result (an object is found), however when i call the method on my domain class which executes the same finder (to test it), it does not find anything (returns null). Is this the expected behaviour or a bug? I could not find any hints about this in the unit testing / mock documentation that i have read so far. Thanks, Paul |
|
Hi
Can you send me a piece of your code.
On Mon, Aug 20, 2012 at 9:21 AM, NoUsername <[hidden email]> wrote: I am trying to test a domain class which has a method that uses dynamic Thando Mafela "Within our dreams and aspirations we find our opportunities". Sue Ebaugh |
|
Sure, this is a minimal example which is quite close to my real setup:
domain file: example.Offer === package example public class Offer { Offer rootVersion int versionNr = 0 def getHighestVersion() { def highestNumber = -1 // search highest version from same root obj def highestVersion = Offer.findByRootVersion(rootVersion, [sort:"versionNr", order:"desc", max: 1]) println "highestVersion: " + highestVersion highestVersion = highestVersion.find()?.versionNr if (!highestNumber) { highestNumber = -1 } highestNumber } } === test file: === package test import example.*; import static org.junit.Assert.* import grails.test.mixin.support.* @TestMixin(GrailsUnitTestMixin) @Mock(Offer) class OfferTest { void testSomething() { def o1 = new Offer([versionNr:1]) o1.rootVersion = o1 o1.save() def o2 = new Offer([versionNr:2, rootVersion:o1]) o2.save() // query in unit test works def highestObj = Offer.findByRootVersion(o1, [sort:"versionNr", order:"desc", max: 1]) assert highestObj.find()?.versionNr == 2 println "this worked" assert o2.getHighestVersion() == 2 println "this didn't - returned -1 because query returned null" } } === |
| Powered by Nabble | Edit this page |
