blob: d5074667ded564752ea9c1eb083ecbf35d5f6f50 [file]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<!-- Matomo -->
<script>
var _paq = window._paq = window._paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
_paq.push(["setDoNotTrack", true]);
_paq.push(["disableCookies"]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="https://analytics.apache.org/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '79']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<!-- End Matomo Code -->
<title>3 Upgrading From The Mixin Framework 2.3.0</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" href="../css/main.css" type="text/css" media="screen, print" title="Style" charset="utf-8"/>
<link rel="stylesheet" href="../css/pdf.css" type="text/css" media="print" title="PDF" charset="utf-8"/>
<script type="text/javascript">
function addJsClass() {
var classes = document.body.className.split(" ");
classes.push("js");
document.body.className = classes.join(" ");
}
</script>
</head>
<body class="body" onload="addJsClass();">
<div id="navigation">
<div class="navTitle">
Grails Testing Support
</div>
<div class="navLinks">
<ul>
<li>
<div id="nav-summary" onmouseover="toggleNavSummary(false)" onmouseout="toggleNavSummary(true)">
<a href="../guide/index.html" class="button">Table of contents</a>
<div id="nav-summary-childs" style="display:none;">
<div class="toc-item" style="margin-left:0"><a href="../guide/introduction.html"><strong>1</strong><span>Introduction</span></a>
</div>
<div class="toc-item" style="margin-left:0"><a href="../guide/installation.html"><strong>2</strong><span>Installation</span></a>
</div>
<div class="toc-item" style="margin-left:0"><a href="../guide/upgrading.html"><strong>3</strong><span>Upgrading From The Mixin Framework</span></a>
</div>
<div class="toc-item" style="margin-left:0"><a href="../guide/unitTesting.html"><strong>4</strong><span>Unit Testing</span></a>
</div>
<div class="toc-item" style="margin-left:0"><a href="../guide/APIDocs.html"><strong>5</strong><span>API Docs</span></a>
</div>
</div>
</div>
</li>
<li class="separator selected">
<a id="ref-button" onclick="localToggle(); return false;" href="#">Quick Reference</a>
</li>
</ul>
</div>
</div>
<table id="colset" border="0" cellpadding="0" cellspacing="0">
<tr>
<td id="col1">
<div id="main" class="corner-all">
<div class="toc-item prev-left"><a href="../guide/installation.html">&lt;&lt; <strong>2</strong><span>Installation</span></a></div>
<span id='toggle-col1' class="toggle">(<a href="#" onclick="localToggle(); return false;">Quick Reference</a>)</span>
<div class="toc-item next-right"><a href="../guide/unitTesting.html"><strong>4</strong><span>Unit Testing</span> >></a></div>
<div class="project">
<h1>3 Upgrading From The Mixin Framework</h1>
<p><strong>Version:</strong> 2.3.0</p>
</div>
<h1 id="upgrading">3 Upgrading From The Mixin Framework</h1>
<div class='contribute-btn'>
<button type='button' class='btn btn-default' onclick='window.location.href="https://github.com/grails/grails-testing-support/edit/master/src/main/docs/guide/upgrading.adoc"'>
<i class='fa fa-pencil-square-o'></i> Improve this doc
</button>
</div>
<div class="paragraph">
<p>This library was designed to be compatible with the old mixin approach. There are some slight differences that you may encounter, however any required changes should be minimal.</p>
</div>
<div class="sect2">
<h3 id="_freshruntime_removed">FreshRuntime Removed</h3>
<div class="paragraph">
<p>The <code>@FreshRuntime</code> annotation was removed due to the design of the new framework. The annotation allowed any metaclass changes to be sandboxed either at the method or class level. Spock provides a similar annotation to do the same thing, <a href="http://spockframework.org/spock/docs/1.1/all_in_one.html#_confinemetaclasschanges">@ConfineMetaClassChanges</a>.</p>
</div>
<div class="paragraph">
<p>In addition, the annotation caused the application context to be refreshed. Because the application context is refreshed between test classes automatically, the annotation is no longer necessary.</p>
</div>
</div>
<div class="sect2">
<h3 id="_integration_tests">Integration Tests</h3>
<div class="paragraph">
<p>The <code>@Integration</code> annotation was copied to this library so a dependency on the old framework is no longer necessary. The package has changed from <code>grails.test.mixin.integration.Integration</code> to <code>grails.testing.mixin.integration.Integration</code>. Everything about integration tests should work the same as before.</p>
</div>
</div>
<div class="sect2">
<h3 id="_example_converted_test">Example Converted Test</h3>
<div class="paragraph">
<p>Here is an example test that may look like something in your project.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="annotation">@TestFor</span>(AuthorController)
<span class="annotation">@Mock</span>(Author)
<span class="type">class</span> <span class="class">AuthorControllerTests</span> {
<span class="annotation">@Test</span>
<span class="type">void</span> testIndex() {
controller.index()
<span class="keyword">assert</span> response.text == <span class="string"><span class="delimiter">'</span><span class="content">Hello</span><span class="delimiter">'</span></span>
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>To convert this test to the new framework, we must update it to use Spock as well as change the annotation usages to trait usages.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">spock.lang.Specification</span>
<span class="keyword">import</span> <span class="include">grails.testing.gorm.DomainUnitTest</span>
<span class="keyword">import</span> <span class="include">grails.testing.web.controllers.ControllerUnitTest</span>
<span class="type">class</span> <span class="class">AuthorControllerTests</span> <span class="directive">extends</span> Specification <span class="directive">implements</span> ControllerUnitTest&lt;AuthorController&gt;, DomainUnitTest&lt;Author&gt; {
<span class="type">void</span> testIndex() {
<span class="key">when</span>:
controller.index()
<span class="key">then</span>:
response.text == <span class="string"><span class="delimiter">'</span><span class="content">Hello</span><span class="delimiter">'</span></span>
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Obviously there are many use cases you may want to convert and it would be impossible to cover them all in this documentation. As a part of the testing of this new framework, all of the usages of the old framework were replaced in Grails core. You can view those changes by looking at <a href="https://github.com/grails/grails-core/commit/8c4f07743108bf1e4e5f69dbb57695cce0b2518d">this commit</a> to see the vast majority of examples you will need.</p>
</div>
</div>
<div style="clear:both;margin-top:15px;"></div>
<div class="toc-item prev-left"><a href="../guide/installation.html">&lt;&lt; <strong>2</strong><span>Installation</span></a></div>
<div class="toc-item next-right"><a href="../guide/unitTesting.html"><strong>4</strong><span>Unit Testing</span> >></a></div>
<div style="clear:both"></div>
</div>
</td>
<td id="col2">
<div class="local clearfix">
<div class="local-title">
<a href="../guide/index.html" target="mainFrame">Quick Reference</a>
<span class="toggle">(<a href="#" onclick="localToggle(); return false;">hide</a>)</span>
</div>
<div class="menu">
</div>
</div>
</td>
</tr>
</table>
<div id="footer">
</div>
<script type="text/javascript" src="../js/docs.js"></script>
</body>
</html>