Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking grails method that uses a findAll, Generating a MissingMethodException

def retrieveEatenFood(String token, String addedDate) {     

    def consumer = Consumer.findByMobileToken(token)
    if(consumer != null) {  

        def efList = []

        def list = consumer.findAll("from EatenFood as ef where date(ef.dateAdded) =  date(:da)",[da:sdf_long.parse(addedDate)])
        list.each{
            def eatenList = [:]
            eatenList.put("foodType",it.food.name)
            eatenList.put("sequenceNumber",it.sequenceNumber)
            eatenList.put("eatenDate", it.eatenDate)
            eatenList.put("DateAdded",it.dateAdded)
            efList.add(eatenList);
        }

        return efList;  
    }
}

Trying to mock the above method, but the findAll keep generating an exception.

This issue it works! Now i need to write the test for it and i keep getting this exception. Can anyone help me please!

groovy.lang.MissingMethodException: No signature of method: carrotdev.Consumer.findAll() is applicable for argument types: (java.lang.String, java.util.LinkedHashMap) values: [from EatenFood as ef where date(ef.dateAdded) =  date(:da), [da:Sun Feb 13 01:51:47 AST 2011]]
Possible solutions: findAll(groovy.lang.Closure), find(groovy.lang.Closure)
    at carrotdev.ConsumerService.retrieveEatenFood(ConsumerService.groovy:146)
    at carrotdev.ConsumerService$retrieveEatenFood.call(Unknown Source)
    at carrotdev.ConsumerServiceTests.testEatenFoodRetrievedSucessfully(ConsumerServiceTests.groovy:359)
like image 248
ferronrsmith Avatar asked Jun 17 '26 10:06

ferronrsmith


1 Answers

I would move the query to the Consumer domain class with a descriptive name, e.g.

static List<EatenFood> findAllEatenByDate(String date) {
   consumer.findAll(
      "from EatenFood as ef where date(ef.dateAdded) = date(:da)",
      [da:sdf_long.parse(addedDate)])
}

Then the call is simply

def list = Consumer.findAllEatenByDate(addedDate)

and you can mock that easily with

def foods = [new EatenFood(...), new EatenFood(...), ...]
Consumer.metaClass.static.findAllEatenByDate = { String date - > foods }

Be sure to test the finder method in your Consumer integration test.

like image 137
Burt Beckwith Avatar answered Jun 21 '26 07:06

Burt Beckwith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!