以下是我的代码:
package ro
import org.junit.Test
/**
* Created by roroco on 3/18/15.
*/
class TryTest extends GroovyTestCase {
@Test
def testSmth() {
assert 1 == 1
}
}
然后我用'gradle test --tests ro.TryTest'运行它,它引发:
ro.TryTest > junit.framework.TestSuite$1.warning FAILED
junit.framework.AssertionFailedError at TestSuite.java:97
1 test completed, 1 failed
:test FAILED
这里是source
答案 0 :(得分:3)
测试需要为void
返回GroovyTestCase
,因此您的测试类应为:
package ro
/**
* Created by roroco on 3/18/15.
*/
class TryTest extends GroovyTestCase {
void testSmth() {
assert 1 == 1
}
}
此外,您的build.gradle文件根据定义不需要java
AND groovy
插件,groovy
导入java
,因此您的文件可以是:
apply plugin: 'groovy'
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.1'
testCompile 'junit:junit:4.12'
}
作为一个无关的方面,我倾向于使用Spock代替GroovyTestCase
这些天,所以如果你添加:
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
到您的依赖项,然后您可以编写Spock测试(这将进入src/test/groovy/ro/TrySpec.groovy
)
package ro
class TrySpec extends spock.lang.Specification {
def 'a simple test'() {
when: 'I have a number'
def number = 1
then: 'It should equal 1'
number == 1
}
}