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><a href="https://jmockit.dev.java.net/">JMockit</a> is a single class with a small set of static methods, which allow arbitrary methods and constructors of any other class to be replaced by mock implementations at runtime. It has the following features:</p> <ul> <li>no particular design must be followed by code under test, e.g.: <ul> <li>you don't need to have interfaces everywhere</li> <li>you don't need to avoid static method calls</li> <li>you don't need to use dependency injection, i.e. you can have <code>new SomeClass()</code> calls throughout your code</li> <li>you don't need to worry about <code>final</code> classes</li> </ul> </li> <li>legacy code can be unit tested without the need for any adaptation</li> </ul> <p>Since JMockit depends on the JVM class redefinition mechanism exposed by java.lang.instrumentation, Groovy, JUnit or TestNG tests that use it must be run under a Java SE 5 VM. However, application and test code can still be compiled to older versions of the language.</p> <p>The sections below illustrate using JMockit for the mocking parts of <a class="confluence-link" href="/display/GROOVY/Using+Testing+Frameworks+with+Groovy" data-linked-resource-id="67695" data-linked-resource-type="page" data-linked-resource-default-alias="Using Testing Frameworks with Groovy" data-base-url="http://docs.codehaus.org">Using Testing Frameworks with Groovy</a>.</p> <h2>The Item Storer Example</h2> <p>We are going to consider how you might use JMockit as part of testing the <a class="confluence-link" href="/display/GROOVY/Using+Testing+Frameworks+with+Groovy#UsingTestingFrameworkswithGroovy-AnItemStorerExample" data-anchor="AnItemStorerExample" data-linked-resource-id="67695" data-linked-resource-type="page" data-linked-resource-default-alias="Using Testing Frameworks with Groovy#AnItemStorerExample" data-base-url="http://docs.codehaus.org">Item Storer Example</a>.</p> <p>First we define the following class in a file called <code>MockReverser.groovy</code>:</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 MockReverser implements Reverser { private testData = [123.456:-123.456, hello:'olleh'] def reverse(value) { testData[value] } } </pre></td></tr></table> <p>We place this in a separate file rather than straight in the same script as the instrumentation classes in the JVM (which JMockit relies upon) don't know about classes compiled in memory by Groovy. They will look for replaced classes on the classpath.</p> <p>Now, here is how we can test <code>Storer</code>:</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> // require(url:'https://jmockit.dev.java.net', jar='jmockit.jar') // require(url:'https://jmockit.dev.java.net', jar='jmockit-asm2.jar') // needs to be run with "-javaagent:jmockit.jar" import mockit.* def checkReverse(storer, value, reverseValue) { storer.put(value) assert value == storer.get() assert reverseValue == storer.getReverse() } Mockit.redefineMethods(GroovyReverser, MockReverser) def storer = new Storer() checkReverse(storer, 123.456, -123.456) checkReverse(storer, 'hello', 'olleh') </pre></td></tr></table> <p>If you recall from the example, <code>Storer</code> does a <code>new GroovyReverser()</code> call, so we use <code>Mockit.redefineMethods()</code> to replace the original version with our mock version.</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