views:

61

answers:

2

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)
}

} 
A: 

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--------
rock298