<?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>Assert Interesting &#187; gwt mockito unit.testing tdd</title>
	<atom:link href="http://www.assertinteresting.com/tag/gwt-mockito-unittesting-tdd/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.assertinteresting.com</link>
	<description>Thoughts on software development</description>
	<lastBuildDate>Sun, 14 Feb 2010 17:00:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Unit testing GWT</title>
		<link>http://www.assertinteresting.com/2009/05/unit-testing-gwt/</link>
		<comments>http://www.assertinteresting.com/2009/05/unit-testing-gwt/#comments</comments>
		<pubDate>Fri, 15 May 2009 23:20:34 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[gwt mockito unit.testing tdd]]></category>

		<guid isPermaLink="false">http://www.assertinteresting.com/?p=71</guid>
		<description><![CDATA[Unit testing GWT can be very difficult.  Let me start by saying I don&#8217;t consider tests that use com.google.gwt.junit.client.GWTTestCase to be unit tests, I consider them to be integration tests.  This article is about compiling your GWT code into java bytecode and running it just like any other unit test.  Here&#8217;s why you&#8217;d want to [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Unit</strong> testing GWT can be very difficult.  Let me start by saying I don&#8217;t consider tests that use com.google.gwt.junit.client.GWTTestCase to be <strong>unit</strong> tests, I consider them to be <strong>integration</strong> tests.  This article is about compiling your GWT code into java bytecode and running it just like any other unit test.  Here&#8217;s why you&#8217;d want to do this:</p>
<ol>
<li>Your test will execute quickly.</li>
<li>You can use any testing framework you want instead of being forced to use JUnit.</li>
<li>As you&#8217;ll see, you can leverage GWT to mock some dependencies for you.</li>
</ol>
<p>The first thing we&#8217;ll be doing is creating our own <a href="http://code.google.com/p/google-web-toolkit/source/browse/releases/1.5/user/src/com/google/gwt/junit/GWTMockUtilities.java?r=3125" target="_blank">GWTMockUtilities</a> class.  GWT already comes with one of these, but it isn&#8217;t ideal for our needs.  This class allows us to use GWT.create(&#8230;); in Java bytecode without exceptions being thrown.  If you were to call this in a class compiled by the Java compiler, you&#8217;d get a runtime exception that says this:</p>
<pre>          ERROR: GWT.create() is only usable in client code!  It cannot be called,
              for example, from server code.  If you are running a unit test,
              check that your test case extends GWTTestCase and that GWT.create()
              is not called from within an initializer or constructor.</pre>
<p>Lies! GWTMockUtilities also prevents this exception from occurring.  Here&#8217;s our version of GWTMockUtilities with a minor change.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.core.client.GWTBridge</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.core.client.GWT</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.Method</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.InvocationTargetException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * This is almost an exact copy of GWT's com.google.gwt.junit.GWTMockUtilities except it uses our own GWTWidgetBridge
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GWTMockUtilities <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> disarm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    GWTBridge bridge <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GWTWidgetBridge<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #008000; font-style: italic; font-weight: bold;">/** our change **/</span>
    setGwtBridge<span style="color: #009900;">&#40;</span>bridge<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: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> restore<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    setGwtBridge<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</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> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> setGwtBridge<span style="color: #009900;">&#40;</span>GWTBridge bridge<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">Class</span> gwtClass <span style="color: #339933;">=</span> GWT.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> paramTypes <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>GWTBridge.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">Method</span> setBridgeMethod <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
      setBridgeMethod <span style="color: #339933;">=</span> gwtClass.<span style="color: #006633;">getDeclaredMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;setBridge&quot;</span>, paramTypes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">NoSuchMethodException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    setBridgeMethod.<span style="color: #006633;">setAccessible</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
      setBridgeMethod.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>gwtClass, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>bridge<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IllegalAccessException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InvocationTargetException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Why do we need to change this line? Well the normal GWTMockUtilities will change GWT.create(&#8230;)  so that it always returns null instead of throwing an UnsupportedOperationException.  Certainly, this is better, but it tends to cause NullPointerExceptions all over the place, especially if you have any logic in your constructors.</p>
<p>Our GWTWidgetBridge returns a mock of the class you tried to create instead of null.  This gets rid of all of those NullPointerExceptions.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.core.client.GWTBridge</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.dev.About</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">Mockito</span>.<span style="color: #339933;">*;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * This is an exact copy of com.google.gwt.junit.GWTDummyBridge except
 * it returns mocked Widgets instead of null's
 **/</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GWTWidgetBridge <span style="color: #000000; font-weight: bold;">extends</span> GWTBridge <span style="color: #009900;">&#123;</span>
    @Override
    <span style="color: #000000; font-weight: bold;">public</span>  T create<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">Class</span> classLiteral<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#41;</span> mock<span style="color: #009900;">&#40;</span>classLiteral<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #008000; font-style: italic; font-weight: bold;">/** Mock what we create.  This used to return null. **/</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getVersion<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> About.<span style="color: #006633;">GWT_VERSION_NUM</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isClient<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> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> log<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> s, <span style="color: #003399;">Throwable</span> throwable<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you can see, I used my favorite Java mocking framework, <a href="http://mockito.org/" target="_blank">Mockito</a>. But, I assume you can use your favorite Java mocking framework, too (BTW, your favorite Java mocking framework should be Mockito).  Lets look at this in action.  Here&#8217;s some 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;">import</span> <span style="color: #006699;">com.google.gwt.core.client.GWT</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.user.client.ui.ChangeListener</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.user.client.ui.ClickListener</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.user.client.ui.Composite</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.user.client.ui.HTML</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.user.client.ui.ListBox</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.user.client.ui.TextBox</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Set</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> Unit <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Composite</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> ListBox listBox1<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> TextBox textBox1<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> ListBox listBox2<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">HTML</span> html1<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">Button</span> button1<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> CustomFlexTable flexTable<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Unit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        initialize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        compose<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        initWidget<span style="color: #009900;">&#40;</span>flexTable<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;">void</span> initialize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        listBox1 <span style="color: #339933;">=</span> GWT.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span>ListBox.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        textBox1 <span style="color: #339933;">=</span> GWT.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span>TextBox.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        listBox2 <span style="color: #339933;">=</span> GWT.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span>ListBox.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        html1 <span style="color: #339933;">=</span> GWT.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">HTML</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        button1 <span style="color: #339933;">=</span> GWT.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Button</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        flexTable <span style="color: #339933;">=</span> GWT.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span>CustomFlexTable.<span style="color: #000000; font-weight: bold;">class</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> <span style="color: #000066; font-weight: bold;">void</span> compose<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        flexTable.<span style="color: #006633;">setCellSpacing</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #008000; font-style: italic; font-weight: bold;">/** Under normal circumstances, this would cause a NPE **/</span>
&nbsp;
        flexTable.<span style="color: #006633;">setWidget</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, listBox1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        flexTable.<span style="color: #006633;">setWidget</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span>, textBox1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        flexTable.<span style="color: #006633;">setWidget</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">2</span>, listBox2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        flexTable.<span style="color: #006633;">setWidget</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">3</span>, button1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        flexTable.<span style="color: #006633;">setWidget</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">0</span>, html1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        flexTable.<span style="color: #006633;">setColSpan</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #008000; font-style: italic; font-weight: bold;">/** normally would be flexTable.getFlexCellFormatter().setColSpan(1, 0, 4); To be discussed... **/</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
     <span style="color: #666666; font-style: italic;">//and so on...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And here&#8217;s how we start our unit test (using Testng):</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> MyTest <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Unit unit<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">HTML</span> html1<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> TextBox textBox1<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> ListBox listBox1<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> ListBox listBox2<span style="color: #339933;">;</span>
&nbsp;
    @BeforeMethod
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        GWTMockUtilities.<span style="color: #006633;">disarm</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #008000; font-style: italic; font-weight: bold;">/** Use our GWTMockUtilities **/</span>
&nbsp;
        unit <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Unit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        listBox1 <span style="color: #339933;">=</span> unit.<span style="color: #006633;">listBox1</span><span style="color: #339933;">;</span> <span style="color: #008000; font-style: italic; font-weight: bold;">/** These have already been mocked for us **/</span>
        textBox1 <span style="color: #339933;">=</span> unit.<span style="color: #006633;">textBox1</span><span style="color: #339933;">;</span>
        html1 <span style="color: #339933;">=</span> unit.<span style="color: #006633;">html1</span><span style="color: #339933;">;</span>
        listBox2 <span style="color: #339933;">=</span> unit.<span style="color: #006633;">listBox2</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @AfterMethod
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> tearDown<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        GWTMockUtilities.<span style="color: #006633;">restore</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #008000; font-style: italic; font-weight: bold;">/** Stop using our GWTMockUtilities (integration tests may appreciate this) **/</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Test
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testTrue<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;">assert</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>  <span style="color: #008000; font-style: italic; font-weight: bold;">/** Normally, you can't even get this test to pass because setup fails. **/</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//other tests...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And with that, we can unit test as if we had written plain old java objects. There are some restrictions on this that may cramp your style.  You have to use GWT.create(&#8230;) instead of the new keyword.  You may be used to typing new Label(&#8220;FooBar&#8221;); in your code. With this method, you&#8217;d have to change that to:</p>
<pre>Label label = GWT.create(Label.class);
label.setText("FooBar");</pre>
<p>The other restriction is related to the CustomFlexTable I&#8217;ve created.  This is its source:</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> CustomFlexTable <span style="color: #000000; font-weight: bold;">extends</span> FlexTable <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setColSpan<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> row, <span style="color: #000066; font-weight: bold;">int</span> column, <span style="color: #000066; font-weight: bold;">int</span> colSpan<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        getFlexCellFormatter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setColSpan</span><span style="color: #009900;">&#40;</span>row, column, colSpan<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If I don&#8217;t use this class, I would get a NullPointerException.  The reason is that the GWTWidgetBridge we created mock&#8217;s the FlexTable but it doesn&#8217;t stub out the FlexTable&#8217;s getFlexCellFormatter() method.  Mockito reacts by returning null for that method and when we call setColSpan(&#8230;) on null, we get a NPE.  In Mockito&#8217;s defense, FlexTable is violating the <a href="http://en.wikipedia.org/wiki/Law_of_Demeter">Law of Demeter</a> here. You can argue that the API is the problem.  Also, note that this is only a problem when the constructor calls the compose() method. If the client called the compose method instead of the constructor, we could stub the FlexTable&#8217;s getFlexCellFormatter() method before we call compose().</p>
]]></content:encoded>
			<wfw:commentRss>http://www.assertinteresting.com/2009/05/unit-testing-gwt/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
