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
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>Suppose we are trying to test the following application:</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 MyApp { def factory def logger def doBusinessLogic(param) { def myObj = factory.instance myObj.doSomething(param) myObj.doSomethingElse(param) logger.log('Something done with: ' + param) } } </pre></td></tr></table> <p>We might be tempted to replace logger and factory with Groovy's <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">built-in mocks</a>, but it turns out that Maps or Expandos are often sufficiently powerful enough in Groovy that we don't need full blown dynamic mocks for this example.</p> <p>Instead of a mock for logger, we can use an Expando 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> ... def logger = new Expando() logger.log = { msg -> assert msg == 'Something done with: ' + param } ... </pre></td></tr></table> <p>Here the expected behaviour for our production code is captured within a Closure. (When using TDD, this closure would force the production code we saw in our original code to be created.)</p> <p>Instead of a mock for factory, we can use a simple map 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> ... def factory = [instance: businessObj] ... </pre></td></tr></table> <p>Here, <code>businessObj</code> is the object we want the factory to return, though in general this could be a Closure similar to what we did for the Expando above.</p> <p>Putting this altogether yields (after some refactoring) the following complete test:</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 MyAppTest extends GroovyTestCase { void testDoesBusinessLogic() { // triangulate checkDoesBusinessLogic "case1" checkDoesBusinessLogic "case2" } private checkDoesBusinessLogic(param) { def logger = setUpLoggingExpectations(param) def businessObj = setUpBusinessObjectExpectations(param) def factory = [instance: businessObj] def cut = new MyApp(logger:logger, factory:factory) cut.doBusinessLogic(param) } private setUpLoggingExpectations(param) { def shouldProduceCorrectLogMessage = { msg -> assert msg == 'Something done with: ' + param } def logger = new Expando() logger.log = shouldProduceCorrectLogMessage return logger } private setUpBusinessObjectExpectations(param) { def shouldBeCalledWithInputParam = { assert it == param } def myObj = new Expando() myObj.doSomething = shouldBeCalledWithInputParam myObj.doSomethingElse = shouldBeCalledWithInputParam return myObj } } </pre></td></tr></table> <p>See also: <a class="confluence-link" href="/display/GROOVY/Developer+Testing+using+Closures+instead+of+Mocks" data-linked-resource-id="66905" data-linked-resource-type="page" data-linked-resource-default-alias="Developer Testing using Closures instead of Mocks" data-base-url="http://docs.codehaus.org">Developer Testing using Closures instead of Mocks</a></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