Groovy, Junit4 unit test and related test runner

I am trying to write Junit4 test cases for my Groovy code. The Junit 4 test script works fine inside my Eclipse IDE, which is the SpringSource Tool Suite. However, I cannot run the test to run all the test cases.

Here is my test subject’s current attempt. This is 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 happened? Oh, here is GSieveTest.groovy. I work great using the "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)
}

} 
+3
source share
2 answers

Groovy JUnit 4. , :

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

. JUnitCore (JUnit4) . , TestSuites , . Result , , , , .

+3

- Groovy JUnit4. JUnit4: http://groovy.codehaus.org/Using+JUnit+4+with+Groovy

script (ScriptTestAdapter), Groovy, JUnit. :

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

Source: https://habr.com/ru/post/1764420/


All Articles