Unit testing – use findall to generate simulated Grails methods for missingmethodexception
•
Java
def retrieveEatenFood(String token,String addedDate) {
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; } }
An attempt was made to emulate the above method, but findall continues to generate an exception
This question is valid! Now I need to write tests for it, and I always get this exception Anyone can 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)
Solution
I will use descriptive names to move the query to the consumer domain class, such as
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 phone is simple
def list = Consumer.findAllEatenByDate(addedDate)
You can easily imitate
def foods = [new EatenFood(...),new EatenFood(...),...] Consumer.MetaClass.static.findAllEatenByDate = { String date - > foods }
Be sure to test the finder method in consumer integration testing
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码