[groovy-user] GroovyTestSuite compile doesn't support annotated Junit 4 test?

160 views
Skip to first unread message

Marcel Wagner

unread,
Apr 26, 2009, 11:43:01 AM4/26/09
to us...@groovy.codehaus.org
I have a Java TestSuite class to run all my test from eclipse build in
JUnit 4 runner:

public class EFCTestSuite extends TestSuite {
private static final String TEST_ROOT = "src/test/";

@SuppressWarnings("unchecked")
public static TestSuite suite() throws Exception {
TestSuite suite = new TestSuite();
GroovyTestSuite gsuite = new GroovyTestSuite();

// add here all tests
suite.addTestSuite(gsuite.compile(TEST_ROOT + "TestTest.groovy"));
return suite;
}
}

My tests should be groovy JUnit 4 annotated test

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

But the eclipse junit 4 runner says, he can't find any test ;-(

Changing the class to extend GroovyTestCase

class TestTest extends GroovyTestCase {
void testSomething() {
assert 2 + 2 == 4 : "We're in trouble, arithmetic is broken"
}
}

All works fine. Annotations seems not supported? Is this a
GroovyTestSuite feature or a Eclipse Plugin feature or a Groovy feature?
Or do I something wrong?

Thanks for the help,

Marcel


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Roshan Dawrani

unread,
Apr 26, 2009, 3:48:50 PM4/26/09
to us...@groovy.codehaus.org
As far as I understand, the cause for the problem you are facing is that you are mixing JUnit 3.x and 4.x ways of defining test suite and writing tests.

If you follow the suite.addTestSuite(<someClass>) approach, you are using junit 3.x approach of creating test suite. When JUnit looks for tests in addTestSuite() path, it does so using 3.x notations (checks whether test class extends TestCase and method name/signature meet the requirements, etc). In your case TestTest.groovy is defined using 4.x notation - it is a POJO, does not follow naming conventions, etc and hence JUnit cannot find any tests in it.

You can use the 3.x suite.addTestSuite(<someClass>) approach with test classes extending TestCase/GroovyTestCase and test methods following the 3.x notations.

Or, you can adopt 4.x approach for both these things and do something like:
============EFCTestSuite.java============
import groovy.util.GroovyTestSuite;
import junit.framework.*;


public class EFCTestSuite extends TestSuite {
   private static final String TEST_ROOT = "src/test/";

  public static Test suite() throws Exception {

      GroovyTestSuite gsuite = new GroovyTestSuite();
       return new JUnit4TestAdapter(gsuite.compile(TEST_ROOT + "TestTest.groovy"));
   }
}
====================================

============src/test/TestTest.groovy============
import org.junit.Test


class TestTest {
      @Test
      void additionIsWorking() {
          assert 4 == 2 + 2
      }
      @Test
      void subtractionIsWorking() {
          assert 1 == 3 - 2
      }
}
====================================

It it helps, you can explore more on these lines, may be.
-- Roshan

Jaromir D.B. Nemec

unread,
Jan 6, 2010, 1:49:13 PM1/6/10
to us...@groovy.codehaus.org

roshandawrani wrote:
>
> As far as I understand, the cause for the problem you are facing is that
> you
> are mixing JUnit 3.x and 4.x ways of defining test suite and writing
> tests.
>
> If you follow the suite.addTestSuite(<someClass>) approach, you are using
> junit 3.x approach of creating test suite. When JUnit looks for tests in
> addTestSuite() path, it does so using 3.x notations (checks whether test
> class extends TestCase and method name/signature meet the requirements,
> etc). In your case TestTest.groovy is defined using 4.x notation - it is a
> POJO, does not follow naming conventions, etc and hence JUnit cannot find
> any tests in it.
>
>
>

Unfortunately I didn’t manage the test Junit test of Groovy Scripts neither
with Junit3 nor 4. Below is my example – with Junit 3 I get “No tests found”
with Junit 4 “Test class can only have one constructor”.
Can someone correct / complete the examples?

Thanks,

Jaromir D.B. Nemec


Tested script

// GroovyOK.groovy
assert 1==1

Junit 3

// Test Groovy Script with JUnit
// RunAllScriptsTests.groovy
//
import groovy.util.GroovyTestSuite
import junit.framework.Test
import junit.textui.TestRunner

class AllTests {
static Test suite() {
def allTests = new GroovyTestSuite()

allTests.addTestSuite(allTests.compile("GroovyOK.groovy"))
return allTests
}

}

TestRunner.run(AllTests.suite())


Gives


.F
Time: 0,015
There was 1 failure:
1) warning(junit.framework.TestSuite$1)junit.framework.AssertionFailedError:
No tests found in GroovyOK
at
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at RunAllScriptsTests.run(RunAllScriptsTests.groovy:18)
at
groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:256)
at groovy.lang.GroovyShell.run(GroovyShell.java:218)
at groovy.lang.GroovyShell.run(GroovyShell.java:147)
at groovy.ui.GroovyMain.processOnce(GroovyMain.java:493)
at groovy.ui.GroovyMain.run(GroovyMain.java:308)
at groovy.ui.GroovyMain.process(GroovyMain.java:294)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:111)
at groovy.ui.GroovyMain.main(GroovyMain.java:92)
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.tools.GroovyStarter.rootLoader(GroovyStarter.java:108)
at
org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:130)

FAILURES!!!
Tests run: 1, Failures: 1, Errors: 0

JUNIT 4

// Test with JUnit4.4 (requires junit-4.8.1.jar)
// RunAllScriptsTestsJU4.groovy
// problem: Test class can only have one constructor


import groovy.util.GroovyTestSuite
import junit.framework.Test
import junit.framework.JUnit4TestAdapter

import junit.textui.TestRunner

class AllTests {
static Test suite() {
def allTests = new GroovyTestSuite()

return new JUnit4TestAdapter(allTests.compile("GroovyOK.groovy"))
}

}

TestRunner.run(AllTests.suite())

Gives


.E
Time: 0,016
There was 1 error:
1) initializationError(GroovyOK)java.lang.IllegalArgumentException: Test
class can only have one constructor
at org.junit.runners.model.TestClass.<init>(TestClass.java:34)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:65)
at
org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:59)
at
org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
at
org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at
org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at
org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at
org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at
junit.framework.JUnit4TestAdapter.<init>(JUnit4TestAdapter.java:31)
at
junit.framework.JUnit4TestAdapter.<init>(JUnit4TestAdapter.java:24)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
Method)
at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at
org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
at
org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:107)
at
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:52)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:192)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:200)
at AllTests.suite(RunAllScriptsTestsJU4.groovy:14)
at AllTests$suite.call(Unknown Source)
at
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:121)
at RunAllScriptsTestsJU4.run(RunAllScriptsTestsJU4.groovy:19)
at
groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:256)
at groovy.lang.GroovyShell.run(GroovyShell.java:218)
at groovy.lang.GroovyShell.run(GroovyShell.java:147)
at groovy.ui.GroovyMain.processOnce(GroovyMain.java:493)
at groovy.ui.GroovyMain.run(GroovyMain.java:308)
at groovy.ui.GroovyMain.process(GroovyMain.java:294)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:111)
at groovy.ui.GroovyMain.main(GroovyMain.java:92)
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.tools.GroovyStarter.rootLoader(GroovyStarter.java:108)
at
org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:130)

FAILURES!!!
Tests run: 1, Failures: 0, Errors: 1


--
View this message in context: http://old.nabble.com/GroovyTestSuite-compile-doesn%27t-support-annotated-Junit-4-test--tp23243411p27026935.html
Sent from the groovy - user mailing list archive at Nabble.com.

Roshan Dawrani

unread,
Jan 6, 2010, 4:03:52 PM1/6/10
to us...@groovy.codehaus.org
For your JUnit 3 example, you can either

1) Change your RunAllScriptsTests.groovy as below
---------------------------------------------------------------------------------------------------------------------

import groovy.util.GroovyTestSuite
import junit.framework.Test
import junit.textui.TestRunner
import org.codehaus.groovy.runtime.ScriptTestAdapter


class AllTests {
    static Test suite() {
        def allTests = new GroovyTestSuite()

        allTests.addTest(new ScriptTestAdapter(allTests.compile("GroovyOK1.groovy"), [] as String[]))

        allTests.addTest(new ScriptTestAdapter(allTests.compile("GroovyOK2.groovy"), [] as String[]))

        return allTests
    }
}

TestRunner.run(AllTests.suite())
---------------------------------------------------------------------------------------------------------------------

2) Use the GroovyTestSuite from the command line as below - one script or test at a time:

java groovy.util.GroovyTestSuite GroovyOK.groovy

HTH.
Roshan

Jaromir D.B. Nemec

unread,
Jan 6, 2010, 5:12:24 PM1/6/10
to us...@groovy.codehaus.org
Hi Roshan,
 
thanks a lot! It works fine!
I'll consider to update the wiki page http://groovy.codehaus.org/Unit+Testing based on this information!
 
Regards,
 
 
Jaromir
Reply all
Reply to author
Forward
0 new messages