<?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>Paul Butcher &#187; Software Engineering</title>
	<atom:link href="http://www.paulbutcher.com/category/softwareengineering/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.paulbutcher.com</link>
	<description></description>
	<lastBuildDate>Sun, 08 Jan 2012 00:33:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>ScalaMock 2.1</title>
		<link>http://www.paulbutcher.com/2011/11/scalamock-2-1/</link>
		<comments>http://www.paulbutcher.com/2011/11/scalamock-2-1/#comments</comments>
		<pubDate>Thu, 24 Nov 2011 14:30:06 +0000</pubDate>
		<dc:creator>paul</dc:creator>
				<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://www.paulbutcher.com/?p=1192</guid>
		<description><![CDATA[I&#8217;ve just released ScalaMock 2.1, with the following improvements over 2.0: Add support for polymorphic (type parameterised) methods Add support for curried methods Fix String constants in Java classes Fix unmocked operators ScalaMock is a native Scala mocking framework which, as well as traits (interfaces) and functions, can also mock: Classes Singleton and companion objects [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just released <a href="http://scalamock.org/">ScalaMock</a> 2.1, with the following improvements over 2.0:</p>
<ul>
<li>Add support for polymorphic (type parameterised) methods</li>
<li>Add support for curried methods</li>
<li>Fix String constants in Java classes</li>
<li>Fix unmocked operators</li>
</ul>
<p>ScalaMock is a native Scala mocking framework which, as well as traits (interfaces) and functions, can also mock:</p>
<ul>
<li>Classes</li>
<li>Singleton and companion objects (static methods)</li>
<li>Object creation (constructor invocation)</li>
<li>Polymorphic (type parameterised) methods</li>
<li>Classes with private constructors</li>
<li>Final classes and classes with final methods</li>
<li>Operators (methods with symbolic names)</li>
<li>Overloaded methods</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.paulbutcher.com/2011/11/scalamock-2-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ScalaMock step-by-step</title>
		<link>http://www.paulbutcher.com/2011/11/scalamock-step-by-step/</link>
		<comments>http://www.paulbutcher.com/2011/11/scalamock-step-by-step/#comments</comments>
		<pubDate>Sun, 06 Nov 2011 22:57:11 +0000</pubDate>
		<dc:creator>paul</dc:creator>
				<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://www.paulbutcher.com/?p=1110</guid>
		<description><![CDATA[ScalaMock (previously Borachio) is a mocking library for Scala. As well as traits (interfaces) and functions, it can also mock: Classes Singleton and companion objects (static methods) Object creation (constructor invocation) Classes with private constructors Final classes and classes with final methods Operators (methods with symbolic names) Overloaded methods This post describes how to setup [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://scalamock.org/">ScalaMock</a> (previously <a href="http://borachio.com/">Borachio</a>) is a mocking library for Scala. As well as traits (interfaces) and functions, it can also mock:</p>
<ul>
<li>Classes</li>
<li>Singleton and companion objects (static methods)</li>
<li>Object creation (constructor invocation)</li>
<li>Classes with private constructors</li>
<li>Final classes and classes with final methods</li>
<li>Operators (methods with symbolic names)</li>
<li>Overloaded methods</li>
</ul>
<p>This post describes how to setup a project that uses ScalaMock in conjunction with <a href="http://scalatest.org/">ScalaTest</a> and <a href="https://github.com/harrah/xsbt/wiki">sbt 0.11</a>. The sample code described in this article is available <a href="https://github.com/paulbutcher/ScalaMockExample">on GitHub</a>.</p>
<p><strong>Note: If you only want to mock functions and traits (interfaces), you can use ScalaMock&#8217;s proxy mocks by simply linking with the .jar file &#8211; no need to use the compiler plugin or sbt plugin described below.</strong></p>
<p>The example assumes that we&#8217;re writing code to control a mechanical turtle, similar to that used by <a href="http://en.wikipedia.org/wiki/Logo_(programming_language)">Logo</a> programs. Mocking is useful in this kind of situation because we might want to create tests that function even if we don&#8217;t have the hardware to hand, which run more quickly than would be the case if we ran on real hardware, and where we can use mocks to simulate errors or other situations difficult to reproduce on demand.</p>
<h3>Getting started</h3>
<ol>
<li>Create a root directory for your project:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">mkdir</span> myproject</pre></div></div>

</li>
<li>ScalaMock uses a compiler plugin to generate code. There&#8217;s an <a href="http://github.com/paulbutcher/scalamock-sbt-plugin">sbt plugin</a> that makes setting this up easy. Add this plugin to your project by creating <code>project/project/Build.scala</code> containing:

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> sbt.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">object</span> PluginDef <span style="color: #0000ff; font-weight: bold;">extends</span> Build <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">override</span> lazy <span style="color: #0000ff; font-weight: bold;">val</span> projects <span style="color: #000080;">=</span> Seq<span style="color: #F78811;">&#40;</span>root<span style="color: #F78811;">&#41;</span>
  lazy <span style="color: #0000ff; font-weight: bold;">val</span> root <span style="color: #000080;">=</span> Project<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;plugins&quot;</span>, file<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;.&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> dependsOn<span style="color: #F78811;">&#40;</span>scalamockPlugin<span style="color: #F78811;">&#41;</span>
  lazy <span style="color: #0000ff; font-weight: bold;">val</span> scalamockPlugin <span style="color: #000080;">=</span> uri<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;git://github.com/paulbutcher/scalamock-sbt-plugin&quot;</span><span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

</li>
<li>Create <code>project/Build.scala</code> containing:

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> sbt.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> Keys.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> ScalaMockPlugin.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> MyBuild <span style="color: #0000ff; font-weight: bold;">extends</span> Build <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">override</span> lazy <span style="color: #0000ff; font-weight: bold;">val</span> settings <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">super</span>.<span style="color: #000000;">settings</span> ++ Seq<span style="color: #F78811;">&#40;</span>
    organization <span style="color: #000080;">:=</span> <span style="color: #6666FF;">&quot;com.example&quot;</span>,
    version <span style="color: #000080;">:=</span> <span style="color: #6666FF;">&quot;1.0&quot;</span>,
    scalaVersion <span style="color: #000080;">:=</span> <span style="color: #6666FF;">&quot;2.9.1&quot;</span>,
&nbsp;
    resolvers +<span style="color: #000080;">=</span> ScalaToolsSnapshots,
    libraryDependencies +<span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;org.scalamock&quot;</span> <span style="color: #000080;">%%</span> <span style="color: #6666FF;">&quot;scalamock-scalatest-support&quot;</span> <span style="color: #000080;">%</span> <span style="color: #6666FF;">&quot;2.0-SNAPSHOT&quot;</span>,
    autoCompilerPlugins <span style="color: #000080;">:=</span> <span style="color: #0000ff; font-weight: bold;">true</span>,
    addCompilerPlugin<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;org.scalamock&quot;</span> <span style="color: #000080;">%%</span> <span style="color: #6666FF;">&quot;scalamock-compiler-plugin&quot;</span> <span style="color: #000080;">%</span> <span style="color: #6666FF;">&quot;2.0-SNAPSHOT&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  lazy <span style="color: #0000ff; font-weight: bold;">val</span> myproject <span style="color: #000080;">=</span> Project<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;MyProject&quot;</span>, file<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;.&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> settings<span style="color: #F78811;">&#40;</span>generateMocksSettings<span style="color: #000080;">:</span> <span style="color: #000080;">_*</span><span style="color: #F78811;">&#41;</span> configs<span style="color: #F78811;">&#40;</span>Mock<span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

</li>
</ol>
<h3>Simple mocking</h3>
<ol start="4">
<li>Now we&#8217;ve got a project, we need some code to test. Let&#8217;s start with a simple trait representing a turtle. Create <code>src/main/scala/Turtle.scala</code> containing:

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> com.<span style="color: #000000;">example</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">trait</span> Turtle <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> penDown<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> penUp<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> forward<span style="color: #F78811;">&#40;</span>distance<span style="color: #000080;">:</span> Double<span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> turn<span style="color: #F78811;">&#40;</span>angle<span style="color: #000080;">:</span> Double<span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> getPosition<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>Double, Double<span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> getAngle<span style="color: #000080;">:</span> Double
<span style="color: #F78811;">&#125;</span></pre></div></div>

</li>
<li>The turtle API is not very convenient, we have no way to move to a specific position, instead we need to work out how to get from where we are now to where we want to get by calculating angles and distances. Here&#8217;s some code that draws a line from a specific point to another by doing exactly that.
<p>Create <code>src/main/scala/Controller.scala</code> containing:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> com.<span style="color: #000000;">example</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">math</span>.<span style="color: #F78811;">&#123;</span>atan2, sqrt<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">class</span> Controller<span style="color: #F78811;">&#40;</span>turtle<span style="color: #000080;">:</span> Turtle<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> drawLine<span style="color: #F78811;">&#40;</span>start<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>Double, Double<span style="color: #F78811;">&#41;</span>, end<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>Double, Double<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    moveTo<span style="color: #F78811;">&#40;</span>start<span style="color: #F78811;">&#41;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">val</span> initialAngle <span style="color: #000080;">=</span> turtle.<span style="color: #000000;">getAngle</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> deltaPos <span style="color: #000080;">=</span> delta<span style="color: #F78811;">&#40;</span>start, end<span style="color: #F78811;">&#41;</span>
&nbsp;
    turtle.<span style="color: #000000;">turn</span><span style="color: #F78811;">&#40;</span>angle<span style="color: #F78811;">&#40;</span>deltaPos<span style="color: #F78811;">&#41;</span> - initialAngle<span style="color: #F78811;">&#41;</span>
    turtle.<span style="color: #000000;">penDown</span>
    turtle.<span style="color: #000000;">forward</span><span style="color: #F78811;">&#40;</span>distance<span style="color: #F78811;">&#40;</span>deltaPos<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> delta<span style="color: #F78811;">&#40;</span>pos1<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>Double, Double<span style="color: #F78811;">&#41;</span>, pos2<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>Double, Double<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> 
    <span style="color: #F78811;">&#40;</span>pos2.<span style="color: #000080;">_</span>1 - pos1.<span style="color: #000080;">_</span>1, pos2.<span style="color: #000080;">_</span>2 - pos1.<span style="color: #000080;">_</span>2<span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> distance<span style="color: #F78811;">&#40;</span>delta<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>Double, Double<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> 
    sqrt<span style="color: #F78811;">&#40;</span>delta.<span style="color: #000080;">_</span>1 <span style="color: #000080;">*</span> delta.<span style="color: #000080;">_</span>1 + delta.<span style="color: #000080;">_</span>2 <span style="color: #000080;">*</span> delta.<span style="color: #000080;">_</span>2<span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> angle<span style="color: #F78811;">&#40;</span>delta<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>Double, Double<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> 
    atan2<span style="color: #F78811;">&#40;</span>delta.<span style="color: #000080;">_</span>2, delta.<span style="color: #000080;">_</span>1<span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> moveTo<span style="color: #F78811;">&#40;</span>pos<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>Double, Double<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> initialPos <span style="color: #000080;">=</span> turtle.<span style="color: #000000;">getPosition</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> initialAngle <span style="color: #000080;">=</span> turtle.<span style="color: #000000;">getAngle</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">val</span> deltaPos <span style="color: #000080;">=</span> delta<span style="color: #F78811;">&#40;</span>initialPos, pos<span style="color: #F78811;">&#41;</span>
&nbsp;
    turtle.<span style="color: #000000;">penUp</span>
    turtle.<span style="color: #000000;">turn</span><span style="color: #F78811;">&#40;</span>angle<span style="color: #F78811;">&#40;</span>deltaPos<span style="color: #F78811;">&#41;</span> - initialAngle<span style="color: #F78811;">&#41;</span>
    turtle.<span style="color: #000000;">forward</span><span style="color: #F78811;">&#40;</span>distance<span style="color: #F78811;">&#40;</span>deltaPos<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

</li>
<li>In order to create mock turtles, we need to tell ScalaMock to generate the appropriate code by using the <code>@mock</code> annotation. Create <code>src/generate-mocks/scala/GenerateMocks.scala</code> containing:

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> com.<span style="color: #000000;">example</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">scalamock</span>.<span style="color: #000000;">annotation</span>.<span style="color: #000000;">mock</span>
&nbsp;
<span style="color: #000080;">@</span>mock<span style="color: #F78811;">&#91;</span>Turtle<span style="color: #F78811;">&#93;</span>
<span style="color: #0000ff; font-weight: bold;">class</span> Dummy</pre></div></div>

</li>
<li>We can now write a test. We&#8217;ll create a mock turtle that pretends to start at the origin (0, 0) and verifies that if we draw a line from (1, 1) to (2, 1) it performs the correct sequence of turns and movements.
<p><img src="http://www.paulbutcher.com/wp-content/uploads/2011/10/Turtle.png" alt="Turtle diagram" /></p>
<p>Create <code>src/test/scala/ControllerTest.scala</code> containing:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> com.<span style="color: #000000;">example</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">scalatest</span>.<span style="color: #000000;">FunSuite</span>
<span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">scalamock</span>.<span style="color: #000000;">scalatest</span>.<span style="color: #000000;">MockFactory</span>
<span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">scalamock</span>.<span style="color: #000000;">generated</span>.<span style="color: #000000;">GeneratedMockFactory</span>
<span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">math</span>.<span style="color: #F78811;">&#123;</span>Pi, sqrt<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">class</span> ControllerTest <span style="color: #0000ff; font-weight: bold;">extends</span> FunSuite <span style="color: #0000ff; font-weight: bold;">with</span> MockFactory <span style="color: #0000ff; font-weight: bold;">with</span> GeneratedMockFactory <span style="color: #F78811;">&#123;</span>
&nbsp;
  test<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;draw line&quot;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> mockTurtle <span style="color: #000080;">=</span> mock<span style="color: #F78811;">&#91;</span>Turtle<span style="color: #F78811;">&#93;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> controller <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Controller<span style="color: #F78811;">&#40;</span>mockTurtle<span style="color: #F78811;">&#41;</span>
&nbsp;
    inSequence <span style="color: #F78811;">&#123;</span>
      inAnyOrder <span style="color: #F78811;">&#123;</span>
        mockTurtle.<span style="color: #000000;">expects</span>.<span style="color: #000000;">penUp</span>
        mockTurtle.<span style="color: #000000;">expects</span>.<span style="color: #000000;">getPosition</span> returning <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0.0</span>, <span style="color: #F78811;">0.0</span><span style="color: #F78811;">&#41;</span>
        mockTurtle.<span style="color: #000000;">expects</span>.<span style="color: #000000;">getAngle</span> returning <span style="color: #F78811;">0.0</span>
      <span style="color: #F78811;">&#125;</span>
      mockTurtle.<span style="color: #000000;">expects</span>.<span style="color: #000000;">turn</span><span style="color: #F78811;">&#40;</span>~<span style="color: #F78811;">&#40;</span>Pi / <span style="color: #F78811;">4</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
      mockTurtle.<span style="color: #000000;">expects</span>.<span style="color: #000000;">forward</span><span style="color: #F78811;">&#40;</span>~sqrt<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">2.0</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
      mockTurtle.<span style="color: #000000;">expects</span>.<span style="color: #000000;">getAngle</span> returning Pi / <span style="color: #F78811;">4</span>
      mockTurtle.<span style="color: #000000;">expects</span>.<span style="color: #000000;">turn</span><span style="color: #F78811;">&#40;</span>~<span style="color: #F78811;">&#40;</span>-Pi / <span style="color: #F78811;">4</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
      mockTurtle.<span style="color: #000000;">expects</span>.<span style="color: #000000;">penDown</span>
      mockTurtle.<span style="color: #000000;">expects</span>.<span style="color: #000000;">forward</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1.0</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
&nbsp;
    controller.<span style="color: #000000;">drawLine</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1.0</span>, <span style="color: #F78811;">1.0</span><span style="color: #F78811;">&#41;</span>, <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">2.0</span>, <span style="color: #F78811;">1.0</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>This should (hopefully!) be self-explanatory, with one possible exception. The tilde (<code>~</code>) operator represents an <em>epsilon match</em>, useful for taking account of rounding errors when dealing with floating-point values.</li>
<li>Generate mocks with <code>generate-mocks</code> and then run the tests with <code>test</code>:

<div class="wp_syntax"><div class="code"><pre class="none" style="font-family:monospace;">$ sbt
&gt; generate-mocks
[log generatemocks] Creating mock for: trait Turtle
&gt; test
[info] ControllerTest:
[info] - draw line
[info] Passed: : Total 1, Failed 0, Errors 0, Passed 1, Skipped 0</pre></div></div>

</ol>
<h3>Getting clever: Constructors and singleton objects</h3>
<ol start="9">
<li>Turtles use ink. Let&#8217;s define a singleton object that keeps track of how much we&#8217;ve used.
<p>Create <code>src/main/scala/InkReservoir.scala</code> containing:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> com.<span style="color: #000000;">example</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> InkReservoir <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> use<span style="color: #F78811;">&#40;</span>r<span style="color: #000080;">:</span> Double, g<span style="color: #000080;">:</span> Double, b<span style="color: #000080;">:</span> Double<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    red -<span style="color: #000080;">=</span> r
    green -<span style="color: #000080;">=</span> g
    blue -<span style="color: #000080;">=</span> b
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">var</span> red <span style="color: #000080;">=</span> <span style="color: #F78811;">10.0</span>
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">var</span> green <span style="color: #000080;">=</span> <span style="color: #F78811;">10.0</span>
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">var</span> blue <span style="color: #000080;">=</span> <span style="color: #F78811;">10.0</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

</li>
<li>And now let&#8217;s create a concrete implementation of our <code>Turtle</code> trait, which adds the ability to define a pen colour, together with a factory method implemented in its companion object.
<p>Create <code>src/main/scala/ColouredTurtle.scala</code> containing:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> com.<span style="color: #000000;">example</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">math</span>.<span style="color: #F78811;">&#123;</span>cos, sin<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">class</span> ColouredTurtle<span style="color: #F78811;">&#40;</span>r<span style="color: #000080;">:</span> Double, g<span style="color: #000080;">:</span> Double, b<span style="color: #000080;">:</span> Double<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">extends</span> Turtle <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> penDown<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span> penIsDown <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">true</span> <span style="color: #F78811;">&#125;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> penUp<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span> penIsDown <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">false</span> <span style="color: #F78811;">&#125;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> turn<span style="color: #F78811;">&#40;</span>angle<span style="color: #000080;">:</span> Double<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span> theta +<span style="color: #000080;">=</span> angle<span style="color: #F78811;">&#125;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> getPosition<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>Double, Double<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span>x, y<span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> getAngle<span style="color: #000080;">:</span> Double <span style="color: #000080;">=</span> theta
  <span style="color: #0000ff; font-weight: bold;">def</span> forward<span style="color: #F78811;">&#40;</span>d<span style="color: #000080;">:</span> Double<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span> 
    x +<span style="color: #000080;">=</span> sin<span style="color: #F78811;">&#40;</span>theta<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">*</span> d
    y +<span style="color: #000080;">=</span> cos<span style="color: #F78811;">&#40;</span>theta<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">*</span> d
    <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>penIsDown<span style="color: #F78811;">&#41;</span>
      InkReservoir.<span style="color: #000000;">use</span><span style="color: #F78811;">&#40;</span>r <span style="color: #000080;">*</span> d, g <span style="color: #000080;">*</span> d, b <span style="color: #000080;">*</span> d<span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">var</span> penIsDown <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">false</span>
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">var</span> x <span style="color: #000080;">=</span> <span style="color: #F78811;">0.0</span>
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">var</span> y <span style="color: #000080;">=</span> <span style="color: #F78811;">0.0</span>
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">var</span> theta <span style="color: #000080;">=</span> <span style="color: #F78811;">0.0</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> ColouredTurtle <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> apply<span style="color: #F78811;">&#40;</span>colour<span style="color: #000080;">:</span> Symbol<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> <span style="color: #F78811;">&#40;</span>r, g, b<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> colourMap<span style="color: #F78811;">&#40;</span>colour<span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">new</span> ColouredTurtle<span style="color: #F78811;">&#40;</span>r, g, b<span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">val</span> colourMap <span style="color: #000080;">=</span> Map<span style="color: #F78811;">&#40;</span><span style="color: #CC66FF;">'red</span> -<span style="color: #000080;">&gt;</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1.0</span>, <span style="color: #F78811;">0.0</span>, <span style="color: #F78811;">0.0</span><span style="color: #F78811;">&#41;</span>, <span style="color: #CC66FF;">'green</span> -<span style="color: #000080;">&gt;</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0.0</span>, <span style="color: #F78811;">1.0</span>, <span style="color: #F78811;">0.0</span><span style="color: #F78811;">&#41;</span>, <span style="color: #CC66FF;">'blue</span> -<span style="color: #000080;">&gt;</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0.0</span>, <span style="color: #F78811;">0.0</span>, <span style="color: #F78811;">1.0</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

</li>
<li>To mock <code>ColouredTurtle</code>, first we need to add another <code>@mock</code> annotation to <code>GenerateMocks.scala</code>:

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #000080;">@</span>mock<span style="color: #F78811;">&#91;</span>ColouredTurtle<span style="color: #F78811;">&#93;</span></pre></div></div>

</li>
<li>We can now write a test to verify that our factory method works.
<p>Create <code>src/test/scala/ColouredTurtleTest.scala</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> com.<span style="color: #000000;">example</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">scalatest</span>.<span style="color: #000000;">FunSuite</span>
<span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">scalamock</span>.<span style="color: #000000;">scalatest</span>.<span style="color: #000000;">MockFactory</span>
<span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">scalamock</span>.<span style="color: #000000;">generated</span>.<span style="color: #000000;">GeneratedMockFactory</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">class</span> ColouredTurtleTest <span style="color: #0000ff; font-weight: bold;">extends</span> FunSuite <span style="color: #0000ff; font-weight: bold;">with</span> MockFactory <span style="color: #0000ff; font-weight: bold;">with</span> GeneratedMockFactory <span style="color: #F78811;">&#123;</span>
&nbsp;
  test<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;coloured turtles&quot;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> m1 <span style="color: #000080;">=</span> mock<span style="color: #F78811;">&#91;</span>ColouredTurtle<span style="color: #F78811;">&#93;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> m2 <span style="color: #000080;">=</span> mock<span style="color: #F78811;">&#91;</span>ColouredTurtle<span style="color: #F78811;">&#93;</span>
&nbsp;
    m1.<span style="color: #000000;">expects</span>.<span style="color: #000000;">newInstance</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1.0</span>, <span style="color: #F78811;">0.0</span>, <span style="color: #F78811;">0.0</span><span style="color: #F78811;">&#41;</span>
    m2.<span style="color: #000000;">expects</span>.<span style="color: #000000;">newInstance</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0.0</span>, <span style="color: #F78811;">1.0</span>, <span style="color: #F78811;">0.0</span><span style="color: #F78811;">&#41;</span>
    m2.<span style="color: #000000;">expects</span>.<span style="color: #000000;">forward</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">3.0</span><span style="color: #F78811;">&#41;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">val</span> t1 <span style="color: #000080;">=</span> ColouredTurtle<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">'red)
    val t2 = ColouredTurtle('</span>green<span style="color: #F78811;">&#41;</span>
    t2.<span style="color: #000000;">forward</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">3.0</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

</li>
<li>Run the tests again (don&#8217;t forget to run <code>generate-mocks</code> first) to see:

<div class="wp_syntax"><div class="code"><pre class="none" style="font-family:monospace;">[info] ColouredTurtleTest:
[info] - coloured turtles
[info] ControllerTest:
[info] - draw line
[info] Passed: : Total 2, Failed 0, Errors 0, Passed 2, Skipped 0</pre></div></div>

</li>
<li>Finally, we can add a test to verify that we&#8217;re keeping track of ink correctly. Let ScalaMock know that we&#8217;ll be mocking <code>InkReservoir</code> by adding a <code>@mockObject</code> annotation:

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #000080;">@</span>mockObject<span style="color: #F78811;">&#40;</span>InkReservoir<span style="color: #F78811;">&#41;</span></pre></div></div>

<p>And add a test to <code>ColouredTurtleTest.scala</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">  test<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;ink reservoir&quot;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> m <span style="color: #000080;">=</span> mockObject<span style="color: #F78811;">&#40;</span>InkReservoir<span style="color: #F78811;">&#41;</span>
&nbsp;
    m.<span style="color: #000000;">expects</span>.<span style="color: #000000;">use</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0.0</span>, <span style="color: #F78811;">3.0</span>, <span style="color: #F78811;">0.0</span><span style="color: #F78811;">&#41;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">val</span> t <span style="color: #000080;">=</span> ColouredTurtle<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">'green)
    t.penDown
    t.forward(3.0)
  }</span></pre></div></div>

</li>

<div class="wp_syntax"><div class="code"><pre class="none" style="font-family:monospace;">[info] ControllerTest:
[info] - draw line
[info] ColouredTurtleTest:
[info] - coloured turtles
[info] - ink reservoir
[info] Passed: : Total 3, Failed 0, Errors 0, Passed 3, Skipped 0</pre></div></div>

</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.paulbutcher.com/2011/11/scalamock-step-by-step/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Scala 2.9.1, sbt 0.10 and ScalaTest step-by-step</title>
		<link>http://www.paulbutcher.com/2011/09/scala-2-9-1-sbt-0-10-and-scalatest-step-by-step/</link>
		<comments>http://www.paulbutcher.com/2011/09/scala-2-9-1-sbt-0-10-and-scalatest-step-by-step/#comments</comments>
		<pubDate>Sat, 03 Sep 2011 22:13:49 +0000</pubDate>
		<dc:creator>paul</dc:creator>
				<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://www.paulbutcher.com/?p=1101</guid>
		<description><![CDATA[Some while ago, I wrote a post about getting Scala 2.8.0.RC1, sbt and ScalaTest to work together. Well, things have moved on somewhat since then, so I thought it was time to update it. Install sbt 0.10.1 Create the root directory for your project: $ mkdir aproject Create a settings file in this directory called [...]]]></description>
			<content:encoded><![CDATA[<p>Some while ago, I wrote a post about getting <a href="http://www.paulbutcher.com/2010/04/scala-2-8-0-rc1-sbt-and-scalatest-step-by-step/">Scala 2.8.0.RC1, sbt and ScalaTest</a> to work together. Well, things have moved on somewhat since then, so I thought it was time to update it.</p>
<ol>
<li><a href="https://github.com/harrah/xsbt/wiki">Install sbt 0.10.1</a></li>
<li>Create the root directory for your project:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">mkdir</span> aproject</pre></div></div>

</li>
<li>Create a settings file in this directory called <code>build.sbt</code> containing:

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">name <span style="color: #000080;">:=</span> <span style="color: #6666FF;">&quot;A Project&quot;</span>
&nbsp;
version <span style="color: #000080;">:=</span> <span style="color: #6666FF;">&quot;0.1&quot;</span>
&nbsp;
scalaVersion <span style="color: #000080;">:=</span> <span style="color: #6666FF;">&quot;2.9.1&quot;</span>
&nbsp;
libraryDependencies ++<span style="color: #000080;">=</span> Seq<span style="color: #F78811;">&#40;</span>
  <span style="color: #6666FF;">&quot;org.scalatest&quot;</span> <span style="color: #000080;">%%</span> <span style="color: #6666FF;">&quot;scalatest&quot;</span> <span style="color: #000080;">%</span> <span style="color: #6666FF;">&quot;1.6.1&quot;</span> <span style="color: #000080;">%</span> <span style="color: #6666FF;">&quot;test&quot;</span>
<span style="color: #F78811;">&#41;</span></pre></div></div>

</li>
<li>Create <code>src/main/scala/Widget.scala</code> containing:

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> com.<span style="color: #000000;">example</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">class</span> Widget <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> colour <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;Blue&quot;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> disposition <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;Awesome&quot;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

</li>
<li>Create <code>src/test/scala/WidgetTest.scala</code> containing:

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> com.<span style="color: #000000;">example</span>.<span style="color: #000000;">test</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">scalatest</span>.<span style="color: #000000;">FunSuite</span>
<span style="color: #0000ff; font-weight: bold;">import</span> com.<span style="color: #000000;">example</span>.<span style="color: #000000;">Widget</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">class</span> WidgetTest <span style="color: #0000ff; font-weight: bold;">extends</span> FunSuite <span style="color: #F78811;">&#123;</span>
&nbsp;
  test<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;colour&quot;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    expect<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Blue&quot;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span> <span style="color: #0000ff; font-weight: bold;">new</span> Widget<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">colour</span> <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  test<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;disposition&quot;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    expect<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Awesome&quot;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span> <span style="color: #0000ff; font-weight: bold;">new</span> Widget<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">disposition</span> <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

</li>
<li>Run your tests with <code>sbt test</code>. You should see:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>info<span style="color: #7a0874; font-weight: bold;">&#93;</span> WidgetTest:
<span style="color: #7a0874; font-weight: bold;">&#91;</span>info<span style="color: #7a0874; font-weight: bold;">&#93;</span> - colour
<span style="color: #7a0874; font-weight: bold;">&#91;</span>info<span style="color: #7a0874; font-weight: bold;">&#93;</span> - disposition
<span style="color: #7a0874; font-weight: bold;">&#91;</span>info<span style="color: #7a0874; font-weight: bold;">&#93;</span> Passed: : Total <span style="color: #000000;">2</span>, Failed <span style="color: #000000;">0</span>, Errors <span style="color: #000000;">0</span>, Passed <span style="color: #000000;">2</span>, Skipped <span style="color: #000000;">0</span></pre></div></div>

</li>
<li>Create <code>src/main/scala/Main.scala</code> containing:

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> com.<span style="color: #000000;">example</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> Main <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> main<span style="color: #F78811;">&#40;</span>args<span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> w <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Widget<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;My new widget is &quot;</span>+ w.<span style="color: #000000;">colour</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

</li>
<li>Run your program with <code>sbt run</code>. You should see:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>info<span style="color: #7a0874; font-weight: bold;">&#93;</span> Running com.example.Main 
My new widget is Blue</pre></div></div>

</li>
</ol>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.paulbutcher.com/2011/09/scala-2-9-1-sbt-0-10-and-scalatest-step-by-step/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>&#8220;Power&#8221; mocking in Scala with Borachio</title>
		<link>http://www.paulbutcher.com/2011/07/power-mocking-in-scala-with-borachio/</link>
		<comments>http://www.paulbutcher.com/2011/07/power-mocking-in-scala-with-borachio/#comments</comments>
		<pubDate>Tue, 05 Jul 2011 15:23:56 +0000</pubDate>
		<dc:creator>paul</dc:creator>
				<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://www.paulbutcher.com/?p=1065</guid>
		<description><![CDATA[The work described in this post has now been released in ScalaMock. Over the last few weeks, I&#8217;ve been working on major enhancements for Borachio (my native mocking library for Scala) to add facilities similar to those provided by PowerMock for Java. As well as interfaces and functions, it can now mock: Classes Final classes [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The work described in this post has now been released in <a href="http://scalamock.org/">ScalaMock</a>.</strong></p>
<p>Over the last few weeks, I&#8217;ve been working on major enhancements for Borachio (my native mocking library for Scala) to add facilities similar to those provided by <a href="http://code.google.com/p/powermock/">PowerMock</a> for Java. As well as interfaces and functions, it can now mock:</p>
<ul>
<li>Classes</li>
<li>Final classes and classes with final methods</li>
<li>Classes with private constructors</li>
<li>Singleton (companion) objects</li>
<li>Object creation (i.e. <code>new</code>)</li>
</ul>
<p>And, as an extra bonus, mocks are now typesafe <img src='http://www.paulbutcher.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<p>It&#8217;s not completely finished yet, but it is close enough that it&#8217;s worth soliciting feedback. The code is <a href="https://github.com/paulbutcher/borachio/tree/typesafemocks">available on GitHub</a> and a snapshot is available on <a href="http://scala-tools.org/repo-snapshots/com/borachio/">scala-tools</a>.</p>
<p><strong>Warning:</strong> Although this is getting close to &#8220;done&#8221;, the details may change slightly before final release.</p>
<p>So what does this look like in action? I&#8217;m going to demonstrate with a slightly modified version of Martin Fowler&#8217;s &#8220;warehouse&#8221; example from <a href="http://martinfowler.com/articles/mocksArentStubs.html">Mocks Aren&#8217;t Stubs</a>. This example is <a href="https://github.com/paulbutcher/powermocking">available on GitHub</a>. First, here&#8217;s a <code>Warehouse</code> object (a Scala singleton object):</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> Warehouse <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> products <span style="color: #000080;">=</span> Map<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Talisker&quot;</span> -<span style="color: #000080;">&gt;</span> <span style="color: #F78811;">5</span><span style="color: #F78811;">&#41;</span>, <span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Lagavulin&quot;</span> -<span style="color: #000080;">&gt;</span> <span style="color: #F78811;">2</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> hasInventory<span style="color: #F78811;">&#40;</span>product<span style="color: #000080;">:</span> String, quantity<span style="color: #000080;">:</span> Int<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> 
    inStock<span style="color: #F78811;">&#40;</span>product<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">&gt;=</span> quantity
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> remove<span style="color: #F78811;">&#40;</span>product<span style="color: #000080;">:</span> String, quantity<span style="color: #000080;">:</span> Int<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> 
    products +<span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span>product -<span style="color: #000080;">&gt;</span> <span style="color: #F78811;">&#40;</span>inStock<span style="color: #F78811;">&#40;</span>product<span style="color: #F78811;">&#41;</span> - quantity<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> inStock<span style="color: #F78811;">&#40;</span>product<span style="color: #000080;">:</span> String<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> products.<span style="color: #000000;">getOrElse</span><span style="color: #F78811;">&#40;</span>product, <span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>And here&#8217;s an <code>Order</code> class that removes things from the warehouse:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> Order<span style="color: #F78811;">&#40;</span>product<span style="color: #000080;">:</span> String, quantity<span style="color: #000080;">:</span> Int<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">var</span> filled <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">false</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> fill<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>Warehouse.<span style="color: #000000;">hasInventory</span><span style="color: #F78811;">&#40;</span>product, quantity<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
      Warehouse.<span style="color: #000000;">remove</span><span style="color: #F78811;">&#40;</span>product, quantity<span style="color: #F78811;">&#41;</span>
      filled <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">true</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> Order <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> apply<span style="color: #F78811;">&#40;</span>product<span style="color: #000080;">:</span> String, quantity<span style="color: #000080;">:</span> Int<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> 
    <span style="color: #0000ff; font-weight: bold;">new</span> Order<span style="color: #F78811;">&#40;</span>product, quantity<span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> largestPossible<span style="color: #F78811;">&#40;</span>product<span style="color: #000080;">:</span> String<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> quantity <span style="color: #000080;">=</span> Warehouse.<span style="color: #000000;">inStock</span><span style="color: #F78811;">&#40;</span>product<span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">new</span> Order<span style="color: #F78811;">&#40;</span>product, quantity<span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>First, let&#8217;s see how we can go about testing the <code>Order</code> class by mocking the <code>Warehouse</code> object:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">test<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Enough stock&quot;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> mockWarehouse <span style="color: #000080;">=</span> mockObject<span style="color: #F78811;">&#40;</span>Warehouse<span style="color: #F78811;">&#41;</span>
&nbsp;
  mockWarehouse.<span style="color: #000000;">expects</span>.<span style="color: #000000;">hasInventory</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Talisker&quot;</span>, <span style="color: #F78811;">50</span><span style="color: #F78811;">&#41;</span> returning <span style="color: #0000ff; font-weight: bold;">true</span>
  mockWarehouse.<span style="color: #000000;">expects</span>.<span style="color: #000000;">remove</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Talisker&quot;</span>, <span style="color: #F78811;">50</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">val</span> order <span style="color: #000080;">=</span> Order<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Talisker&quot;</span>, <span style="color: #F78811;">50</span><span style="color: #F78811;">&#41;</span>
  order.<span style="color: #000000;">fill</span>
&nbsp;
  assert<span style="color: #F78811;">&#40;</span>order.<span style="color: #000000;">filled</span><span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>The &#8220;magic&#8221; happens in the <code>mockObject</code> call:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">  <span style="color: #0000ff; font-weight: bold;">val</span> mockWarehouse <span style="color: #000080;">=</span> mockObject<span style="color: #F78811;">&#40;</span>Warehouse<span style="color: #F78811;">&#41;</span></pre></div></div>

<p>This returns a mock version of the <code>Warehouse</code> object upon which expectations can be set. From that point onwards, the code should be pretty self-explanatory.</p>
<p>Another example, this time of mocking object creation:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">test<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Order everything&quot;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> mockWarehouse <span style="color: #000080;">=</span> mockObject<span style="color: #F78811;">&#40;</span>Warehouse<span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> mockOrder <span style="color: #000080;">=</span> mock<span style="color: #F78811;">&#91;</span>Order<span style="color: #F78811;">&#93;</span>
&nbsp;
  mockWarehouse.<span style="color: #000000;">expects</span>.<span style="color: #000000;">inStock</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Laphroig&quot;</span><span style="color: #F78811;">&#41;</span> returning <span style="color: #F78811;">10</span>
  mockOrder.<span style="color: #000000;">expects</span>.<span style="color: #000000;">newInstance</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Laphroig&quot;</span>, <span style="color: #F78811;">10</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  Order.<span style="color: #000000;">largestPossible</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Laphroig&quot;</span><span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>This uses two mock objects, one representing the <code>Warehouse</code> object and one representing an (as yet uncreated) instance of the <code>Order</code> class:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">  <span style="color: #0000ff; font-weight: bold;">val</span> mockWarehouse <span style="color: #000080;">=</span> mockObject<span style="color: #F78811;">&#40;</span>Warehouse<span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> mockOrder <span style="color: #000080;">=</span> mock<span style="color: #F78811;">&#91;</span>Order<span style="color: #F78811;">&#93;</span></pre></div></div>

<p>The interesting line is this one:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">  mockOrder.<span style="color: #000000;">expects</span>.<span style="color: #000000;">newInstance</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Laphroig&quot;</span>, <span style="color: #F78811;">10</span><span style="color: #F78811;">&#41;</span></pre></div></div>

<p>Which sets an expectation that a new instance of the <code>Order</code> class will be created with constructor arguments <code>("Laphroig", 10)</code>.</p>
<h3>Building</h3>
<p>Most of the cleverness happens within a Scala compiler plugin. Compiling tests that use mocks requires three steps:</p>
<ol>
<li>Compile the code you want to test as normal.</li>
<li>Compile this code again with the Borachio compiler plugin enabled. The plugin will generate mock source code for any classes and objects mentioned in <code>@mock</code> <code>@mockObject</code> annotations.</li>
<li>Compile this code together with your test code</li>
</ol>
<p>These steps have all been collected into an <a href="https://github.com/harrah/xsbt/wiki">sbt</a> plugin, so all you need to do is mix the <code>GenerateMocks</code> trait into your project definition and everything else should happen automagically.</p>
<h3>Under the hood</h3>
<p>Given the <code>Order</code> class in our example above, the compiler plugin generates two different classes, one with the same name (i.e. <code>Order</code>) and one called <code>Mock$Order</code>. Here&#8217;s (a simplified version of) what they look like:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> Order<span style="color: #F78811;">&#40;</span>dummy<span style="color: #000080;">:</span> MockConstructorDummy<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">extends</span> Mock$Order <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> fill<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Unit <span style="color: #000080;">=</span> mock$0<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> <span style="color: #0000ff; font-weight: bold;">this</span><span style="color: #F78811;">&#40;</span>product<span style="color: #000080;">:</span> String, quantity<span style="color: #000080;">:</span> Int<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">this</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> MockConstructorDummy<span style="color: #F78811;">&#41;</span>
    mock$1<span style="color: #F78811;">&#40;</span>product, quantity<span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  lazy <span style="color: #0000ff; font-weight: bold;">val</span> mock$0 <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> MockFunction0<span style="color: #F78811;">&#91;</span>Unit<span style="color: #F78811;">&#93;</span>
  lazy <span style="color: #0000ff; font-weight: bold;">val</span> mock$1 <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> MockConstructor2<span style="color: #F78811;">&#91;</span>String, Int, Order<span style="color: #F78811;">&#93;</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">trait</span> Mock$Order <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">val</span> expects <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> fill<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> mock$0.<span style="color: #000000;">expects</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> newInstance<span style="color: #F78811;">&#40;</span>product<span style="color: #000080;">:</span> String, quantity<span style="color: #000080;">:</span> Int<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> 
      mock$1.<span style="color: #000000;">expects</span><span style="color: #F78811;">&#40;</span>product, quantity<span style="color: #F78811;">&#41;</span>
&nbsp;
    lazy <span style="color: #0000ff; font-weight: bold;">val</span> mock$0 <span style="color: #000080;">=</span> <span style="color: #008000; font-style: italic;">// reference to mock$0 in Order</span>
    lazy <span style="color: #0000ff; font-weight: bold;">val</span> mock$1 <span style="color: #000080;">=</span> <span style="color: #008000; font-style: italic;">// reference to mock$1 in Order</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Borachio then uses a custom class loader to ensure that, when the code being tested tries to load the <code>Order</code> class, it gets the generated version above (calls get forwarded to the &#8220;real&#8221; <code>Order</code> class if mocking isn&#8217;t in force).</p>
<h3>To do</h3>
<p>I hope that this code is complete enough to be useful. Certainly it&#8217;s complete enough that I&#8217;d welcome feedback on anything that doesn&#8217;t work or could be done better. The things that still need doing are:</p>
<ul>
<li>Android and JUnit3 integration</li>
<li>Support for mocking private constructors and methods</li>
<li>Forwarding to non-mock singleton functionality (this is in place for classes, not yet for singleton objects)</li>
<li>Static method (Java) support</li>
<li>Support for type-parameterised methods</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.paulbutcher.com/2011/07/power-mocking-in-scala-with-borachio/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Video: Mocking, Testing and Dependency Injection</title>
		<link>http://www.paulbutcher.com/2011/06/video-mocking-testing-and-dependency-injection/</link>
		<comments>http://www.paulbutcher.com/2011/06/video-mocking-testing-and-dependency-injection/#comments</comments>
		<pubDate>Wed, 22 Jun 2011 11:21:56 +0000</pubDate>
		<dc:creator>paul</dc:creator>
				<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://www.paulbutcher.com/?p=1060</guid>
		<description><![CDATA[A video of Chetan Padia talking about Mocking, Testing and Dependency Injection at Londroid.]]></description>
			<content:encoded><![CDATA[<p>A video of Chetan Padia talking about <a href="http://skillsmatter.com/podcast/os-mobile-server/mocking-and-testing/js-1930">Mocking, Testing and Dependency Injection</a> at Londroid.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.paulbutcher.com/2011/06/video-mocking-testing-and-dependency-injection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Android, Dependency Injection and Mock Testing</title>
		<link>http://www.paulbutcher.com/2011/06/android-dependency-injection-and-mock-testing/</link>
		<comments>http://www.paulbutcher.com/2011/06/android-dependency-injection-and-mock-testing/#comments</comments>
		<pubDate>Tue, 21 Jun 2011 18:28:13 +0000</pubDate>
		<dc:creator>paul</dc:creator>
				<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://www.paulbutcher.com/?p=1055</guid>
		<description><![CDATA[James Aley has written up a great worked example of using Borachio on Android with RoboGuice for Dependency Injection.]]></description>
			<content:encoded><![CDATA[<p><a href="https://github.com/jaley">James Aley</a> has written up a great worked example of using <a href="https://github.com/jaley/borachio-warehouse">Borachio on Android with RoboGuice for Dependency Injection</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.paulbutcher.com/2011/06/android-dependency-injection-and-mock-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Towards typesafe native Scala mocks in Borachio</title>
		<link>http://www.paulbutcher.com/2011/06/towards-typesafe-native-scala-mocks/</link>
		<comments>http://www.paulbutcher.com/2011/06/towards-typesafe-native-scala-mocks/#comments</comments>
		<pubDate>Sat, 11 Jun 2011 23:20:37 +0000</pubDate>
		<dc:creator>paul</dc:creator>
				<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://www.paulbutcher.com/?p=1028</guid>
		<description><![CDATA[The work described in this post has now been released in ScalaMock. Borachio, my native mocking library for Scala, works well enough but has a couple of annoying limitations: It can only mock functions and traits (interfaces). No support for classes, companion objects, constructors, etc. etc. etc. It&#8217;s not typesafe. I&#8217;ve been working to address [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The work described in this post has now been released in <a href="http://scalamock.org/">ScalaMock</a>.</strong></p>
<p><a href="http://borachio.com/">Borachio</a>, my native mocking library for Scala, works well enough but has a couple of annoying limitations:</p>
<ul>
<li>It can only mock functions and traits (interfaces). No support for classes, companion objects, constructors, etc. etc. etc.</li>
<li>It&#8217;s not typesafe.</li>
</ul>
<p>I&#8217;ve been working to address those limitations and, although I&#8217;m far from done, I&#8217;ve made enough progress to be worth reporting.</p>
<p><strong>Warning:</strong> The code described here is <em>experimental</em> and very likely to change. Use at your own risk, and don&#8217;t be surprised if things break in the future.</p>
<p>This experimental version of Borachio (the code is in the <a href="https://github.com/paulbutcher/borachio/tree/typesafemocks">typesafemocks branch</a> and a compiled snapshot is <a href="http://scala-tools.org/repo-snapshots/com/borachio/">available here</a>) uses a Scala compiler plugin to generate typesafe mock objects. These mock objects can extend traits <em>or</em> classes.</p>
<p>Unfortunately, current limitations of the Scala compiler plugin architecture mean that it&#8217;s effectively impossible to generate methods within a compiler plugin (see <a href="http://groups.google.com/group/scala-user/browse_thread/thread/505dca512c25d42e">this discussion</a> to understand why). So the Borachio plugin instead requires two passes &#8211; one pass to generate the source and then a second pass to compile it.</p>
<p>There&#8217;s an example of its use (an updated version of the <a href="http://www.paulbutcher.com/2011/02/mocking-in-scala-with-borachio-step-by-step/">Turtle example</a>) available  <a href="https://github.com/paulbutcher/typesafemocks">on GitHub</a>.</p>
<p>The key class in this example is <code>Turtle</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> Turtle <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> penUp<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span> ... <span style="color: #F78811;">&#125;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> penDown<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span> ... <span style="color: #F78811;">&#125;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> forward<span style="color: #F78811;">&#40;</span>distance<span style="color: #000080;">:</span> Double<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span> ... <span style="color: #F78811;">&#125;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> turn<span style="color: #F78811;">&#40;</span>angle<span style="color: #000080;">:</span> Double<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span> ... <span style="color: #F78811;">&#125;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> getAngle <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span> ... <span style="color: #F78811;">&#125;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> getPosition <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span> ... <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>In the first pass, the compiler is called with the <code>-P:borachio:generatemocks</code> argument. This causes the plugin to look for <code>@mock</code> annotations to determine which classes it needs to generate mocks for. Here&#8217;s how we specify that we want to create mock turtles:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #000080;">@</span>mock<span style="color: #F78811;">&#40;</span>classOf<span style="color: #F78811;">&#91;</span>Turtle<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span>
<span style="color: #0000ff; font-weight: bold;">class</span> Dummy</pre></div></div>

<p>And here&#8217;s what the plugin generates as a result:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> MockTurtle<span style="color: #F78811;">&#40;</span>factory<span style="color: #000080;">:</span> com.<span style="color: #000000;">borachio</span>.<span style="color: #000000;">AbstractMockFactory</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">extends</span> com.<span style="color: #000000;">example</span>.<span style="color: #000000;">Turtle</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> getPosition <span style="color: #000080;">=</span> mock$getPosition<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> getAngle <span style="color: #000080;">=</span> mock$getAngle<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> turn<span style="color: #F78811;">&#40;</span>angle<span style="color: #000080;">:</span> Double<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> mock$turn<span style="color: #F78811;">&#40;</span>angle<span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> forward<span style="color: #F78811;">&#40;</span>distance<span style="color: #000080;">:</span> Double<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> mock$forward<span style="color: #F78811;">&#40;</span>distance<span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> penDown<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> mock$penDown<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> penUp<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> mock$penUp<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">val</span> expects <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> getPosition<span style="color: #000080;">:</span> com.<span style="color: #000000;">borachio</span>.<span style="color: #000000;">TypeSafeExpectation</span><span style="color: #F78811;">&#91;</span><span style="color: #F78811;">&#40;</span>Double, Double<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> mock$getPosition.<span style="color: #000000;">expects</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> getAngle<span style="color: #000080;">:</span> com.<span style="color: #000000;">borachio</span>.<span style="color: #000000;">TypeSafeExpectation</span><span style="color: #F78811;">&#91;</span>Double<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> mock$getAngle.<span style="color: #000000;">expects</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> turn<span style="color: #F78811;">&#40;</span>angle<span style="color: #000080;">:</span> com.<span style="color: #000000;">borachio</span>.<span style="color: #000000;">MockParameter</span><span style="color: #F78811;">&#91;</span>Double<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> com.<span style="color: #000000;">borachio</span>.<span style="color: #000000;">TypeSafeExpectation</span><span style="color: #F78811;">&#91;</span>Unit<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> mock$turn.<span style="color: #000000;">expects</span><span style="color: #F78811;">&#40;</span>angle<span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> forward<span style="color: #F78811;">&#40;</span>distance<span style="color: #000080;">:</span> com.<span style="color: #000000;">borachio</span>.<span style="color: #000000;">MockParameter</span><span style="color: #F78811;">&#91;</span>Double<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> com.<span style="color: #000000;">borachio</span>.<span style="color: #000000;">TypeSafeExpectation</span><span style="color: #F78811;">&#91;</span>Unit<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> mock$forward.<span style="color: #000000;">expects</span><span style="color: #F78811;">&#40;</span>distance<span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> penDown<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> com.<span style="color: #000000;">borachio</span>.<span style="color: #000000;">TypeSafeExpectation</span><span style="color: #F78811;">&#91;</span>Unit<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> mock$penDown.<span style="color: #000000;">expects</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> penUp<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> com.<span style="color: #000000;">borachio</span>.<span style="color: #000000;">TypeSafeExpectation</span><span style="color: #F78811;">&#91;</span>Unit<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> mock$penUp.<span style="color: #000000;">expects</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">val</span> mock$getPosition <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> com.<span style="color: #000000;">borachio</span>.<span style="color: #000000;">MockFunction0</span><span style="color: #F78811;">&#91;</span><span style="color: #F78811;">&#40;</span>Double, Double<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>factory, <span style="color: #6666FF;">'getPosition)
  private val mock$getAngle = new com.borachio.MockFunction0[Double](factory, '</span>getAngle<span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">val</span> mock$turn <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> com.<span style="color: #000000;">borachio</span>.<span style="color: #000000;">MockFunction1</span><span style="color: #F78811;">&#91;</span>Double, Unit<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>factory, <span style="color: #6666FF;">'turn)
  private val mock$forward = new com.borachio.MockFunction1[Double, Unit](factory, '</span>forward<span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">val</span> mock$penDown <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> com.<span style="color: #000000;">borachio</span>.<span style="color: #000000;">MockFunction0</span><span style="color: #F78811;">&#91;</span>Unit<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>factory, <span style="color: #6666FF;">'penDown)
  private val mock$penUp = new com.borachio.MockFunction0[Unit](factory, '</span>penUp<span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>This can then be used in tests like this:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> ControllerTest <span style="color: #0000ff; font-weight: bold;">extends</span> Suite <span style="color: #0000ff; font-weight: bold;">with</span> MockFactory <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">val</span> mockTurtle <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> MockTurtle<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">this</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> controller <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Controller<span style="color: #F78811;">&#40;</span>mockTurtle<span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> testDrawLine<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    inSequence <span style="color: #F78811;">&#123;</span>
      mockTurtle.<span style="color: #000000;">expects</span>.<span style="color: #000000;">getPosition</span>.<span style="color: #000000;">returning</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0.0</span>, <span style="color: #F78811;">0.0</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
      mockTurtle.<span style="color: #000000;">expects</span>.<span style="color: #000000;">getAngle</span>.<span style="color: #000000;">returning</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0.0</span><span style="color: #F78811;">&#41;</span>
      mockTurtle.<span style="color: #000000;">expects</span>.<span style="color: #000000;">penUp</span>
      mockTurtle.<span style="color: #000000;">expects</span>.<span style="color: #000000;">turn</span><span style="color: #F78811;">&#40;</span>~<span style="color: #F78811;">&#40;</span>Pi / <span style="color: #F78811;">4</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
      mockTurtle.<span style="color: #000000;">expects</span>.<span style="color: #000000;">forward</span><span style="color: #F78811;">&#40;</span>~sqrt<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">2.0</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
      mockTurtle.<span style="color: #000000;">expects</span>.<span style="color: #000000;">getAngle</span>.<span style="color: #000000;">returning</span><span style="color: #F78811;">&#40;</span>Pi / <span style="color: #F78811;">4</span><span style="color: #F78811;">&#41;</span>
      mockTurtle.<span style="color: #000000;">expects</span>.<span style="color: #000000;">turn</span><span style="color: #F78811;">&#40;</span>~<span style="color: #F78811;">&#40;</span>-Pi / <span style="color: #F78811;">4</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
      mockTurtle.<span style="color: #000000;">expects</span>.<span style="color: #000000;">penDown</span>
      mockTurtle.<span style="color: #000000;">expects</span>.<span style="color: #000000;">forward</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1.0</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
&nbsp;
    controller.<span style="color: #000000;">drawLine</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1.0</span>, <span style="color: #F78811;">1.0</span><span style="color: #F78811;">&#41;</span>, <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">2.0</span>, <span style="color: #F78811;">1.0</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>There&#8217;s still a huge amount left to do:</p>
<ul>
<li>Support for type-parameterised methods</li>
<li>Support for classes with non-trivial constructors</li>
<li>Support for final classes or classes with final methods or private constructors</li>
<li>Support for companion objects</li>
<li>etc. etc. etc.</li>
</ul>
<p>And hopefully the limitations on code generation in Scala compiler plugins will be lifted soon, meaning that the two-stage compilation process won&#8217;t be necessary either.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.paulbutcher.com/2011/06/towards-typesafe-native-scala-mocks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mock objects on Android with Borachio: Part 3</title>
		<link>http://www.paulbutcher.com/2011/03/mock-objects-on-android-with-borachio-part-3/</link>
		<comments>http://www.paulbutcher.com/2011/03/mock-objects-on-android-with-borachio-part-3/#comments</comments>
		<pubDate>Sun, 13 Mar 2011 00:12:18 +0000</pubDate>
		<dc:creator>paul</dc:creator>
				<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://www.paulbutcher.com/?p=1002</guid>
		<description><![CDATA[As we saw in part 2 of this series, mocking Android&#8217;s PowerManager service directly is impossible. But there is an alternative approach that gives us something close enough. This article describes that approach. The code of the application described here is checked into GitHub. Given that we can&#8217;t mock PowerManager directly, instead we&#8217;re going to [...]]]></description>
			<content:encoded><![CDATA[<p>As we saw in <a href="http://www.paulbutcher.com/2011/03/mock-objects-on-android-with-borachio-part-2">part 2</a> of this series, mocking Android&#8217;s <code>PowerManager</code> service directly is impossible. But there is an alternative approach that gives us something close enough. This article describes that approach.</p>
<p>The code of the application described here is <a href="https://github.com/paulbutcher/powercontrol">checked into GitHub</a>.</p>
<p>Given that we can&#8217;t mock PowerManager directly, instead we&#8217;re going to create an interface that we <em>can</em> mock:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> PowerControl
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">void</span> disablePowerOff<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">void</span> enablePowerOff<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Together with an implementation which will be used in production code:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PowerControlImpl <span style="color: #000000; font-weight: bold;">implements</span> PowerControl
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> PowerControlImpl<span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span> context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        PowerManager powerManager <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>PowerManager<span style="color: #009900;">&#41;</span>
            context.<span style="color: #006633;">getSystemService</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span>.<span style="color: #006633;">POWER_SERVICE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        wakeLock <span style="color: #339933;">=</span> powerManager.<span style="color: #006633;">newWakeLock</span><span style="color: #009900;">&#40;</span>
            PowerManager.<span style="color: #006633;">FULL_WAKE_LOCK</span>, <span style="color: #0000ff;">&quot;PowerControl&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> disablePowerOff<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        wakeLock.<span style="color: #006633;">acquire</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> enablePowerOff<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        wakeLock.<span style="color: #006633;">release</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> PowerManager.<span style="color: #006633;">WakeLock</span> wakeLock<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We won&#8217;t be able to test this implementation, but hopefully it&#8217;s so simple that (as <a href="http://en.wikipedia.org/wiki/C._A._R._Hoare">Hoare</a> puts it) it obviously contains no deficiencies (as opposed to contains no obvious deficiencies).</p>
<p>But we <em>do</em> now have something that we can mock, so we can test that the code that calls it does so correctly.</p>
<p>The first challenge we&#8217;re going to have to overcome is how to inject a <code>PowerControl</code> implementation (the real one or the mock) into the code under test. We could use a dependency injection framework like <a href="http://code.google.com/p/roboguice/">RoboGuice</a>, but for the purposes of this article I&#8217;m going to keep things simple and use a custom <code>Application</code> class which implements a <code>getPowerControl</code> method:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PowerControlApplication <span style="color: #000000; font-weight: bold;">extends</span> Application
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        powerControl <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PowerControlImpl<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> PowerControl getPowerControl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> powerControl<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> PowerControl powerControl<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Our activity calls this during onCreate:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PowerActivity <span style="color: #000000; font-weight: bold;">extends</span> Activity
<span style="color: #009900;">&#123;</span>
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        PowerControlApplication app <span style="color: #339933;">=</span>
            <span style="color: #009900;">&#40;</span>PowerControlApplication<span style="color: #009900;">&#41;</span>getApplication<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        powerControl <span style="color: #339933;">=</span> app.<span style="color: #006633;">getPowerControl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> startImportant<span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> button<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        powerControl.<span style="color: #006633;">disablePowerOff</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> stopImportant<span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> button<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        powerControl.<span style="color: #006633;">enablePowerOff</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> PowerControl powerControl<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We can now write a test to verify that <code>startImportant</code> calls <code>disablePowerOff</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> PowerActivityTest
  <span style="color: #0000ff; font-weight: bold;">extends</span> ActivityUnitTestCase<span style="color: #F78811;">&#91;</span>PowerActivity<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>classOf<span style="color: #F78811;">&#91;</span>PowerActivity<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> 
  <span style="color: #0000ff; font-weight: bold;">with</span> MockFactory <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">val</span> startIntent <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Intent<span style="color: #F78811;">&#40;</span>Intent.<span style="color: #000000;">ACTION_MAIN</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> testStartImportant <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> mockPowerControl <span style="color: #000080;">=</span> mock<span style="color: #F78811;">&#91;</span>PowerControl<span style="color: #F78811;">&#93;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> application <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> PowerControlApplication <span style="color: #F78811;">&#123;</span>
      powerControl <span style="color: #000080;">=</span> mockPowerControl
    <span style="color: #F78811;">&#125;</span>
    setApplication<span style="color: #F78811;">&#40;</span>application<span style="color: #F78811;">&#41;</span>
    startActivity<span style="color: #F78811;">&#40;</span>startIntent, <span style="color: #0000ff; font-weight: bold;">null</span>, <span style="color: #0000ff; font-weight: bold;">null</span><span style="color: #F78811;">&#41;</span>
&nbsp;
    withExpectations <span style="color: #F78811;">&#123;</span>
      mockPowerControl expects <span style="color: #CC66FF;">'disablePowerOff</span> once
&nbsp;
      getActivity.<span style="color: #000000;">startImportant</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">null</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Our test first creates a mock <code>PowerControl</code> object:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">    <span style="color: #0000ff; font-weight: bold;">val</span> mockPowerControl <span style="color: #000080;">=</span> mock<span style="color: #F78811;">&#91;</span>PowerControl<span style="color: #F78811;">&#93;</span></pre></div></div>

<p>And then creates an application object that returns this mock instead of a &#8220;real&#8221; <code>PowerControl</code> instance:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">    <span style="color: #0000ff; font-weight: bold;">val</span> application <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> PowerControlApplication <span style="color: #F78811;">&#123;</span>
      powerControl <span style="color: #000080;">=</span> mockPowerControl
    <span style="color: #F78811;">&#125;</span></pre></div></div>

<p>We tell Android&#8217;s test framework to use this application object by calling <code>setApplication</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">    setApplication<span style="color: #F78811;">&#40;</span>application<span style="color: #F78811;">&#41;</span></pre></div></div>

<p>Finally, we set our expectation (that <code>disablePowerOff</code> is called once) and call <code>startImportant</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">    mockPowerControl expects <span style="color: #CC66FF;">'disablePowerOff</span> once
&nbsp;
    getActivity.<span style="color: #000000;">startImportant</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">null</span><span style="color: #F78811;">&#41;</span></pre></div></div>

<h2>Updated 2011-04-15</h2>
<p>Updated to Borachio 0.6.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.paulbutcher.com/2011/03/mock-objects-on-android-with-borachio-part-3/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Mock objects on Android with Borachio: Part 2</title>
		<link>http://www.paulbutcher.com/2011/03/mock-objects-on-android-with-borachio-part-2/</link>
		<comments>http://www.paulbutcher.com/2011/03/mock-objects-on-android-with-borachio-part-2/#comments</comments>
		<pubDate>Sat, 12 Mar 2011 00:18:48 +0000</pubDate>
		<dc:creator>paul</dc:creator>
				<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://www.paulbutcher.com/?p=973</guid>
		<description><![CDATA[In part 1 of this series, I showed how to mock an interface that we created ourselves under Android. That&#8217;s useful, but mocking really pays dividends when mocking OS services—doing so allows us to test our code in isolation, verify that it interacts with the OS correctly and that it handles errors properly. But there&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.paulbutcher.com/2011/03/mock-objects-on-android-with-borachio-part-1">part 1</a> of this series, I showed how to mock an interface that we created ourselves under Android. That&#8217;s useful, but mocking really pays dividends when mocking OS services—doing so allows us to test our code in isolation, verify that it interacts with the OS correctly and that it handles errors properly.</p>
<p>But there&#8217;s a wrinkle. Android is the most test-hostile environment I&#8217;ve ever had the misfortune to find myself working in. I wonder sometimes if its designers deliberately designed it to make testing as difficult as they possibly could. It <em>can</em> be done, and I&#8217;ll show how in <a href="http://www.paulbutcher.com/2011/03/mock-objects-on-android-with-borachio-part-3">part 3</a>, but if you&#8217;ll forgive me a digression, in this article I&#8217;m going to try the simple, &#8220;obvious&#8221; solution and demonstrate why it doesn&#8217;t work.</p>
<p>I&#8217;m going to try to write a simple test of an application that uses Android&#8217;s <a href="http://developer.android.com/reference/android/os/PowerManager.html"><code>PowerManager</code> service</a>. <code>PowerManager</code> allows us to control when the device switches on or off. If we&#8217;re about to start some critical operation that must complete without the device switching off, we can obtain a <a href="http://developer.android.com/reference/android/os/PowerManager.WakeLock.html">WakeLock</a>, which is what this sample app does.</p>
<p>The code is <a href="https://github.com/paulbutcher/androidistesthostile">checked into GitHub</a>. Here&#8217;s the code that we want to test:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.paulbutcher.powercontrol</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.PowerManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PowerControl <span style="color: #000000; font-weight: bold;">extends</span> Activity
<span style="color: #009900;">&#123;</span>
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> startImportant<span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> button<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        PowerManager powerManager <span style="color: #339933;">=</span>
            <span style="color: #009900;">&#40;</span>PowerManager<span style="color: #009900;">&#41;</span>getSystemService<span style="color: #009900;">&#40;</span>POWER_SERVICE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        wakeLock <span style="color: #339933;">=</span> powerManager.<span style="color: #006633;">newWakeLock</span><span style="color: #009900;">&#40;</span>
            PowerManager.<span style="color: #006633;">FULL_WAKE_LOCK</span>, <span style="color: #0000ff;">&quot;PowerControl&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        wakeLock.<span style="color: #006633;">acquire</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> stopImportant<span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> button<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        wakeLock.<span style="color: #006633;">release</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> PowerManager.<span style="color: #006633;">WakeLock</span> wakeLock<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Let&#8217;s try to write a test that verifies that <code>startImportant</code> calls <code>PowerManager.newWakeLock</code>. Our first task is going to be working out how to inject a mock <code>PowerManager</code> into the code under test.</p>
<p>That code obtains its <code>PowerManager</code> instance by calling <code>Context.getSystemService</code>. Happily, Android provides <a href="http://developer.android.com/reference/android/test/mock/MockContext.html"><code>MockContext</code></a> and <a href="http://developer.android.com/reference/android/test/ActivityUnitTestCase.html#setActivityContext(android.content.Context)"><code>ActivityUnitTestCase.setActivityContext</code></a>, so we should be all set.</p>
<p>Before we get carried away, let&#8217;s just verify that we can get a test using a MockContext to run at all:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">  <span style="color: #0000ff; font-weight: bold;">def</span> testAttempt1 <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> mockContext <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> MockContext<span style="color: #000080;">;</span>
    setActivityContext<span style="color: #F78811;">&#40;</span>mockContext<span style="color: #F78811;">&#41;</span>
    startActivity<span style="color: #F78811;">&#40;</span>startIntent, <span style="color: #0000ff; font-weight: bold;">null</span>, <span style="color: #0000ff; font-weight: bold;">null</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Let&#8217;s see what happens when we run that:</p>

<div class="wp_syntax"><div class="code"><pre class="ant" style="font-family:monospace;">Failure in testAttempt1:
junit.framework.AssertionFailedError
  at android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:148)</pre></div></div>

<p>Hmm—apparently this isn&#8217;t going to be as easy as we hoped.</p>
<p>The problem is that <code>startActivity</code> calls our <code>MockContext</code>. Specifically it calls <code>getSystemService("layout_inflater")</code> which fails because <code>MockContext</code>&#8216;s methods are non-functional and throw <code>UnsupportedOperationException</code>.</p>
<p>It turns out that what Android means by &#8220;mock&#8221; isn&#8217;t what the rest of the world means. As Martin Fowler says in <a href="http://martinfowler.com/articles/mocksArentStubs.html">Mocks Aren&#8217;t Stubs</a>, mocks are:</p>
<blockquote><p>
objects pre-programmed with expectations which form a specification of the calls they are expected to receive.
</p></blockquote>
<p>Never mind—there is another way. Android&#8217;s <a href="http://developer.android.com/reference/android/content/ContextWrapper.html"><code>ContextWrapper</code></a> allows us to wrap an existing context, only changing those bits of functionality we&#8217;re interested in for the purposes of our test:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">  <span style="color: #0000ff; font-weight: bold;">def</span> testAttempt2 <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> testContext <span style="color: #000080;">=</span> 
        <span style="color: #0000ff; font-weight: bold;">new</span> ContextWrapper<span style="color: #F78811;">&#40;</span>getInstrumentation.<span style="color: #000000;">getTargetContext</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span>
    setActivityContext<span style="color: #F78811;">&#40;</span>testContext<span style="color: #F78811;">&#41;</span>
    startActivity<span style="color: #F78811;">&#40;</span>startIntent, <span style="color: #0000ff; font-weight: bold;">null</span>, <span style="color: #0000ff; font-weight: bold;">null</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span></pre></div></div>

<p>That works, so now we just need to modify it to return a mock PowerManager when getSystemService is called:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">  <span style="color: #0000ff; font-weight: bold;">def</span> testAttempt3 <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> mockPowerManager <span style="color: #000080;">=</span> mock<span style="color: #F78811;">&#91;</span>PowerManager<span style="color: #F78811;">&#93;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> testContext <span style="color: #000080;">=</span> 
      <span style="color: #0000ff; font-weight: bold;">new</span> ContextWrapper<span style="color: #F78811;">&#40;</span>getInstrumentation.<span style="color: #000000;">getTargetContext</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
        <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> getSystemService<span style="color: #F78811;">&#40;</span>name<span style="color: #000080;">:</span> String<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> name <span style="color: #0000ff; font-weight: bold;">match</span> <span style="color: #F78811;">&#123;</span>
          <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #6666FF;">&quot;power&quot;</span> <span style="color: #000080;">=&gt;</span> mockPowerManager
          <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #000080;">_</span> <span style="color: #000080;">=&gt;</span> <span style="color: #0000ff; font-weight: bold;">super</span>.<span style="color: #000000;">getSystemService</span><span style="color: #F78811;">&#40;</span>name<span style="color: #F78811;">&#41;</span>
        <span style="color: #F78811;">&#125;</span>
      <span style="color: #F78811;">&#125;</span>
    setActivityContext<span style="color: #F78811;">&#40;</span>testContext<span style="color: #F78811;">&#41;</span>
    startActivity<span style="color: #F78811;">&#40;</span>startIntent, <span style="color: #0000ff; font-weight: bold;">null</span>, <span style="color: #0000ff; font-weight: bold;">null</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Which looks great, right up until we run it:</p>

<div class="wp_syntax"><div class="code"><pre class="ant" style="font-family:monospace;">Error in testAttempt3:
java.lang.IllegalArgumentException: android.os.PowerManager is not an interface</pre></div></div>

<p>Borachio, in common with most other mocking frameworks, can only mock interfaces, and <code>PowerManager</code> is a class, not an interface. There are mocking frameworks that can mock classes, for example <a href="http://code.google.com/p/powermock/">PowerMock</a>, but they rely on code generation, which Android&#8217;s Dalvik VM <a href="http://code.google.com/p/android/issues/detail?id=6322">doesn&#8217;t (yet) support</a>. So that&#8217;s not going to be any help <img src='http://www.paulbutcher.com/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </p>
<p>There&#8217;s one final thing we can try. As well as mocking interfaces, Borachio can also mock functions. So we can derive from <code>PowerManager</code> and just mock the single method we&#8217;re interested in like this:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">  <span style="color: #0000ff; font-weight: bold;">def</span> testAttempt4 <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> mockNewWakeLock <span style="color: #000080;">=</span> 
      mockFunction<span style="color: #F78811;">&#91;</span>Int, String, PowerManager<span style="color: #000080;">#</span>WakeLock<span style="color: #F78811;">&#93;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> mockPowerManager <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> PowerManager <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> newWakeLock<span style="color: #F78811;">&#40;</span>flags<span style="color: #000080;">:</span> Int, tag<span style="color: #000080;">:</span> String<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> 
        mockNewWakeLock<span style="color: #F78811;">&#40;</span>flags, tag<span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> testContext <span style="color: #000080;">=</span> 
      <span style="color: #0000ff; font-weight: bold;">new</span> ContextWrapper<span style="color: #F78811;">&#40;</span>getInstrumentation.<span style="color: #000000;">getTargetContext</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
        <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> getSystemService<span style="color: #F78811;">&#40;</span>name<span style="color: #000080;">:</span> String<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> name <span style="color: #0000ff; font-weight: bold;">match</span> <span style="color: #F78811;">&#123;</span>
          <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #6666FF;">&quot;power&quot;</span> <span style="color: #000080;">=&gt;</span> mockPowerManager
          <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #000080;">_</span> <span style="color: #000080;">=&gt;</span> <span style="color: #0000ff; font-weight: bold;">super</span>.<span style="color: #000000;">getSystemService</span><span style="color: #F78811;">&#40;</span>name<span style="color: #F78811;">&#41;</span>
        <span style="color: #F78811;">&#125;</span>
      <span style="color: #F78811;">&#125;</span>
    setActivityContext<span style="color: #F78811;">&#40;</span>testContext<span style="color: #F78811;">&#41;</span>
    startActivity<span style="color: #F78811;">&#40;</span>startIntent, <span style="color: #0000ff; font-weight: bold;">null</span>, <span style="color: #0000ff; font-weight: bold;">null</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span></pre></div></div>

<p>But, when we compile this, we get:</p>

<div class="wp_syntax"><div class="code"><pre class="ant" style="font-family:monospace;">PowerControlTest.scala:53: error: constructor PowerManager cannot be accessed in anonymous class $anon
    val mockPowerManager = new PowerManager {
                               ^</pre></div></div>

<p><code>PowerManager</code>&#8216;s constructor is private <img src='http://www.paulbutcher.com/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </p>
<p>And at this point, we&#8217;re out of easy options. We can&#8217;t play the same trick with <code>PowerManager</code> as we played with <code>Context</code>, as there&#8217;s no <code>PowerManagerWrapper</code> or similar.</p>
<p>We&#8217;re not beaten yet—I&#8217;ll show how to get around this problem in <a href="http://www.paulbutcher.com/2011/03/mock-objects-on-android-with-borachio-part-3/">part 3</a> of this series. Small wonder, however, that most of the Android code I&#8217;ve seen has virtually no tests!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.paulbutcher.com/2011/03/mock-objects-on-android-with-borachio-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Mock objects on Android with Borachio: Part 1</title>
		<link>http://www.paulbutcher.com/2011/03/mock-objects-on-android-with-borachio-part-1/</link>
		<comments>http://www.paulbutcher.com/2011/03/mock-objects-on-android-with-borachio-part-1/#comments</comments>
		<pubDate>Fri, 11 Mar 2011 22:05:41 +0000</pubDate>
		<dc:creator>paul</dc:creator>
				<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://www.paulbutcher.com/?p=947</guid>
		<description><![CDATA[For a complete worked example of using Borachio on Android with RoboGuice for Dependency Injection, go here One of my biggest frustrations with writing code for Android has been the fact that none of the current Java mocking frameworks work on Android&#8217;s Dalvik VM. I recently released Borachio a native Scala mocking framework which does [...]]]></description>
			<content:encoded><![CDATA[<p><b>For a complete worked example of using Borachio on Android with RoboGuice for Dependency Injection, go <a href="https://github.com/jaley/borachio-warehouse">here</a></b></p>
<p>One of my biggest frustrations with writing code for Android has been the fact that none of the current Java mocking frameworks work on Android&#8217;s Dalvik VM. I recently released <a href="http://borachio.com/">Borachio</a> a native Scala mocking framework which does work on Android.</p>
<p>Because Borachio is written in Scala, you&#8217;ll need to write your tests in Scala. But it can be used to test code written in Java.</p>
<p>This post demonstrates how to get basic mocking working. Things get more complicated when you try to mock bits of Android itself, but I&#8217;ll cover that in a subsequent article.</p>
<p>This is an Android version of the example in Martin Fowler&#8217;s article <a href="http://martinfowler.com/articles/mocksArentStubs.html">Mocks Aren&#8217;t Stubs</a>. The code is checked into GitHub <a href="https://github.com/paulbutcher/warehousemanager">here</a>. You&#8217;ll need to have the <a href="http://developer.android.com/sdk/index.html">Android SDK</a> and <a href="http://www.scala-lang.org/downloads">Scala 2.8</a> installed to run this code.</p>
<p>We&#8217;re going to build a (very) simple ordering system. Orders will succeed if there&#8217;s enough inventory in our warehouse and fail if not. Let&#8217;s start by creating a very simple little Android application for us to test:</p>
<ol>
<li>
Create a new project with:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">android create project <span style="color: #660033;">-p</span> WarehouseManager <span style="color: #660033;">-t</span> android-<span style="color: #000000;">8</span> \
    <span style="color: #660033;">-p</span> warehousemanager <span style="color: #660033;">-k</span> com.example.warehousemanager \
    <span style="color: #660033;">-a</span> WarehouseManager</pre></div></div>

</li>
<li>
The core abstraction is a warehouse, represented by a <code>Warehouse</code> interface:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.example.warehousemanager</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Warehouse <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">boolean</span> hasInventory<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> product, <span style="color: #000066; font-weight: bold;">int</span> quantity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">void</span> remove<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> product, <span style="color: #000066; font-weight: bold;">int</span> quantity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li>
And here&#8217;s a very simple concrete implementation of <code>Warehouse</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.example.warehousemanager</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.HashMap</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RealWarehouse <span style="color: #000000; font-weight: bold;">implements</span> Warehouse <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> RealWarehouse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        products <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>String, Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        products.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Talisker&quot;</span>, <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        products.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Lagavulin&quot;</span>, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> hasInventory<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> product, <span style="color: #000066; font-weight: bold;">int</span> quantity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> inStock<span style="color: #009900;">&#40;</span>product<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> quantity<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> remove<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> product, <span style="color: #000066; font-weight: bold;">int</span> quantity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        products.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>product, inStock<span style="color: #009900;">&#40;</span>product<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> quantity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> inStock<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> product<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">Integer</span> quantity <span style="color: #339933;">=</span> products.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>product<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> quantity <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">:</span> quantity<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> HashMap<span style="color: #339933;">&lt;</span>String, Integer<span style="color: #339933;">&gt;</span> products<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li>
We remove things from the warehouse by placing an <code>Order</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.example.warehousemanager</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Order <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Order<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> product, <span style="color: #000066; font-weight: bold;">int</span> quantity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">product</span> <span style="color: #339933;">=</span> product<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">quantity</span> <span style="color: #339933;">=</span> quantity<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> fill<span style="color: #009900;">&#40;</span>Warehouse warehouse<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>warehouse.<span style="color: #006633;">hasInventory</span><span style="color: #009900;">&#40;</span>product, quantity<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            warehouse.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span>product, quantity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            filled <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isFilled<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> filled<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> filled <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> product<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> quantity<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li>
We&#8217;ll need a UI to allow us to make orders, so modify <code>main.xml</code> to look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;LinearLayout</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:orientation</span>=<span style="color: #ff0000;">&quot;vertical&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextView</span>  </span>
<span style="color: #009900;">      <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;Product:&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;EditText</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/product&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextView</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;Quantity:&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;EditText</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;fill_parent&quot;</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/quantity&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Button</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;Place order&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">android:onClick</span>=<span style="color: #ff0000;">&quot;placeOrder&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/LinearLayout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</li>
<li>
And finally, here&#8217;s the implementation of <code>WarehouseManager</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.example.warehousemanager</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.EditText</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.Toast</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> WarehouseManager <span style="color: #000000; font-weight: bold;">extends</span> Activity
<span style="color: #009900;">&#123;</span>
    <span style="color: #008000; font-style: italic; font-weight: bold;">/** Called when the activity is first created. */</span>
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        productEditText <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>EditText<span style="color: #009900;">&#41;</span>findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">product</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        quantityEditText <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>EditText<span style="color: #009900;">&#41;</span>findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">quantity</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> placeOrder<span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> view<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span> product <span style="color: #339933;">=</span> productEditText.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> quantity <span style="color: #339933;">=</span> <span style="color: #003399;">Integer</span>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span>quantityEditText.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Order order <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Order<span style="color: #009900;">&#40;</span>product, quantity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        order.<span style="color: #006633;">fill</span><span style="color: #009900;">&#40;</span>warehouse<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">String</span> message <span style="color: #339933;">=</span> order.<span style="color: #006633;">isFilled</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #0000ff;">&quot;Success&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Failure&quot;</span><span style="color: #339933;">;</span>
        Toast toast <span style="color: #339933;">=</span> Toast.<span style="color: #006633;">makeText</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, message, Toast.<span style="color: #006633;">LENGTH_SHORT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        toast.<span style="color: #006633;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> Warehouse warehouse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RealWarehouse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> EditText productEditText<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> EditText quantityEditText<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
</ol>
<p>You should now have a little Android application that can be compiled and installed with <code>ant install</code>. Here&#8217;s what it looks like:</p>
<p><img src="http://www.paulbutcher.com/wp-content/uploads/2011/03/WarehouseManager.png" alt="Screenshot" /></p>
<p>So, now that we&#8217;ve got something to test, let&#8217;s create a test project to test it:</p>
<ol>
<li>
Create a test project with:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">android create test-project <span style="color: #660033;">-p</span> <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-m</span> .. <span style="color: #660033;">-n</span> WarehouseManagerTest</pre></div></div>

<p>Next, we&#8217;ll convert this to a Scala project, as described <a href="http://lamp.epfl.ch/~michelou/android/">here</a>.
</li>
<li>
Add <code>scala.dir</code> and <code>proguard.dir</code> to <code>local.properties</code>. Here&#8217;s what I added to mine (you&#8217;ll need to change the paths to match your local installation):</p>

<div class="wp_syntax"><div class="code"><pre class="properties" style="font-family:monospace;"><span style="color: #000080; font-weight:bold;">scala.dir</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">/opt/local/share/scala-2.8</span>
<span style="color: #000080; font-weight:bold;">proguard.dir</span><span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;">/Users/paul/android-sdk-mac_86/tools/proguard/</span></pre></div></div>

</li>
<li>
Copy <a href="http://lampsvn.epfl.ch/svn-repos/scala/android-examples/trunk/android-sdk/HelloActivity/build-scala.xml"><code>build-scala.xml</code></a> into the root of the test project and add the following to <code>build.xml</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;import</span> <span style="color: #000066;">file</span>=<span style="color: #ff0000;">&quot;build-scala.xml&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- Converts this project's .class files into .dex files --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;-dex&quot;</span> <span style="color: #000066;">depends</span>=<span style="color: #ff0000;">&quot;compile, scala-compile, scala-shrink&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scala-dex-helper</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</li>
<li>
Delete the <code>proguard.cfg</code> file and copy the <a href="http://lampsvn.epfl.ch/svn-repos/scala/android-examples/trunk/android-sdk/HelloActivity/configs/"><code>configs</code></a> directory into the test project. Add the following to the bottom of both <code>default-debug.cfg</code> and <code>default-release.cfg</code> (to ensure that ProGuard doesn&#8217;t discard our test classes:</p>

<div class="wp_syntax"><div class="code"><pre class="proguard" style="font-family:monospace;">-keep public class * implements junit.framework.Test { public void test*(); }</pre></div></div>

</li>
<li>
Copy the <a href="http://scala-tools.org/repo-releases/com/borachio/borachio_2.8.1/0.6/borachio_2.8.1-0.6.jar">Borachio JAR</a> to the <code>libs</code> directory.
</li>
<li>
Finally, we can write our tests, which create mock instances of the <code>Warehouse</code> interface:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> com.<span style="color: #000000;">example</span>.<span style="color: #000000;">warehousemanager</span><span style="color: #000080;">;</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">import</span> junit.<span style="color: #000000;">framework</span>.<span style="color: #000000;">TestCase</span>
<span style="color: #0000ff; font-weight: bold;">import</span> com.<span style="color: #000000;">borachio</span>.<span style="color: #000000;">junit3</span>.<span style="color: #000000;">MockFactory</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">class</span> OrderTest <span style="color: #0000ff; font-weight: bold;">extends</span> TestCase <span style="color: #0000ff; font-weight: bold;">with</span> MockFactory <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> testInStock<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    withExpectations <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">val</span> mockWarehouse <span style="color: #000080;">=</span> mock<span style="color: #F78811;">&#91;</span>Warehouse<span style="color: #F78811;">&#93;</span>
      inSequence <span style="color: #F78811;">&#123;</span>
        mockWarehouse expects <span style="color: #CC66FF;">'hasInventory</span> withArguments <span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Talisker&quot;</span>, <span style="color: #F78811;">50</span><span style="color: #F78811;">&#41;</span> returning <span style="color: #0000ff; font-weight: bold;">true</span> once<span style="color: #000080;">;</span>
        mockWarehouse expects <span style="color: #CC66FF;">'remove</span> withArguments <span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Talisker&quot;</span>, <span style="color: #F78811;">50</span><span style="color: #F78811;">&#41;</span> once
      <span style="color: #F78811;">&#125;</span>
&nbsp;
      <span style="color: #0000ff; font-weight: bold;">val</span> order <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Order<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Talisker&quot;</span>, <span style="color: #F78811;">50</span><span style="color: #F78811;">&#41;</span>
      order.<span style="color: #000000;">fill</span><span style="color: #F78811;">&#40;</span>mockWarehouse<span style="color: #F78811;">&#41;</span>
&nbsp;
      assert<span style="color: #F78811;">&#40;</span>order.<span style="color: #000000;">isFilled</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> testOutOfStock<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    withExpectations <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">val</span> mockWarehouse <span style="color: #000080;">=</span> mock<span style="color: #F78811;">&#91;</span>Warehouse<span style="color: #F78811;">&#93;</span>
      mockWarehouse expects <span style="color: #CC66FF;">'hasInventory</span> returns <span style="color: #0000ff; font-weight: bold;">false</span> once
&nbsp;
      <span style="color: #0000ff; font-weight: bold;">val</span> order <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Order<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Talisker&quot;</span>, <span style="color: #F78811;">50</span><span style="color: #F78811;">&#41;</span>
      order.<span style="color: #000000;">fill</span><span style="color: #F78811;">&#40;</span>mockWarehouse<span style="color: #F78811;">&#41;</span>
&nbsp;
      assert<span style="color: #F78811;">&#40;</span><span style="color: #000080;">!</span>order.<span style="color: #000000;">isFilled</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

</li>
<li>
Run the tests with:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">ant run-tests</pre></div></div>

</li>
</ol>
<p>In <a href="http://www.paulbutcher.com/2011/03/mock-objects-on-android-with-borachio-part-2/">part 2</a>, we&#8217;ll look at some of the challenges of mocking Android components.</p>
<h2>Updated 2011-04-15</h2>
<p>Updated to Borachio 0.6.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.paulbutcher.com/2011/03/mock-objects-on-android-with-borachio-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

