groovy, Junit4 unit test, and related test runner
I'm trying to write Junit4 test cases for my Groovy code. The Junit 4 test case works fine inside my Eclipse IDE which is SpringSource Tool Suite. I can't get a test running to run the all of the test cases, however.
Here is my current attempt at a test runner. It's pretty much taken directly from the Groovy website itself:
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())
Results in:
org.codehaus.groovy.runtime.InvokerInvocationException:
org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack:
No signature of method: GSieveTest.main() is applicable for argument types: () values: []
What's wrong? Oh, here is the GSieveTest.groovy. I runs fine using "Run As Junit test..."
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)
}
}
I also battled with this for ages and could not get the Groovy documented samples to work with JUnit 4. This was my solution which was trivially simple in the end:
Create a script called RunAllTestScripts.groovy:
package com.mypackage
import org.junit.runner.JUnitCore
result = JUnitCore.runClasses MyGroovyTestClass, MyOtherGroovyTestClass, AnotherGroovyTestClass
String message = "Ran: " + result.getRunCount() + ", Ignored: " + result.getIgnoreCount() + ", Failed: " + result.getFailureCount()
if (result.wasSuccessful()) {
println "SUCCESS! " + message
} else {
println "FAILURE! " + message
result.getFailures().each {
println it.toString()
}
}
That's it. The JUnitCore (JUnit4) runner will run these test classes. There's probably a more elegant solution with TestSuites to be had but this is sufficient for my needs. The Result object contains the ran, ignored, failed counts as well as the detailed failure list which is easily interrogated.
Now I got it -- Groovy doesn't have a test runner for JUnit4 tests. Here is the extent of JUnit4 support: http://groovy.codehaus.org/Using+JUnit+4+with+Groovy
What the script test adapter (ScriptTestAdapter) allows you to do is run groovy scripts that are NOT JUnit tests. Here's an example:
-----ScriptTest1.groovy--------
import static org.junit.Assert.assertEquals;
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());
-----end of ScriptTest1--------
精彩评论