简体   繁体   English

网格单元测试

[英]Grails Unit Test

I have unit test wich extends GrailsUnitTestCase : 我有扩展GrailsUnitTestCase的单元测试:

 import grails.test.GrailsUnitTestCase
 class HttpdParserSpec extends GrailsUnitTestCase {

 } 

However I saw in Grails documentation that is deprecated. 但是,我在Grails文档中看到了已弃用的文档。 I tried to use the following : 我尝试使用以下内容:

import grails.test.mixin.TestFor

@TestFor(HttpdParser)
class HttpdParserSpec {
}

I obtain the following error : 我收到以下错误:

Cannot add Domain class [class fr.edu.toolprod.parser.HttpdParser]. 无法添加域类[类fr.edu.toolprod.parser.HttpdParser]。 It is not a Domain! 它不是域!

It's true.It's not a Domain class.I only want test a simple class HttpdParser. 是的,它不是Domain类,我只想测试一个简单的类HttpdParser。

What am I doing wrong ? 我究竟做错了什么 ?

So how to make a simple unit test ? 那么如何进行简单的单元测试? Have you an example ? 你有例子吗?

Don't use the TestFor annotation. 不要使用TestFor批注。 Just write a unit test as you normally would. 像往常一样编写单元测试。 TestFor is useful for rigging up Grails artifacts and relevant elements of the environment for unit testing them. TestFor对于组装Grails工件和环境的相关元素以进行单元测试非常有用。

class HttpdParserSpec extends spock.lang.Specification {
    void 'test something'() {
        when:
        def p = new HttpdParser()
        p.doSomething()

        then:
        p.someValue == 42
    }
}

You can also just use the @TestMixin annotation with the GrailsUnitTestCaseMixin like this: 您还可以将@TestMixin批注与GrailsUnitTestCaseMixin一起使用,如下所示:

import grails.test.mixin.support.GrailsUnitTestMixin
import grails.test.mixin.TestMixin

@TestMixin(GrailsUnitTestMixin)
class MyTestClass {}

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

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