Quantcast

Mocked GORM methods in unit test produce different results in unit test and domain class

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Mocked GORM methods in unit test produce different results in unit test and domain class

NoUsername
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Mocked GORM methods in unit test produce different results in unit test and domain class

Thando Mafela
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
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



--
View this message in context: http://grails.1312388.n4.nabble.com/Mocked-GORM-methods-in-unit-test-produce-different-results-in-unit-test-and-domain-class-tp4633443.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email





--
Thando Mafela
"Within our dreams and aspirations we find our opportunities".
Sue Ebaugh

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Mocked GORM methods in unit test produce different results in unit test and domain class

NoUsername
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"
    }
}
===
Loading...