我正在尝试为我的 Groovy 代码编写 Junit4 测试用例。Junit 4 测试用例在我的 Eclipse IDE(即 SpringSource 工具套件)中运行良好。但是,我无法运行测试来运行所有测试用例。
这是我目前对测试跑步者的尝试。它几乎直接取自 Groovy 网站本身:
import groovy.util.GroovyTestSuite;
import org.codehaus.groovy.runtime.ScriptTestAdapter
import junit.framework.*;
class allTests {
static Test suite() {
def gsuite = new GroovyTestSuite()
gsuite.addTest(new ScriptTestAdapter(gsuite.compile("test/GSieveTest.groovy"), [] as String[]))
return gsuite
}
}
junit.textui.TestRunner.run(allTests.suite())
结果是:
org.codehaus.groovy.runtime.InvokerInvocationException:
org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack:
No signature of method: GSieveTest.main() is applicable for argument types: () values: []
怎么了?哦,这里是 GSieveTest.groovy。我使用“运行为 Junit 测试...”运行良好
import static org.junit.Assert.assertEquals;
import org.junit.Test;
class GSieveTest {
@Test
public void Primes1To10() {
def sieve = (0..10).toList()
GSieve.filter(sieve); // [1,2,3,5,7]
assertEquals("Count of primes in 1..10 not correct", 5, (sieve.findAll {it -> it != 0}).size());
}
@Test
public void FiftyNineIsPrime() {
def sieve = (0..60).toList()
GSieve.filter(sieve);
assertEquals("59 must be a prime", 59, sieve[59]);
}
@Test
public void Primes1To100() {
def sieve = (0..100).toList()
GSieve.filter(sieve);
def list = sieve.findAll {it -> it != 0}
def primes = [1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]
println list
println primes
assertEquals(true, list == primes)
}
}