user1403505
user1403505

Reputation: 1005

error when running groovy test suite

created two tests in groovy able to run them indecently as groovy test cases but when I create a test suite and run them like groovy MyTestSuite.groovy on cmd line I get the below error:

F.F
Time: 0
There were 2 failures:
1) warning(junit.framework.TestSuite$1)junit.framework.AssertionFailedError: No tests found in mypack.ArithmeticTest
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
        at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1318)
        at org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod(InvokerHelper.java:927)
        at org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod(InvokerHelper.java:77)
        at groovy.lang.GroovyShell.runJUnit3TestSuite(GroovyShell.java:370)
        at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:277)
        at groovy.lang.GroovyShell.run(GroovyShell.java:502)
        at groovy.lang.GroovyShell.run(GroovyShell.java:491)
"a.txt" 53L, 3408C

The test suite class is as follows

package mypack

import junit.framework.TestSuite
import junit.framework.JUnit4TestAdapter

public class myTestSuite extends TestSuite {
     // Since Eclipse launches tests relative to the project root,
     // declare the relative path to the test scripts for convenience
     private static final String TEST_ROOT = "src/mypack/";
     public static TestSuite suite() throws Exception {
         TestSuite suite = new TestSuite();
         GroovyTestSuite gsuite = new GroovyTestSuite();

         suite.addTestSuite(gsuite.compile("/Users/barumugham/Documents/workspace/Groovy/UnitTestGroovy/src/mypack/ArithmeticGroovy.groovy"));
         suite.addTestSuite(gsuite.compile("/Users/barumugham/Documents/workspace/Groovy/UnitTestGroovy/src/mypack/ArrayTest.groovy"));
         return suite;
     }
}

ArithmeticGroovy.groovy

package mypack

import org.junit.Test
import static org.junit.Assert.assertEquals

class ArithmeticTest {
    @Test
    void additionIsWorking() {
        assertEquals 4, 2+2
    }

    @Test(expected=ArithmeticException)
    void divideByZero() {
        println 1/0
    }
}

when i run it through eclipse i get initializationerror. I am new to groovy any help is appreciated

Upvotes: 2

Views: 834

Answers (1)

Patru
Patru

Reputation: 4551

Groovy seems to be confused if you confuse (pardon the pun :-) JUnit3 and JUnit4 style. Using TestSuite and addTestSuite is JUnit3 style and it should be coupled with classes derived from TestCase (or ultimately Test). Mixing JUnit4 annotation style won't work in this setup as exemplified in this post.

You should select either JUnit3 or JUnit4 style for your tests (where I personally tend to prefer JUnit4).

Upvotes: 1

Related Questions