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>Groovy has excellent support for Testing with Unit Testing (GroovyTestCase adds extra capabilities to TestCase from JUnit 3.8.2) and Mocking capabilities built right in. Currently, there are no special Groovy extensions for JUnit 4 but it's easy to use so long as you are using Groovy 1.5+ and Java 5+ (a requirement for annotations/Junit 4.x). Here are some examples.</p> <p>Make sure you are using at least Groovy 1.5 (we used 1.5.6) and JUnit 4.x (we used 4.4).</p> <h3>Simple Example</h3> <p>Let's test some of the built-in arithmetic operators in Groovy.</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 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 } } </pre></td></tr></table> <p>Alternatively, one could use the shouldFail method as follows:</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> class ArithmeticTest { final shouldFail = new GroovyTestCase().&shouldFail @Test void divideByZero() { shouldFail(ArithmeticException) { println 1/0 } } } </pre></td></tr></table> <p>Our test class includes two tests <em>additionIsWorking</em> and <em>divideByZero</em>. The second of these is expected to fail with an <em>ArithmeticException</em> exception.</p> <p>Running the test gives:</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> JUnit version 4.4 .. Time: 0.078 OK (2 tests) </pre></td></tr></table> <h3>Hamcrest Matching</h3> <p>You can use the Hamcrest matchers that come with JUnit 4.4 and above 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> import static org.junit.Assert.assertThat import static org.hamcrest.CoreMatchers.* import static org.junit.matchers.JUnitMatchers.* import org.junit.Test class LanguageTest { def languages = [tom:['Java', 'Groovy'], dick:['C#', 'Ruby']] @Test void tomKnowsJavaAndGroovyHamcrest() { assertThat languages['tom'], is(["Java", "Groovy"]) assertThat languages['tom'][0], containsString("v") assertThat languages['tom'][1], containsString("v") } @Test void tomKnowsJavaAndGroovyNative() { assert languages['tom'] == ["Java", "Groovy"] assert languages['tom'].every{ it.contains("v") } } } </pre></td></tr></table> <p>The first test uses the matchers. You can see by the second test, that native Groovy assertions are usually going to do the job just as well if not better in the Groovy world.</p> <h3>Parameterized Testing</h3> <p>JUnit 4.x includes the ability to have parameterized tests. Suppose we want to test the following program:</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> class GroovyMultiplier { int triple(int val) { return val * 3 } } </pre></td></tr></table> <p>Here is what your code might look like. This example uses parameterization.</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 org.junit.Test import org.junit.Before import org.junit.runner.RunWith import org.junit.runners.Parameterized import org.junit.runners.Parameterized.Parameters @RunWith(Parameterized) class MultiplierTest { def testee def param def expectedResult @Parameters static data() { return (2..4).collect{ [it, it * 3] as Integer[] } } MultiplierTest(a, b) { param = a expectedResult = b } @Before void setUp() { testee = new GroovyMultiplier() } @Test void positivesFixed() { assert testee.triple(1) == 3: "+ve multiplier error" } @Test void positivesParameterized() { assert testee.triple(param) == expectedResult } @Test void negativesParameterized() { assert testee.triple(-param) == -expectedResult } } </pre></td></tr></table> <p>The output will look something 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> JUnit version 4.4 ............ Time: 0.062 OK (9 tests) </pre></td></tr></table> <h3>Theory Tests</h3> <p>You can use the experimental Theory tests from JUnit 4.4 and above as follows:</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 org.junit.runner.* import org.junit.experimental.theories.* import static org.junit.Assume.assumeTrue as assume @RunWith(Theories) class LanguageTheoryTest { @DataPoint public static String java = 'Java' @DataPoint public static String ruby = 'JRuby' @DataPoint public static String python = 'Jython' @DataPoint public static String javascript = 'Rhino' @DataPoint public static String groovy = 'Groovy' @DataPoint public static String scala = 'Scala' @DataPoint public static String csharp = 'C#' def jvmLanguages = [java, ruby, python, groovy, scala, javascript] def teamSkills = [ tom: [java, groovy, ruby], dick: [csharp, scala, java, python], harry: [javascript, groovy, java] ] @Theory void everyone_knows_java() { teamSkills.each { person, skills -> assert java in skills } } @Theory void someone_knows_each_jvm_language(String language) { assume language in jvmLanguages assert teamSkills.any { person, skills -> language in skills } } @Theory void tom_knows_all_languages_ending_with_y(String language) { assume language.endsWith('y') assert language in teamSkills.tom } } </pre></td></tr></table> <p>When run, this gives:</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> JUnit version 4.4 ... Time: 0.063 OK (3 tests) </pre></td></tr></table>
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