The exiled grails method that uses findAll, throwing 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 this method, but findAll continues to throw an exception.

This problem is working! Now I need to write a test for it, and I keep getting this exception. Can anybody help me!

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)
+3
source share
1 answer

I would move the request to a Consumer domain class with a descriptive name like

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 challenge is just

def list = Consumer.findAllEatenByDate(addedDate)

and you can easily fool it with

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

Be sure to test the search method in your user integration test.

+7
source

Source: https://habr.com/ru/post/1792907/


All Articles