简体   繁体   English

Grails单元测试失败

[英]Grails Unit Test Failing

So, I am starting learning groovy & grails. 因此,我开始学习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' 这是在“ 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. 在测试类中模拟域实例之前,它无法识别域类上的诸如save()之类的动态方法。

Use @Mock(Product) at class level in the test class. 在测试类的类级别上使用@Mock(Product)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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