SSC
SSC

Reputation: 3008

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

Upvotes: 0

Views: 88

Answers (1)

dmahapatro
dmahapatro

Reputation: 50245

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.

Upvotes: 2

Related Questions