Grails

If Service use domain.find() method, the testcase will throw MissingMethodException

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Not A Bug
  • Affects Version/s: 1.2 final
  • Fix Version/s: 1.2.1
  • Component/s: Testing
  • Labels:
    None
  • Environment:
    jdk1.6,winxp,groovy 1.6.7,grails 1.2.0
  • Testcase included:
    yes

Description

i create a demo project to repeat this problem(i can't add Attachment,so i paste code below):

==========domain========================
class Book {
String title
String isbn
static constraints = {
}
}

==================service============================
class BookService {

boolean transactional = true

def add(String title,String isbn) {
def books = Book.list()

//======test case will fire exception====================================
Book book = Book.find(" from Book as b where b.isbn=:isbn",[isbn:isbn])
//=======================================================================
if(!book){ book = new Book() book.title = title book.isbn = isbn book.save() }
return book

}

def test(){ return Book.count() }
}

=================testcase======================
import grails.test.*

class BookServiceTests extends GrailsUnitTestCase {
protected void setUp() { super.setUp() }

protected void tearDown() { super.tearDown() }

void testAddBook() { mockDomain(Book) Book b1 = new Book() b1.title="Groovy in action" b1.isbn ="332-2342" b1.save(flush:true) def bookService = new BookService() def b2 = bookService.add("Groovy in action","332-2342") def books = Book.list() assertTrue books.size()==1 }
}

================================================
then the testcase will throw MissingMethodException:

No signature of method: org.bookcrm.Book.find() is applicable for argument types: (java.lang.String, java.util.LinkedHashMap) values: [ from Book as b where b.isbn=:isbn, [isbn:332-2342]]

groovy.lang.MissingMethodException: No signature of method: org.bookcrm.Book.find() is applicable for argument types: (java.lang.String, java.util.LinkedHashMap) values: [ from Book as b where b.isbn=:isbn, [isbn:332-2342]]
at grails.test.MockUtils$_addDynamicFinders_closure54.doCall(MockUtils.groovy:636)
at org.bookcrm.BookService.add(BookService.groovy:11)
at org.bookcrm.BookService$add.call(Unknown Source)
at org.bookcrm.BookServiceTests.testAddBook(BookServiceTests.groovy:23)
at _GrailsTest_groovy$_run_closure4.doCall(_GrailsTest_groovy:262)
at _GrailsTest_groovy$_run_closure4.call(_GrailsTest_groovy)
at _GrailsTest_groovy$_run_closure2.doCall(_GrailsTest_groovy:221)
at _GrailsTest_groovy$_run_closure1_closure21.doCall(_GrailsTest_groovy:181)
at _GrailsTest_groovy$_run_closure1.doCall(_GrailsTest_groovy:168)
at TestApp$_run_closure1.doCall(TestApp.groovy:102)
at gant.Gant$_dispatch_closure4.doCall(Gant.groovy:324)
at gant.Gant$_dispatch_closure6.doCall(Gant.groovy:334)
at gant.Gant$_dispatch_closure6.doCall(Gant.groovy)
at gant.Gant.withBuildListeners(Gant.groovy:344)
at gant.Gant.this$2$withBuildListeners(Gant.groovy)
at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
at gant.Gant.dispatch(Gant.groovy:334)
at gant.Gant.this$2$dispatch(Gant.groovy)
at gant.Gant.invokeMethod(Gant.groovy)
at gant.Gant.processTargets(Gant.groovy:495)
at gant.Gant.processTargets(Gant.groovy:480)

Activity

Hide
Graeme Rocher added a comment -

Is the test in your test/unit or test/integration directory?

Show
Graeme Rocher added a comment - Is the test in your test/unit or test/integration directory?
Hide
Sakke Wiik added a comment -

Same happens with createCriteria(). Tests are in test/unit directory.

groovy.lang.MissingMethodException: No signature of method: DomainObject.createCriteria() is applicable for argument types: () values: []

Using Groovy 1.7.0 + Grails 1.2.0

Show
Sakke Wiik added a comment - Same happens with createCriteria(). Tests are in test/unit directory. groovy.lang.MissingMethodException: No signature of method: DomainObject.createCriteria() is applicable for argument types: () values: [] Using Groovy 1.7.0 + Grails 1.2.0
Hide
Luke Daley added a comment -

HQL finders are not supported in integration tests.

Either use a dynamic finder...

Book book = Book.findByIsbn(isbn)

Or move this to an integration test.

Show
Luke Daley added a comment - HQL finders are not supported in integration tests. Either use a dynamic finder...
Book book = Book.findByIsbn(isbn)
Or move this to an integration test.
Hide
Graeme Rocher added a comment -

This is by design, in unit tests use the mockDomain method, otherwise create an integration test

Show
Graeme Rocher added a comment - This is by design, in unit tests use the mockDomain method, otherwise create an integration test

People

Vote (1)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: