<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Luminis Software Development &#187; jmockit</title>
	<atom:link href="http://lsd.luminis.nl/tag/jmockit/feed/" rel="self" type="application/rss+xml" />
	<link>http://lsd.luminis.nl</link>
	<description></description>
	<lastBuildDate>Wed, 18 Aug 2010 10:10:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>nl</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Xtend Mocking and Unit Testing</title>
		<link>http://lsd.luminis.nl/xtend-mocking-and-unit-testing/</link>
		<comments>http://lsd.luminis.nl/xtend-mocking-and-unit-testing/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 18:24:59 +0000</pubDate>
		<dc:creator>Alexander Broekhuis</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[jmockit]]></category>
		<category><![CDATA[mda]]></category>

		<guid isPermaLink="false">http://lsd.luminis.net/?p=187</guid>
		<description><![CDATA[<img class="thumbnail" src="http://blog.luminis.nl/luminis/resource/thumbnails/xtendunittesting_thumb.png">
With the introduction of oAW and modeling, new scripting languages are used to transform the models into different artifacts (models and/or code). One of these languages is XTend. With XTend it is possible to write so called extensions that can be used to query a model for information, but also to create/change/update information with new/additional information.
With the growing number of extensions, there was a need for a method to test these. A logical solution is to create unit tests that can be used for regression testing.]]></description>
			<content:encoded><![CDATA[<p>
Back in april last year we started looking into MDA and model transformations.<br />
During a small research period we started using oAW as tool for this. Using oAW a<br />
transformation is written for a large code generation framework.
</p>
<p>
With the introduction of oAW and modeling, new scripting languages are used to<br />
transform the models into different artifacts (models and/or code).<br />
The functional language of oAW is XTend. With XTend it is possible to write so<br />
called extensions that can be used to query a model for information,<br />
but also to create/change/update information with new/additional information.</p>
<p>
Using a custom meta-model, or UML in combination with profiles, and a transformation,<br />
it is possible to transform the model to any required artifact. OAW is used to<br />
transform a UML model, marked with stereotypes, to XML. The XML is created using the XSD MetaModel support of oAW.
</p>
<p>
With the growing number of extensions, there was a need for a method to test these.<br />
A logical solution is to create unit tests that can be used for regression testing.
</p>
<p>This post describes a flexible way to write unit tests for extensions, including<br />
mocking with EasyMock and Jmockit. Since this post is very technical, it is<br />
assumed that the reader is familiar with oAW, UML and unit testing. More<br />
information about oAW, EMF and Mocking can be found in the resources listed below the post.
</p>
<h3>Requirements</h3>
<p>To be able to perform flexible and complete unit tests there are several requirements:</p>
<ul>
<li>Be able to call extensions from the unit test</li>
<p>Extensions are written in XTend. To be able to unit test them, it must be possible<br />
to call extensions from Java.</p>
<li>Be able to mock extensions</li>
<p>Extension heavily rely on other extension for querying or transforming.<br />
With a unit test only the called extension must be tested. Each included extension<br />
should have a test case of itself. So it must be possible to mock extensions and<br />
be able to define the behavior inside the test case.
</ul>
</p>
<h3>Approach</h3>
<p>
The following image shows an overview of the set up used to test extensions.
</p>
<p><img src="../resource/alexanderb/overview.png" /></p>
<p>
To realize this set up, the following approach is used:</p>
<ul>
<li>Call extensions from Java</li>
<p>Extensions can be called from Java with a so called XtendFacade (link to oAW api).<br />
This class will be used in the unit tests.</p>
<li>Mock extensions</li>
<p>Mocking extensions involves some more work.</p>
<ul>
<li>Advices</li>
<p>At first it must be possible to override the extensions that are called by the<br />
extension to be tested. oAW includes AOP support, with which it is possible to<br />
define advices that override extensions.</p>
<li>XtendFacade</li>
<p>The facade that is part of oAW does not have support for advices. This<br />
functionality must be added.</p>
<li>Mocks</li>
<p>With advices and a custom facade it is possible to create stubs in which fixed<br />
values can be specified. For functional testing it must be possible to mock<br />
these stubs. To be able to do this JAVA extensions are used. From the advices a<br />
java extension is called. The java method invoked by the extension can be mocked.
</ul>
</ul>
<h4>XtendFacade</h4>
<p>An own implementation of the facade, with which it is possible to add advices is created. The advice argument must be a string pointing to the ext file with the advices.<br />
The advices are registered to the context so that they are handled during the invocation of extensions.</p>
<pre>
public final void addAdvice(String advice) {
  ctx.registerExtensionAdvices(advice.trim());
}
</pre>
<h4>Advices</h4>
<p>Now that it is possible to add advices to the Facade, it is possible to override extensions</p>
<pre>
MyXtendFacade f = createFacade("net::luminis::extensions::activator");
f.addAdvice("net::luminis::test::advices::createActivator");
</pre>
<h4>Stubs</h4>
<p>The overridden extensions can be used as stubs. Controlled responses can be declared, and helper extensions can be added to check/validate input.</p>
<pre>
around net::luminis::extensions::activator::createActivator(*):
  // test behavior
</pre>
<h3>Mocks</h3>
<p>Mocks are more flexible and give more/better validation options.<br />
For an interesting read, see an article of Martin Fowler linked at the end of this post.<br />
The original extension can&#8217;t be mocked, this is solved by using the overridden extensions.<br />
Since there is no mocking library for XTend, the overridden functions must forward their invocation to a java method.<br />
For each extension a java method is created. This method is called from the overridden extension.</p>
<pre>
Class test_createActivator():
    JAVA net.luminis.test.helpers.Helper.test_createActivator();

around net::luminis::extensions::activator::createActivator(*):
    test_createActivator();
</pre>
<h4>EasyMock</h4>
<p>Since the java method is static, and can&#8217;t easily be mocked, an extra interface is used.<br />
The class containing the static methods has a public static field of the interface.<br />
The interface declares a method for each static method. From the unit test this field<br />
can be assigned during the setup phase. The interface method is called from the static method.<br />
Now the interface can be used, in combination with eg EasyMock, to mock extensions.</p>
<p>To create the mock:</p>
<pre>
helperMock = createMock(HelperFactory.class);
Helper.f = helperMock;
</pre>
<p>Use the mock:</p>
<pre>
// record the expected calls
expect(helperMock.test_createActivator()).andReturn(returnValue);
replay(helperMock);
// run tests
f.call("addActivator", parameter1, parameter2);
// assert results
assert...(...);
// verify the mock
verify(helperMock);
</pre>
<h4>JMockit</h4>
<p>As an alternative, another library can be used. While most mock libraries do not<br />
support mocking static methods, this is possible with JMockit by instrumenting the<br />
code (requires java 1.5 and up). An agent is added to the JVM that performs the instrumentation.<br />
In the test case the static method can be mocked, and a controlled response can be given for each method.</p>
<p>Create the mock:</p>
<pre>
helperMock = createMock(HelperFactory.class);
Helper.f = helperMock;
</pre>
<p>Use the mock:</p>
<pre>
// define the expected behavior
new Expectations(true) {
  Helper mock;

  {
    mock.test_primitiveTypesModel();
    returns(model);

    mock.test_createPackageImport();
    returns(packageImport);

    mock.test_createActivator();
    returns(activator);
  }
};
// run tests
f.call("addActivator", parameter1, parameter2);
</pre>
<p>In the case of EasyMock some more work is needed to set up the java methods that are<br />
called from the extensions. But the testing code is easier to create/update.<br />
EasyMock also has more options with regards to behavioral checking. Furthermore,<br />
EasyMock has a larger user base, so more people will likely be known with it<br />
compared to JMockit.</p>
<h3>Conclusion</h3>
<p>Using the proposed method it is possible to write unit tests for oAW extensions.<br />
These unit tests can be integrated in continuous build environments, so that it is<br />
possible to have proper validation of a transformation.<br />
This post does not handle the creation of test models (UML and/or XSD based),<br />
but there are enough sources to find more information about the use of EMF<br />
(which is used, by oAW, to load models) in general.</p>
<h3>Links/Resources</h3>
<ul>
<li><a href="http://www.openarchitectureware.org">oAW</a></li>
<li><a href="http://www.eclipse.org/emf">EMF</a></li>
<li><a href="https://jmockit.dev.java.net">JMockit</a></li>
<li><a href="http://www.easymock.org">Easymock</a></li>
<li><a href="http://martinfowler.com/articles/mocksArentStubs.html">Martin Fowler about stubs and mocks</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://lsd.luminis.nl/xtend-mocking-and-unit-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
