Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p><table class="wysiwyg-macro" data-macro-name="excerpt" data-macro-parameters="atlassian-macro-output-type=BLOCK|hidden=true" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2V4Y2VycHQ6aGlkZGVuPXRydWV8YXRsYXNzaWFuLW1hY3JvLW91dHB1dC10eXBlPUJMT0NLfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"><p>shows how Groovy may be used to simplify unit testing</p></td></tr></table></p> <h2>The Groovy Advantage</h2> <p>Groovy simplifies JUnit testing, making it more Groovy, in several ways, including:</p> <ul> <li>JUnit is built into the groovy runtime, so you can script JUnit tests for your Groovy and Java classes using Groovy syntax.</li> <li>Groovy provides many additional JUnit assertion statements (see below)</li> <li>Groovy unit tests are easily scriptable with Ant / Maven (see below)</li> <li>Groovy provides <a class="confluence-link" href="/display/GROOVY/Groovy+Mocks" data-linked-resource-id="59174" data-linked-resource-type="page" data-linked-resource-default-alias="Groovy Mocks" data-base-url="http://docs.codehaus.org">Groovy Mocks</a></li> </ul> <p>See also:</p> <ul> <li><a href="http://www-106.ibm.com/developerworks/java/library/j-pg11094">Unit test your java code with groovy</a></li> <li><a href="http://code.google.com/p/testngroove/">testngroove project page</a></li> <li><a class="confluence-link" href="/display/GROOVY/Using+JUnit+4+with+Groovy" data-linked-resource-id="75345" data-linked-resource-type="page" data-linked-resource-default-alias="Using JUnit 4 with Groovy" data-base-url="http://docs.codehaus.org">Using JUnit 4 with Groovy</a></li> </ul> <h2>Example</h2> <p>To write unit tests in Groovy, you have to create a class extending groovy.util.GroovyTestCase.</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>import groovy.util.GroovyTestCase class MyTest extends GroovyTestCase { void testSomething() { assert 1 == 1 assert 2 + 2 == 4 : "We're in trouble, arithmetic is broken" } } </pre></td></tr></table> <h2>Groovy Test Assertions</h2> <p>Apart from the <a href="http://junit.sourceforge.net/javadoc/org/junit/Assert.html">default assertion methods inherited</a> from the JUnit framework's TestCase class, GroovyTestCase also offers additional test assertions:</p> <ul> <li>assertArrayEquals(Object[] expected, Object[] value)</li> <li>assertLength(int length, char[] array)</li> <li>assertLength(int length, int[] array)</li> <li>assertLength(int length, Object[] array)</li> <li>assertContains(char expected, char[] array)</li> <li>assertContains(int expected, int[] array)</li> <li>assertToString(Object value, String expected)</li> <li>assertInspect(Object value, String expected)</li> <li>assertScript(final String script) // assert that a script runs without exceptions</li> <li>shouldFail(Closure code) // assert that an exception was thrown in that closure</li> <li>shouldFail(Class clazz, Closure code) // the same but for a class</li> </ul> <h2>Details</h2> <p>By default Groovy unit test cases generate java bytecode and so are just the same as any other Java unit test cases. One thing to watch is often Ant / Maven look for *.java files to find unit tests with pattern matching, rather than *.class files.<br /> There's an option in Maven to ensure you search for classes (and so find any Groovy unit test cases) via this property</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>maven.test.search.classdir = true </pre></td></tr></table> <p>Once you've got this enabled you can use Maven goals to run individual test cases like this</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>maven test:single -Dtestcase=foo.MyGroovyTest </pre></td></tr></table> <h2>Running GroovyTestCases on the command-line</h2> <p>Since beta-6, you can also run your groovy tests (extending GroovyTestCase) on the command-line. It has simple as launching any other Groovy script or class:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="none" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bm9uZX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>groovy MyTest.groovy </pre></td></tr></table> <h2>Running GroovyTestCases in IDEs</h2> <p>Most IDEs support JUnit but maybe don't yet handle Groovy shame!<img class="emoticon emoticon-smile" data-emoticon-name="smile" border="0" src="/s/en_GB/3278/15/_/images/icons/emoticons/smile.png" alt="(smile)" title="(smile)" /> .<br /> Firstly if you compile the groovy code to bytecode, then it'll just work in any JUnit IDE just fine.</p> <p>Sometimes though you want to just hack the unit test script and run from in your IDE without doing a build.<br /> If you're IDE doesn't automatically recompile Groovy for you then there's a utility to help you run Groovy unit test cases inside any JUnit IDE without needing to run your Ant / Maven build.</p> <p>The <a href="http://groovy.codehaus.org/apidocs/groovy/util/GroovyTestSuite.html">GroovyTestSuite</a> class is a JUnit TestSuite which will compile and run a GroovyUnit test case<br /> from a command line argument (when run as an application) or from the _<em>test</em>_ system property when run as a JUnit test suite.<br /> To run the GroovyUnitTest as an application, just do the equivalent of this in your IDE</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>java groovy.util.GroovyTestSuite src/test/Foo.groovy </pre></td></tr></table> <p>Or to run the test suite inside your IDE, just run the GroovyTestSuite test with this system property defined</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>-Dtest=src/test/Foo.groovy </pre></td></tr></table> <p>Either of the above can really help improve the development experience of writing Groovy unit test cases in IDEs that don't yet support Groovy natively.</p> <h2>Running a TestSuite containing GroovyTestCase scripts directly in Eclipse</h2> <p>You can take advantage of <a href="http://groovy.codehaus.org/apidocs/groovy/util/GroovyTestSuite.html">GroovyTestSuite</a>'s ability to compile GroovyTestCase scripts into classes to build a TestSuite which can be run from Eclipse's JUnit runner directly.<br /> The suite() method of TestSuite creates and returns a Test. Within your TestSuite's suite() method, you can create a GroovyTestSuite and use to to compile groovy scripts into Class instances, and add them to a TestSuite that you are building using TestSuite.addSuite(Class).</p> <p>Here's a TestSuite that contains some GroovyTestCase scripts:</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6amF2YX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>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/test/com/foo/bar/"; public static TestSuite suite() throws Exception { TestSuite suite = new TestSuite(); GroovyTestSuite gsuite = new GroovyTestSuite(); suite.addTestSuite(FooTest.class); // non-groovy test cases welcome, too. suite.addTestSuite(gsuite.compile(TEST_ROOT + "BarTest.groovy")); suite.addTestSuite(gsuite.compile(TEST_ROOT + "FooFactoryTest.groovy")); suite.addTestSuite(gsuite.compile(TEST_ROOT + "BaazTest.groovy")); return suite; } } </pre></td></tr></table> <p>This TestSuite subclass can then be launched as a normal TestSuite in Eclipse. For example, right-click, Run As -> JUnit Test.</p> <p>From there, the behavior of the JUnit test runner is the same; hierarchy view of all tests and individual methods, their results, etc.</p> <h2>Using normal scripts as unit test cases</h2> <p>You can write scripts like this</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>x = [1, 2, 3] assert x.size() == 3 </pre></td></tr></table> <p>and use these scripts as unit test cases if you use the GroovyTestSuite class to run them as described below.</p> <p>When the above script is compiled, it doesn't actually implement JUnit's TestCase and so needs a special runner so that it can be used inside a JUnit test framework. This is what GroovyTestSuite does, it detects scripts like the above and wraps them in a JUnit Test adapter so you can run scripts like the above as a unit test case inside your IDE.<br /> Example of a test suite to test Groovy scripts</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>// RunAllScriptsTests.groovy 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("GroovyOK.groovy"), [] as String[])) allTests.addTest(new ScriptTestAdapter(allTests.compile("GroovyOK2.groovy"), [] as String[])) return allTests } } TestRunner.run(AllTests.suite()) </pre></td></tr></table> <p>Sample output</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>groovy RunAllScriptsTests.groovy .. Time: 0,015 OK (2 tests) </pre></td></tr></table> <h2>New AllTestSuite</h2> <p>There is a new class groovy.util.AllTestSuite that transparently cares for all the above.</p> <p>Simply make a Run Configuration in your IDE for this class, providing the following System Properties:</p> <table class="confluenceTable"><tbody> <tr> <th class="confluenceTh"><p> Property name </p></th> <th class="confluenceTh"><p> meaning </p></th> <th class="confluenceTh"><p> default </p></th> </tr> <tr> <td class="confluenceTd"><p> groovy.test.dir </p></td> <td class="confluenceTd"><p> the directory to search for groovy tests </p></td> <td class="confluenceTd"><p> ./test/ </p></td> </tr> <tr> <td class="confluenceTd"><p> groovy.test.pattern </p></td> <td class="confluenceTd"><p> the ant fileset pattern to search below the dir </p></td> <td class="confluenceTd"><p> **/*Test.groovy </p></td> </tr> </tbody></table> <p>See the API documentation of groovy.util.AllTestSuite for more details.</p> <p>Here is the run config for JetBrains IDEA:</p> <p><img class="confluence-embedded-image" src="/download/attachments/231082298/AllTestSuiteSettings.JPG?version=1&modificationDate=1371611573719" data-image-src="/download/attachments/231082298/AllTestSuiteSettings.JPG?version=1&modificationDate=1371611573719" data-linked-resource-id="231377030" data-linked-resource-type="attachment" data-linked-resource-default-alias="AllTestSuiteSettings.JPG" data-base-url="http://docs.codehaus.org" data-linked-resource-container-id="231082298" title="null > AllTestSuiteSettings.JPG"></p> <p>Here is how it looks like when running the AllTestSuite for the the groovy Unit tests of the Groovy project itself:</p> <p><img class="confluence-embedded-image" src="/download/attachments/231082298/AllTestSuiteRun.JPG?version=1&modificationDate=1371611573709" data-image-src="/download/attachments/231082298/AllTestSuiteRun.JPG?version=1&modificationDate=1371611573709" data-linked-resource-id="231377029" data-linked-resource-type="attachment" data-linked-resource-default-alias="AllTestSuiteRun.JPG" data-base-url="http://docs.codehaus.org" data-linked-resource-container-id="231082298" title="null > AllTestSuiteRun.JPG"></p>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced