简体   繁体   中英

Grails Unit Test Failing

So, I am starting learning groovy & grails. I am trying to write unit tests for my controller like this:

void testSave() {

  params.productName = 'ProdName'
  params.productBarCode = '123'
  params.productStore = 'ProdStore'
  def response = controller.save()
  assert response.productInstance.productName == 'ProdName'

}

and this is the controller action

def save() {
        def productInstance = new Product(params)
        if (!productInstance.save(flush: true)) {
            render(view: "create", model: [productInstance: productInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'product.label', default: 'Product'), productInstance.id])
        redirect(action: "show", id: productInstance.id)
    }

and this is the exception it throws when 'test-app'

groovy.lang.MissingMethodException: No signature of method: xxx.Product.save() is applicable for argument types: () values: []
Possible solutions: save(), save(boolean), save(java.util.Map), wait(), any(), wait(long)
    at xxx.ProductController.save(ProductController.groovy:59)
    at xxx.ProductControllerTests.testSave(ProductControllerTests.groovy:35)

I am sorry if this question is too naive. Please help Thanks

Until the domain instance is mocked in the test class, it won't be able to recognize dynamic methods like save() on the domain class.

Use @Mock(Product) at class level in the test class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM