<?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>REVERT TO CONSOLE &#187; UnitTest</title>
	<atom:link href="http://www.reverttoconsole.com/blog/category/unittest/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.reverttoconsole.com</link>
	<description>for f in *;do echo &#124; sed 'i\rtc' &#62;&#62; $f;done;</description>
	<lastBuildDate>Sat, 10 Jul 2010 12:40:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Grails and HtmlUnit</title>
		<link>http://www.reverttoconsole.com/blog/grails/grails-and-htmlunit/</link>
		<comments>http://www.reverttoconsole.com/blog/grails/grails-and-htmlunit/#comments</comments>
		<pubDate>Sat, 09 May 2009 18:56:14 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[UnitTest]]></category>
		<category><![CDATA[Useful Links]]></category>

		<guid isPermaLink="false">http://www.reverttoconsole.com/?p=360</guid>
		<description><![CDATA[I was struggling to add HtmlUnit to Grails to do some testing when I came upon this. The solution was to remove the HtmlUnit included jar, xml-apis-1.3.04. The error looked like this: loader constraint violation: loader (instance of &#38;amp;amp;lt;bootloader&#38;amp;amp;gt;) previously initiated loading for a different type with name &#38;quot;org/w3c/dom/NamedNodeMap&#38;quot; java.lang.LinkageError: loader constraint violation: loader (instance of ) [...]]]></description>
			<content:encoded><![CDATA[<p>I was struggling to add HtmlUnit to Grails to do some testing <a href="http://jira.codehaus.org/browse/GROOVY-3356">when I came upon this</a>.</p>
<p>The solution was to remove the HtmlUnit included jar, xml-apis-1.3.04.</p>
<p>The error looked like this:</p>
<pre class="brush: php">
loader constraint violation: loader (instance of &amp;amp;amp;lt;bootloader&amp;amp;amp;gt;) previously initiated loading for a different type with name &amp;quot;org/w3c/dom/NamedNodeMap&amp;quot;
java.lang.LinkageError: loader constraint violation: loader (instance of ) previously initiated loading for a different type with name &amp;quot;org/w3c/dom/NamedNodeMap&amp;quot;
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
at java.lang.Class.getMethod0(Class.java:2670)
at java.lang.Class.getMethod(Class.java:1603)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.isDom3Present(ContextFactory.java:301)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.getE4xImplementationFactory(ContextFactory.java:328)
at net.sourceforge.htmlunit.corejs.javascript.Context.getE4xImplementationFactory(Context.java:2175)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.initStandardObjects(ScriptRuntime.java:222)
at net.sourceforge.htmlunit.corejs.javascript.Context.initStandardObjects(Context.java:1069)
at net.sourceforge.htmlunit.corejs.javascript.Context.initStandardObjects(Context.java:1036)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.init(JavaScriptEngine.java:157)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.access$000(JavaScriptEngine.java:68)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$1.run(JavaScriptEngine.java:130)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:515)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:507)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.initialize(JavaScriptEngine.java:141)
at com.gargoylesoftware.htmlunit.WebClient.initialize(WebClient.java:1107)
at com.gargoylesoftware.htmlunit.WebWindowImpl.setEnclosedPage(WebWindowImpl.java:99)
at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:268)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:127)
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:101)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:442)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:329)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:386)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:371)
at UIUnitTests.setUp(UIUnitTests.groovy:12)
at _GrailsTest_groovy$_run_closure4.doCall(_GrailsTest_groovy:202)
at _GrailsTest_groovy$_run_closure2.doCall(_GrailsTest_groovy:146)
at _GrailsTest_groovy$_run_closure1_closure19.doCall(_GrailsTest_groovy:112)
at _GrailsTest_groovy$_run_closure1.doCall(_GrailsTest_groovy:95)
at TestApp$_run_closure1.doCall(TestApp:66)
at gant.Gant$_dispatch_closure4.doCall(Gant.groovy:324)
at gant.Gant$_dispatch_closure6.doCall(Gant.groovy:334)
at gant.Gant$_dispatch_closure6.doCall(Gant.groovy)
at gant.Gant.withBuildListeners(Gant.groovy:344)
at gant.Gant.this$2$withBuildListeners(Gant.groovy)
at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
at gant.Gant.dispatch(Gant.groovy:334)
at gant.Gant.this$2$dispatch(Gant.groovy)
at gant.Gant.invokeMethod(Gant.groovy)
at gant.Gant.processTargets(Gant.groovy:495)
at gant.Gant.processTargets(Gant.groovy:480)
</pre>

<span class="slashdigglicious">
<a href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fgrails%2Fgrails-and-htmlunit%2F&amp;title=Grails+and+HtmlUnit" title="Slashdot It!"><img src="http://slashdot.org/favicon.ico" height="16" width="16" alt="[Slashdot]" /></a>
<a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fgrails%2Fgrails-and-htmlunit%2F&amp;title=Grails+and+HtmlUnit" title="Digg This Story"><img src="http://digg.com/favicon.ico" width="16" height="16" alt="[Digg]" /></a>
<a href="http://reddit.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fgrails%2Fgrails-and-htmlunit%2F&amp;title=Grails+and+HtmlUnit" title="Reddit"><img src="http://reddit.com/favicon.ico" width="16" height="16" alt="[Reddit]" /></a>
<a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fgrails%2Fgrails-and-htmlunit%2F&amp;title=Grails+and+HtmlUnit" title="Save to del.icio.us" onclick="window.open('http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fgrails%2Fgrails-and-htmlunit%2F&amp;title=Grails+and+HtmlUnit', 'delicious', 'toolbar=no,width=700,height=400'); return false;"><img src="http://images.del.icio.us/static/img/delicious.small.gif" width="16" height="16" alt="[del.icio.us]" /></a>
<a href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fgrails%2Fgrails-and-htmlunit%2F" title="Share on Facebook"><img src="http://www.facebook.com/favicon.ico" width="16" height="16" alt="[Facebook]" /></a>
<a href="http://technorati.com/faves?add=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fgrails%2Fgrails-and-htmlunit%2F" title="Add to my Technorati Favorites"><img src="http://technorati.com/favicon.ico" width="16" height="16" alt="[Technorati]" /></a>
<a href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fgrails%2Fgrails-and-htmlunit%2F&amp;title=Grails+and+HtmlUnit" title="Save to Google Bookmarks"><img src="http://www.google.com/favicon.ico" width="16" height="16" alt="[Google]" /></a>
<a href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fgrails%2Fgrails-and-htmlunit%2F&amp;title=Grails+and+HtmlUnit" title="Stumble it!"><img src="http://www.stumbleupon.com/favicon.ico" width="16" height="16" alt="[StumbleUpon]" /></a>
</span>]]></content:encoded>
			<wfw:commentRss>http://www.reverttoconsole.com/blog/grails/grails-and-htmlunit/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Headless Javascript Unit Testing</title>
		<link>http://www.reverttoconsole.com/blog/hibernate/headless-javascript-unit-testing/</link>
		<comments>http://www.reverttoconsole.com/blog/hibernate/headless-javascript-unit-testing/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 20:06:44 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[UnitTest]]></category>

		<guid isPermaLink="false">http://reverttoconsole.com/?p=253</guid>
		<description><![CDATA[I gave a presentation at work a couple of months ago covering JavaScript scoping considerations for a team of Java developers new to JavaScript. During the presentation, I used Rhino to run the examples. I have seen other projects that use Rhino for unit testing, but they seemed a little heavy weight for what I [...]]]></description>
			<content:encoded><![CDATA[<p>I gave a presentation at work a couple of months ago covering JavaScript scoping considerations for a team of Java developers new to JavaScript.</p>
<p>During the presentation, I used <a href="https://developer.mozilla.org/en/Rhino_documentation">Rhino</a> to run the examples. I have <a href="http://httpunit.sourceforge.net/doc/javascript-support.html">seen</a> <a href="http://www.thefrontside.net/crosscheck">other</a> projects that use Rhino for unit testing, but they seemed a little heavy weight for what I wanted to do, which was to write unit tests for the project&#8217;s JavaScript and integrate it into our continuous integration environment.</p>
<p>The following explains what I did instead that seems to work well. I&#8217;d be interested in hearing any alternative suggestions.</p>
<p><span id="more-214"></span></p>
<p>To start with, I used Rhino and <a href="http://code.google.com/p/envjs/">env.js</a> to create a more full featured javascript runtime environment in the jvm, as <a href="http://ejohn.org/projects/bringing-the-browser-to-the-server/">John Resig wrote about a while back</a>. <a href="http://reverttoconsole.com/2008/09/bringing-the-browser-to-the-server-just-discovered-this/">I wrote about this a while back</a>.</p>
<p>Because I wanted to run Rhino and execute JavaScript inside a unit test, it was easiest for me to modify <a href="http://mxr.mozilla.org/mozilla/source/js/rhino/examples/Shell.java">the Shell example provided by the Rhino group</a>.</p>
<p>Note: WordPress was blowing up on me when I tried to show the entire class&#8230; this is the abbreviated version with most of my changes.</p>
<pre class="brush: java">
public class Shell extends ScriptableObject {

    private boolean quitting;
    private static final long serialVersionUID = -5638074146250193112L;
    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger.getLogger(Shell.class);

    @Override
    public String getClassName() {
        return &quot;global&quot;;
    }

    public static void print(final Context cx, final Scriptable thisObj, final Object[] args, final Function funObj) {
        for (int i = 0; i &lt; args.length; i++) {
            if (i &gt; 0) {
                logger.info(&quot; &quot;);
            }

            // Convert the arbitrary JavaScript value into a string form.
            String s = Context.toString(args[i]);

            logger.info(s);
        }
    }

    public void quit() {
        quitting = true;
    }

    public static double version(final Context cx, final Scriptable thisObj,
            final Object[] args, final Function funObj) {
        double result = cx.getLanguageVersion();
        if (args.length &gt; 0) {
            double d = Context.toNumber(args[0]);
            cx.setLanguageVersion((int) d);
        }
        return result;

    }

    public static void load(final Context cx, final Scriptable thisObj,
            final Object[] args, final Function funObj) {
        Shell shell = (Shell) getTopLevelScope(thisObj);
        for (int i = 0; i &lt; args.length; i++) {
            shell.processSource(cx, Context.toString(args[i]));
        }
    }

    public static void assertEquals(final String expected, final String actual) {
        Assert.assertEquals(expected, actual);
    }

    public static void assertNotNull(final Object object) {
        Assert.assertTrue(!(object instanceof org.mozilla.javascript.Undefined));
        Assert.assertTrue(&quot;Provided object is null&quot;, object != null);
    }

    public static void assertTrue(final boolean condition) {
        Assert.assertTrue(condition);
    }

    public static void assertFalse(final boolean condition) {
        Assert.assertFalse(condition);
    }

}
</pre>
<p>Next I created an Abstract JUnit test that loads my Javascript environment. My setUp method looks something like this:</p>
<pre class="brush: java">
    protected void setUp() throws Exception {
        super.setUp();
        context = Context.enter();
        try {

            context.setOptimizationLevel(-1);
            shell = new Shell();
            context.initStandardObjects(shell);

            // Define some global functions particular to the shell. Note
            // that these functions are not part of ECMA.
            String[] names = { &quot;print&quot;, &quot;quit&quot;, &quot;version&quot;, &quot;load&quot;,
                    &quot;assertEquals&quot;, &quot;assertNotNull&quot;,
                    &quot;assertTrue&quot;, &quot;assertFalse&quot;};
            shell.defineFunctionProperties(names, Shell.class,
                                           ScriptableObject.DONTENUM);
            shell.processSource(context, &quot;src/test/resources/scripts/startup.js&quot;);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
</pre>
<p>This initializes the JavaScript context and loads the environment-specific javascript I want to test. In my case I wanted to test code that uses <a href="http://extjs.com/">ExtJS</a>. My startup.js script looks like this:</p>
<pre class="brush: javascript">
load(&#039;src/test/resources/scripts/env.js&#039;);
window.location=&#039;src/test/resources/scripts/empty.html&#039;;
document=&#039;src/test/resources/scripts/empty.html&#039;;
load(&#039;src/main/javascript/extjs/adapter/ext/ext-base.js&#039;);
load(&#039;src/main/javascript/extjs/ext-all-debug.js&#039;);
load(&#039;src/test/resources/scripts/resource.js&#039;);
load(&#039;src/test/resources/scripts/testdata.js&#039;);
load(&#039;target/myappcode.js&#039;);
</pre>
<p>Order is important here. First, I load env.js. Next I set values for window.location and document. I don&#8217;t use either explicitly, but some of the other files look for them. Then I load extjs, and my data and app code.</p>
<p>The best part about this arrangement is the predefined functions provided by the shell class. I can easily reference junit assert statements from within my javascript test.</p>
<p>Here is an example JUnit class:</p>
<pre class="brush: java">
public class PhoneNumberTest extends AbstractJSTestCase {

    public PhoneNumberTest(final String name) {
        super(name);
    }

   public void testPhoneCountryCodeRender() {
       processScript(&quot;src/test/javascript/PhoneCountryCodeRenderTest.js&quot;);
   }

   public void testCreateGrid() {
       processScript(&quot;src/test/javascript/PhoneNumberCreateGridTest.js&quot;);
   }

}
</pre>
<p>and the javascript test code it calls:</p>
<pre class="brush: javascript">

var phoneNumber = new edit.form.PhoneNumber();

assertNotNull(phoneNumber);
assertNotNull(profile);

var grid = phoneNumber.createGrid(profile);

assertNotNull(grid);

var store = grid.store;

assertNotNull(store);

assertTrue(store.getCount() === 2);

var record1 = store.getAt(0);
assertNotNull(record1);
var record2 = store.getAt(1);
assertNotNull(record2);

print(record1.data.phoneNumber);
print(record2.data.phoneNumber);

print(record1.data.extension);
print(record2.data.extension);

// Now remove the access code from the phone number
record1.data.phoneNumber = PhoneUtils.removeAccessCode(record1);
print(record1.data.phoneNumber);

record2.data.phoneNumber = PhoneUtils.removeAccessCode(record2);
print(record2.data.phoneNumber);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.reverttoconsole.com/blog/hibernate/headless-javascript-unit-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Disabling Ganymede Plugins</title>
		<link>http://www.reverttoconsole.com/blog/java/disabling-ganymede-plugins/</link>
		<comments>http://www.reverttoconsole.com/blog/java/disabling-ganymede-plugins/#comments</comments>
		<pubDate>Thu, 20 Nov 2008 19:28:11 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[UnitTest]]></category>

		<guid isPermaLink="false">http://reverttoconsole.com/?p=246</guid>
		<description><![CDATA[In Ganymede, the Manage Configuration option Eclipse users were used to using is gone. Help->Software Updates is for just that now. So how do you go about disabling a plugin now? Well the good news is you no longer have to wait 5 minutes for the Manage Configuration window to load! Ganymede Eclipse now provides [...]]]></description>
			<content:encoded><![CDATA[<p>In Ganymede, the Manage Configuration option Eclipse users were used to using is gone. Help->Software Updates is for just that now.</p>
<p>So how do you go about disabling a plugin now? Well the good news is you no longer have to wait 5 minutes for the Manage Configuration window to load!</p>
<p>Ganymede Eclipse now provides a view, the Plug-In Registry, where you can disable crappy plugins like ajdt.</p>
<p>In my case, I was unable to run unit tests in Eclipse until I disabled org.eclipse.ajdt.ui. Why on earth I would need ajdt to run a unit test I have no idea.</p>
<p>FYI, there is a new <a href="http://www.eclipse.org/ajdt/downloads/">AJDT update site here</a>. Ajdt 1.6.1 fixed the problem.</p>

<span class="slashdigglicious">
<a href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fdisabling-ganymede-plugins%2F&amp;title=Disabling+Ganymede+Plugins" title="Slashdot It!"><img src="http://slashdot.org/favicon.ico" height="16" width="16" alt="[Slashdot]" /></a>
<a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fdisabling-ganymede-plugins%2F&amp;title=Disabling+Ganymede+Plugins" title="Digg This Story"><img src="http://digg.com/favicon.ico" width="16" height="16" alt="[Digg]" /></a>
<a href="http://reddit.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fdisabling-ganymede-plugins%2F&amp;title=Disabling+Ganymede+Plugins" title="Reddit"><img src="http://reddit.com/favicon.ico" width="16" height="16" alt="[Reddit]" /></a>
<a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fdisabling-ganymede-plugins%2F&amp;title=Disabling+Ganymede+Plugins" title="Save to del.icio.us" onclick="window.open('http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fdisabling-ganymede-plugins%2F&amp;title=Disabling+Ganymede+Plugins', 'delicious', 'toolbar=no,width=700,height=400'); return false;"><img src="http://images.del.icio.us/static/img/delicious.small.gif" width="16" height="16" alt="[del.icio.us]" /></a>
<a href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fdisabling-ganymede-plugins%2F" title="Share on Facebook"><img src="http://www.facebook.com/favicon.ico" width="16" height="16" alt="[Facebook]" /></a>
<a href="http://technorati.com/faves?add=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fdisabling-ganymede-plugins%2F" title="Add to my Technorati Favorites"><img src="http://technorati.com/favicon.ico" width="16" height="16" alt="[Technorati]" /></a>
<a href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fdisabling-ganymede-plugins%2F&amp;title=Disabling+Ganymede+Plugins" title="Save to Google Bookmarks"><img src="http://www.google.com/favicon.ico" width="16" height="16" alt="[Google]" /></a>
<a href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fdisabling-ganymede-plugins%2F&amp;title=Disabling+Ganymede+Plugins" title="Stumble it!"><img src="http://www.stumbleupon.com/favicon.ico" width="16" height="16" alt="[StumbleUpon]" /></a>
</span>]]></content:encoded>
			<wfw:commentRss>http://www.reverttoconsole.com/blog/java/disabling-ganymede-plugins/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Automated Unit Testing with DBUnit, Hsqldb in Spring &amp; Hibernate projects</title>
		<link>http://www.reverttoconsole.com/blog/hibernate/automated-unit-testing-with-dbunit-hsqldb-in-spring-hibernate-projects/</link>
		<comments>http://www.reverttoconsole.com/blog/hibernate/automated-unit-testing-with-dbunit-hsqldb-in-spring-hibernate-projects/#comments</comments>
		<pubDate>Sun, 08 Jun 2008 04:16:11 +0000</pubDate>
		<dc:creator>Priyatam</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Hsqldb]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[UnitTest]]></category>

		<guid isPermaLink="false">http://reverttoconsole.com/archives/157</guid>
		<description><![CDATA[If your project has a lot of unit testing in Java and you use continuous builds (CruiseControl) to automate, it is often difficult to automate tests which connect to Database (Dao and integration tests). For most companies, having a dedicated Oracle schema for testing is expensive. Also, sometimes, developers need their own instances of schemas [...]]]></description>
			<content:encoded><![CDATA[<p>If your project has a lot of unit testing in Java and you use continuous builds (CruiseControl) to automate, it is often difficult to automate tests which connect to Database (Dao and integration tests). For most companies,  having a dedicated Oracle schema for testing is expensive. Also, sometimes, developers need their own instances of schemas to test on, which again is not possible with a corporate DBA on Oracle.  Obviously writing unit tests connecting to the same instance where dev environment is setup is extremely unpleasant.</p>
<p>What do you do then?</p>
<p>Welcome to <a href="http://schemamule.sourceforge.net/index.html">Schemamule</a>. It&#8217;s a tiny jar file and has a single ant task which copies the entire Oracle Schema and generates a Hsqldb database. <strong>YES. </strong>It replicates an entire Oracle Schema into a hsqldb in seconds! You can either use it as an in memory database for your automated unit testing or as dev only local database for unit testing. For more details, read the original blog <a href="http://www.moseshohman.com/blog/2006/05/23/schemamule-10-released/">here</a>.</p>
<p>Let me iterate the advantages:-</p>
<p>&#8211; Can write database dependent test cases<br />
&#8211; Need not connect to a network for running tests<br />
&#8211; Database tests run at blazing speed, as now they&#8217;re on hsqldb</p>
<p>I worked with one of the developers &#8212; Rhett Sutphin, in my earlier project. He&#8217;s a brilliant programmer. I&#8217;ve also reused it in one of my projects at a financial firm, it works great and is amazingly productive once you have setup a framework based on <a href="http://dbunit.sourceforge.net/">dbunit</a>. Wait, what is dbunit? I&#8217;m not going into the details but it&#8217;s a no-brainer that if you use hibernate and junit, you better get started on using dbunit for unit testing backed by xml datasets instead of the cumbersome setup/teardown methods to populate data for each junit test case</p>
<p>We have a sample project checked into RTC&#8217;s sourceforge (We call it <a href="http://sourceforge.net/projects/helloworld2/" target="_blank">helloworld2</a>), with custom classes on top of DBUnit (most of it has been written by the author of Schemamule).</p>
<p>My example considers of an entity, Foo with FooDao, mapped to a table Foo<em>.</em> For simplicity, I assume one field called name. As a step by step tutorial on extending DBUnit Unit Testing ,  I&#8217;ll show how simple it is to write a Dao based unit test case using Dbunit, Spring and Hibernate (of course on our generated hsqldb)</p>
<p>Step 1:Create an Hsqldb database from Oracle</p>
<p>Download <a href="https://sourceforge.net/project/showfiles.php?group_id=166480">Schemamule</a> and run the following ant task</p>
<pre class="brush: xml">
&lt;schemacopy xmlns=&quot;http://bioinformatics.northwestern.edu/schemamule&quot;&gt;
    &lt;to uri=&quot;jdbc:hsqldb:file:${basedir}/hsqldb/mydb&quot; username=&quot;sa&quot;&gt;
    &lt;from uri=&quot;jdbc:oracle:thin:mydb&quot; username=&quot;scott&quot; password=&quot;tiger&quot;&gt;
&lt;/from&gt;
&lt;/to&gt;&lt;/schemacopy&gt;
</pre>
<p>Step 2: Setup DBUnit<br />
Download the latest version of <a href="http://dbunit.sourceforge.net/">dbunit </a>(jdk1.4 or jdk5)</p>
<p>Step 3: Download the svn repository (0r copy by browsing svn) Revert To Console&#8217;s sample test utility classes and example DBUnit tutorial from sourceforge <a href="http://sourceforge.net/svn/?group_id=200388">here</a>.</p>
<p>- Checkout under projects/unit-test.<br />
- I&#8217;m not providing a complete working example with spring/hibernate configuration, as I presume you are running under an environment where you already have a this configured with an Oracle database.</p>
<p>- the utility classes include the following classes<br />
- CoreTestCase &#8212; An extension on JUnit with much more useful assert statements<br />
- ApplicationTestCase &#8212; All the unit tests in your Spring/Hibernate project should extend this class<br />
- DaoTestCase &#8212; All your Dao Tests should extend this class<br />
- ContextTools &#8211; The class which reads your applicationContext-xyz.xml file, where hibernate session is configured<br />
- Sample Dao and entity</p>
<p>Configure these files in your project, which can read the applicationContext from the classpath (for the tests to work!)</p>
<p>Step 4:With all this configured, let&#8217;s write a simple Dao Test</p>
<pre class="brush: java">
public class FooDAOTest extends DaoTestCase {
    FooDao dao = (FooDao)getApplicationContext().getBean(&amp;quot;fooDao&amp;quot;);
         // assuming that your spring config has a bean-id &#039;fooDao&#039;
	 public void testGetById() throws Exception {
            Foo foo = (Foo)dao.getById(1);
            assertEquals(&amp;quot;revert to console&amp;quot;, foo.getName());
	 }
}</pre>
<p>By Default, DBUnit assumes a test data xml file in the same testdata/FooDaoTest.xml (you can override this). Now, lets see the dataset</p>
<pre class="brush: xml">
&lt;dataset&gt;
&lt;table name=&quot;FOO&quot;&gt;&lt;column&gt;name&lt;/column&gt;
        &lt;row&gt;
            &lt;value&gt;revert to console&lt;/value&gt;
         &lt;/row&gt;&lt;/table&gt;
&lt;/dataset&gt;
</pre>
<p>That&#8217;s it! For all the configuration that I&#8217;ve showed you (which will take some time to setup), all you need to do to write another test is, to write another method with testdata! This setup will create,drop the testdata on the generated HsqlDb schema before/after each test. What more, because, it is in memory, you can even run, Dao Tests and complete round trip unit tests automated in your nightly builds, without coupling to an existing Oracle Schema.</p>
<p>There are some limitations to Schemamule though. I had to customize it for my project as we were using DB Views. I finally got it working after modifying the sourcecode. Unfortunately I could never put that back into Schemamule as a patch but I recommend downloading the source and try it for yourself. It&#8217;s a great example on understanding how Spring&#8217;s Jdbc template works and can be extended.</p>

<span class="slashdigglicious">
<a href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fhibernate%2Fautomated-unit-testing-with-dbunit-hsqldb-in-spring-hibernate-projects%2F&amp;title=Automated+Unit+Testing+with+DBUnit%2C+Hsqldb+in+Spring+%26%23038%3B+Hibernate+projects" title="Slashdot It!"><img src="http://slashdot.org/favicon.ico" height="16" width="16" alt="[Slashdot]" /></a>
<a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fhibernate%2Fautomated-unit-testing-with-dbunit-hsqldb-in-spring-hibernate-projects%2F&amp;title=Automated+Unit+Testing+with+DBUnit%2C+Hsqldb+in+Spring+%26%23038%3B+Hibernate+projects" title="Digg This Story"><img src="http://digg.com/favicon.ico" width="16" height="16" alt="[Digg]" /></a>
<a href="http://reddit.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fhibernate%2Fautomated-unit-testing-with-dbunit-hsqldb-in-spring-hibernate-projects%2F&amp;title=Automated+Unit+Testing+with+DBUnit%2C+Hsqldb+in+Spring+%26%23038%3B+Hibernate+projects" title="Reddit"><img src="http://reddit.com/favicon.ico" width="16" height="16" alt="[Reddit]" /></a>
<a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fhibernate%2Fautomated-unit-testing-with-dbunit-hsqldb-in-spring-hibernate-projects%2F&amp;title=Automated+Unit+Testing+with+DBUnit%2C+Hsqldb+in+Spring+%26%23038%3B+Hibernate+projects" title="Save to del.icio.us" onclick="window.open('http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fhibernate%2Fautomated-unit-testing-with-dbunit-hsqldb-in-spring-hibernate-projects%2F&amp;title=Automated+Unit+Testing+with+DBUnit%2C+Hsqldb+in+Spring+%26%23038%3B+Hibernate+projects', 'delicious', 'toolbar=no,width=700,height=400'); return false;"><img src="http://images.del.icio.us/static/img/delicious.small.gif" width="16" height="16" alt="[del.icio.us]" /></a>
<a href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fhibernate%2Fautomated-unit-testing-with-dbunit-hsqldb-in-spring-hibernate-projects%2F" title="Share on Facebook"><img src="http://www.facebook.com/favicon.ico" width="16" height="16" alt="[Facebook]" /></a>
<a href="http://technorati.com/faves?add=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fhibernate%2Fautomated-unit-testing-with-dbunit-hsqldb-in-spring-hibernate-projects%2F" title="Add to my Technorati Favorites"><img src="http://technorati.com/favicon.ico" width="16" height="16" alt="[Technorati]" /></a>
<a href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fhibernate%2Fautomated-unit-testing-with-dbunit-hsqldb-in-spring-hibernate-projects%2F&amp;title=Automated+Unit+Testing+with+DBUnit%2C+Hsqldb+in+Spring+%26%23038%3B+Hibernate+projects" title="Save to Google Bookmarks"><img src="http://www.google.com/favicon.ico" width="16" height="16" alt="[Google]" /></a>
<a href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fhibernate%2Fautomated-unit-testing-with-dbunit-hsqldb-in-spring-hibernate-projects%2F&amp;title=Automated+Unit+Testing+with+DBUnit%2C+Hsqldb+in+Spring+%26%23038%3B+Hibernate+projects" title="Stumble it!"><img src="http://www.stumbleupon.com/favicon.ico" width="16" height="16" alt="[StumbleUpon]" /></a>
</span>]]></content:encoded>
			<wfw:commentRss>http://www.reverttoconsole.com/blog/hibernate/automated-unit-testing-with-dbunit-hsqldb-in-spring-hibernate-projects/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Commons StringUtils Is Your Friend</title>
		<link>http://www.reverttoconsole.com/blog/codesnippets/commons-stringutils-is-your-friend/</link>
		<comments>http://www.reverttoconsole.com/blog/codesnippets/commons-stringutils-is-your-friend/#comments</comments>
		<pubDate>Wed, 14 May 2008 14:34:08 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[CodeSnippets]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[UnitTest]]></category>

		<guid isPermaLink="false">http://reverttoconsole.com/archives/181</guid>
		<description><![CDATA[I have just completed a test for a method that parses a forward-slash delimited String: private static final String PROJECTID_STR_1 = &#34;&#34;; private static final String PROJECTID_STR_2 = &#34;101/103/102/104/105/106/107&#34;; private static final String PROJECTID_STR_3 = &#34;///103/104&#34;; private static final String PROJECTID_STR_4 = &#34;a/b/102/104/105/106/107&#34;; @Test public void testParseProjectIds1() { List&#60;integer&#62; projectIdList1 = getViewBillingReportController().parseProjectIds(PROJECTID_STR_1); assertTrue(projectIdList1.size() == 0); [...]]]></description>
			<content:encoded><![CDATA[<p>I have just completed a test for a method that parses a forward-slash delimited String:</p>
<pre class="brush: java">
	private static final String PROJECTID_STR_1 = &quot;&quot;;
	private static final String PROJECTID_STR_2 = &quot;101/103/102/104/105/106/107&quot;;
	private static final String PROJECTID_STR_3 = &quot;///103/104&quot;;
	private static final String PROJECTID_STR_4 = &quot;a/b/102/104/105/106/107&quot;;

	@Test
	public void testParseProjectIds1() {
		List&lt;integer&gt; projectIdList1 =
			getViewBillingReportController().parseProjectIds(PROJECTID_STR_1);
		assertTrue(projectIdList1.size() == 0);
	}

	@Test
	public void testParseProjectIds2() {
		List&lt;integer&gt; projectIdList2 =
			getViewBillingReportController().parseProjectIds(PROJECTID_STR_2);
		assertTrue(projectIdList2.size() == 7);
	}

	@Test
	public void testParseProjectIds3() {
		List&lt;integer&gt; projectIdList3 =
			getViewBillingReportController().parseProjectIds(PROJECTID_STR_3);
		assertTrue(projectIdList3.size() == 2);
	}

	@Test
	public void testParseProjectIds4() {
		List&lt;integer&gt; projectIdList4 =
			getViewBillingReportController().parseProjectIds(PROJECTID_STR_4);
		assertTrue(projectIdList4.size() == 5);
	}
</pre>
<p>This test initially failed all but one of the assertions (it passed the happy path).</p>
<p>The initial method looked like this:</p>
<pre class="brush: java">
    public List&lt;integer&gt; parseProjectIds(String projectIdsStr) {
    	ArrayList&lt;integer&gt; result = new ArrayList&lt;integer&gt;();
    	int lastDelimiterLoc = -1;
    	boolean done = false;
    	while (!done) {
    		int delimiterLoc = projectIdsStr.indexOf(&#039;/&#039;, lastDelimiterLoc + 1);
    		if (delimiterLoc &gt;= 0) {
    			String idStr = projectIdsStr.substring(lastDelimiterLoc + 1, delimiterLoc);
    			Integer id = new Integer(idStr);
   			result.add(id);
    			lastDelimiterLoc = delimiterLoc;
    		} else {
    			delimiterLoc = projectIdsStr.length();
    			String idStr = projectIdsStr.substring(lastDelimiterLoc + 1, delimiterLoc);
    			Integer id = new Integer(idStr);
    			result.add(id);
    			done = true;
    		}
        }
    	return result;
    }
</pre>
<p>I don&#8217;t know why the developer on this would have done it this way, since we already had <a href="http://commons.apache.org/lang/">Commons Lang</a> available in the classpath. But after a quick refactoring I came up with this:</p>
<pre class="brush: java">
public List&lt;integer&gt; parseProjectIds(String projectIdsStr) {
    	ArrayList&lt;integer&gt; result = new ArrayList&lt;integer&gt;();
    	if(StringUtils.isNotBlank(projectIdsStr)) {
    		String[] parsedList = StringUtils.split(projectIdsStr, &quot;/&quot;);
    		for( int i =0; i &lt; parsedList.length; i++) {
    			if( StringUtils.isNotEmpty(parsedList[i])){
    				if( StringUtils.isNumeric(parsedList[i])) {
    					result.add(new Integer(parsedList[i]));
    				}
    			}
    		}
    	}
    	return result;
    }
</pre>
<p>This passed all my tests, and seems much more readable. This is how <a href="http://commons.apache.org/lang/api/org/apache/commons/lang/StringUtils.html">StringUtils</a> rocks!</p>
<p>PS- StringUtils.isNotBlank also handles null values.</p>

<span class="slashdigglicious">
<a href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fcodesnippets%2Fcommons-stringutils-is-your-friend%2F&amp;title=Commons+StringUtils+Is+Your+Friend" title="Slashdot It!"><img src="http://slashdot.org/favicon.ico" height="16" width="16" alt="[Slashdot]" /></a>
<a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fcodesnippets%2Fcommons-stringutils-is-your-friend%2F&amp;title=Commons+StringUtils+Is+Your+Friend" title="Digg This Story"><img src="http://digg.com/favicon.ico" width="16" height="16" alt="[Digg]" /></a>
<a href="http://reddit.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fcodesnippets%2Fcommons-stringutils-is-your-friend%2F&amp;title=Commons+StringUtils+Is+Your+Friend" title="Reddit"><img src="http://reddit.com/favicon.ico" width="16" height="16" alt="[Reddit]" /></a>
<a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fcodesnippets%2Fcommons-stringutils-is-your-friend%2F&amp;title=Commons+StringUtils+Is+Your+Friend" title="Save to del.icio.us" onclick="window.open('http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fcodesnippets%2Fcommons-stringutils-is-your-friend%2F&amp;title=Commons+StringUtils+Is+Your+Friend', 'delicious', 'toolbar=no,width=700,height=400'); return false;"><img src="http://images.del.icio.us/static/img/delicious.small.gif" width="16" height="16" alt="[del.icio.us]" /></a>
<a href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fcodesnippets%2Fcommons-stringutils-is-your-friend%2F" title="Share on Facebook"><img src="http://www.facebook.com/favicon.ico" width="16" height="16" alt="[Facebook]" /></a>
<a href="http://technorati.com/faves?add=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fcodesnippets%2Fcommons-stringutils-is-your-friend%2F" title="Add to my Technorati Favorites"><img src="http://technorati.com/favicon.ico" width="16" height="16" alt="[Technorati]" /></a>
<a href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fcodesnippets%2Fcommons-stringutils-is-your-friend%2F&amp;title=Commons+StringUtils+Is+Your+Friend" title="Save to Google Bookmarks"><img src="http://www.google.com/favicon.ico" width="16" height="16" alt="[Google]" /></a>
<a href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fcodesnippets%2Fcommons-stringutils-is-your-friend%2F&amp;title=Commons+StringUtils+Is+Your+Friend" title="Stumble it!"><img src="http://www.stumbleupon.com/favicon.ico" width="16" height="16" alt="[StumbleUpon]" /></a>
</span>]]></content:encoded>
			<wfw:commentRss>http://www.reverttoconsole.com/blog/codesnippets/commons-stringutils-is-your-friend/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Limitations of AbstractTransactionalDataSourceSpringContextTests</title>
		<link>http://www.reverttoconsole.com/blog/spring/limitations-of-abstracttransactionaldatasourcespringcontexttests/</link>
		<comments>http://www.reverttoconsole.com/blog/spring/limitations-of-abstracttransactionaldatasourcespringcontexttests/#comments</comments>
		<pubDate>Tue, 13 May 2008 21:35:13 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Spring]]></category>
		<category><![CDATA[UnitTest]]></category>

		<guid isPermaLink="false">http://reverttoconsole.com/archives/180</guid>
		<description><![CDATA[This has apparently gotten a fair amount of attention over the last year or so, but I never encountered it until today- call me lucky. Here&#8217;s the situation, you&#8217;re using Spring MVC and you have a controller that extends another controller (A->B). Your unit test extends AbstractTransactionalDataSourceSpringContextTests, and makes use of autowiring. You write your [...]]]></description>
			<content:encoded><![CDATA[<p>This has apparently gotten a fair amount of attention over the last year or so, but I never encountered it until today- call me lucky. Here&#8217;s the situation, you&#8217;re using <a href="http://www.springframework.org/about">Spring MVC</a> and you have a controller that extends another controller (A->B).</p>
<p>Your unit test extends <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/test/AbstractTransactionalDataSourceSpringContextTests.html">AbstractTransactionalDataSourceSpringContextTests</a>, and makes use of <a href="http://groups.google.com/group/EtoE/browse_thread/thread/9306c581470b47cb/31a62772d677bd12">autowiring</a>.</p>
<p>You write your test, run it, and receive something like this:</p>
<pre>
 Unsatisfied dependency expressed through bean property 'aController':
 No unique bean of type [org.reverttoconsole.majorapplication.controller.aController] is defined:
  expected single matching bean but found 2: [aController.htm, bController.htm]
</pre>
<p>I tried using <a href="http://static.springframework.org/spring/docs/2.5.x/reference/testing.html">annotations</a>, using @Autowire and @Resource, but in the end had to refactor my code to fit the tests&#8230; by creating an Abstract class that both controllers could extend.</p>

<span class="slashdigglicious">
<a href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fspring%2Flimitations-of-abstracttransactionaldatasourcespringcontexttests%2F&amp;title=Limitations+of+AbstractTransactionalDataSourceSpringContextTests" title="Slashdot It!"><img src="http://slashdot.org/favicon.ico" height="16" width="16" alt="[Slashdot]" /></a>
<a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fspring%2Flimitations-of-abstracttransactionaldatasourcespringcontexttests%2F&amp;title=Limitations+of+AbstractTransactionalDataSourceSpringContextTests" title="Digg This Story"><img src="http://digg.com/favicon.ico" width="16" height="16" alt="[Digg]" /></a>
<a href="http://reddit.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fspring%2Flimitations-of-abstracttransactionaldatasourcespringcontexttests%2F&amp;title=Limitations+of+AbstractTransactionalDataSourceSpringContextTests" title="Reddit"><img src="http://reddit.com/favicon.ico" width="16" height="16" alt="[Reddit]" /></a>
<a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fspring%2Flimitations-of-abstracttransactionaldatasourcespringcontexttests%2F&amp;title=Limitations+of+AbstractTransactionalDataSourceSpringContextTests" title="Save to del.icio.us" onclick="window.open('http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fspring%2Flimitations-of-abstracttransactionaldatasourcespringcontexttests%2F&amp;title=Limitations+of+AbstractTransactionalDataSourceSpringContextTests', 'delicious', 'toolbar=no,width=700,height=400'); return false;"><img src="http://images.del.icio.us/static/img/delicious.small.gif" width="16" height="16" alt="[del.icio.us]" /></a>
<a href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fspring%2Flimitations-of-abstracttransactionaldatasourcespringcontexttests%2F" title="Share on Facebook"><img src="http://www.facebook.com/favicon.ico" width="16" height="16" alt="[Facebook]" /></a>
<a href="http://technorati.com/faves?add=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fspring%2Flimitations-of-abstracttransactionaldatasourcespringcontexttests%2F" title="Add to my Technorati Favorites"><img src="http://technorati.com/favicon.ico" width="16" height="16" alt="[Technorati]" /></a>
<a href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fspring%2Flimitations-of-abstracttransactionaldatasourcespringcontexttests%2F&amp;title=Limitations+of+AbstractTransactionalDataSourceSpringContextTests" title="Save to Google Bookmarks"><img src="http://www.google.com/favicon.ico" width="16" height="16" alt="[Google]" /></a>
<a href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fspring%2Flimitations-of-abstracttransactionaldatasourcespringcontexttests%2F&amp;title=Limitations+of+AbstractTransactionalDataSourceSpringContextTests" title="Stumble it!"><img src="http://www.stumbleupon.com/favicon.ico" width="16" height="16" alt="[StumbleUpon]" /></a>
</span>]]></content:encoded>
			<wfw:commentRss>http://www.reverttoconsole.com/blog/spring/limitations-of-abstracttransactionaldatasourcespringcontexttests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The size of an object instance at runtime &#8211; pre Java 1.5</title>
		<link>http://www.reverttoconsole.com/blog/java/the-size-of-an-object-instance-at-runtime-pre-java-15/</link>
		<comments>http://www.reverttoconsole.com/blog/java/the-size-of-an-object-instance-at-runtime-pre-java-15/#comments</comments>
		<pubDate>Tue, 13 Nov 2007 17:42:56 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[UnitTest]]></category>

		<guid isPermaLink="false">http://reverttoconsole.com/archives/147</guid>
		<description><![CDATA[In Java 1.5, Sun provides us with a miracle method that is long overdue: long java.lang.instrument.Instrumentation.getObjectSize (Object objectToSize) However, this is still limited in that it only returns the total size of the Object, not the details about the Object size in regard to its composition. Enter monq.jar from Whatizit. There&#8217;s a Sizeof class in [...]]]></description>
			<content:encoded><![CDATA[<p>In Java 1.5, Sun provides us with a miracle method that is long overdue:<br />
long java.lang.instrument.Instrumentation.getObjectSize (Object objectToSize)</p>
<p>However, this is still limited in that it only returns the total size of the Object, not the details about the Object size in regard to its composition.</p>
<p>Enter <a href="ftp://ftp.ebi.ac.uk/pub/software/textmining/monq/monq.jar">monq.jar</a> from<br />
<a href="http://www.ebi.ac.uk/Rebholz-srv/whatizit/software">Whatizit</a>.</p>
<p>There&#8217;s a <code>Sizeof</code> class in monq.jar with a static <code>sizeof</code> method that walks through an Object instance at runtime and counts up the bytes in use for all its elements and then returns a HashTable.  The results seem to be consistent with what I&#8217;ve seen by using the java.lang.Runtime class to estimate heap size.  The Sizeof class also provides a convenient <code>printTypes</code> method which can be used to dump the HashTable data to an OutputStream (such as System.out).  This allows you to easily see which nodes of a given Object tree might be prone to causing trouble.  Though it cannot be 100% accurate, it&#8217;s probably very close.</p>
<p>Here&#8217;s a simple usage example:</p>
<pre>
public void printObjectSizeTable(Object object) {
   Sizeof.printTypes(System.out, Sizeof.sizeof(object));
}
</pre>
<p>The above will print out a table consisting of four columns, which are:</p>
<ul>
<li>Count of the particular type</li>
<li>Total byte size of the type</li>
<li>
Containing type class name</li>
<li>Type class name</li>
</ul>
<p>Example output using a populated ArrayList as the parameter to the method above:</p>
<pre>
26 1516 java.lang.Object; java.util.ArrayList
25 500 java.util.ArrayList java.util.ArrayList
1 20 java.util.ArrayList monq.stuff.Sizeof$Root
52 2036 TOTAL
</pre>
<p>I might just start integrating this into some junit tests.  I certainly believe that it will be very useful in determining the cause of performance problems.</p>
<p>-&#8221;That&#8217;s all for now folks&#8221;</p>

<span class="slashdigglicious">
<a href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fthe-size-of-an-object-instance-at-runtime-pre-java-15%2F&amp;title=The+size+of+an+object+instance+at+runtime+%26%238211%3B+pre+Java+1.5" title="Slashdot It!"><img src="http://slashdot.org/favicon.ico" height="16" width="16" alt="[Slashdot]" /></a>
<a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fthe-size-of-an-object-instance-at-runtime-pre-java-15%2F&amp;title=The+size+of+an+object+instance+at+runtime+%26%238211%3B+pre+Java+1.5" title="Digg This Story"><img src="http://digg.com/favicon.ico" width="16" height="16" alt="[Digg]" /></a>
<a href="http://reddit.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fthe-size-of-an-object-instance-at-runtime-pre-java-15%2F&amp;title=The+size+of+an+object+instance+at+runtime+%26%238211%3B+pre+Java+1.5" title="Reddit"><img src="http://reddit.com/favicon.ico" width="16" height="16" alt="[Reddit]" /></a>
<a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fthe-size-of-an-object-instance-at-runtime-pre-java-15%2F&amp;title=The+size+of+an+object+instance+at+runtime+%26%238211%3B+pre+Java+1.5" title="Save to del.icio.us" onclick="window.open('http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fthe-size-of-an-object-instance-at-runtime-pre-java-15%2F&amp;title=The+size+of+an+object+instance+at+runtime+%26%238211%3B+pre+Java+1.5', 'delicious', 'toolbar=no,width=700,height=400'); return false;"><img src="http://images.del.icio.us/static/img/delicious.small.gif" width="16" height="16" alt="[del.icio.us]" /></a>
<a href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fthe-size-of-an-object-instance-at-runtime-pre-java-15%2F" title="Share on Facebook"><img src="http://www.facebook.com/favicon.ico" width="16" height="16" alt="[Facebook]" /></a>
<a href="http://technorati.com/faves?add=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fthe-size-of-an-object-instance-at-runtime-pre-java-15%2F" title="Add to my Technorati Favorites"><img src="http://technorati.com/favicon.ico" width="16" height="16" alt="[Technorati]" /></a>
<a href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fthe-size-of-an-object-instance-at-runtime-pre-java-15%2F&amp;title=The+size+of+an+object+instance+at+runtime+%26%238211%3B+pre+Java+1.5" title="Save to Google Bookmarks"><img src="http://www.google.com/favicon.ico" width="16" height="16" alt="[Google]" /></a>
<a href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fthe-size-of-an-object-instance-at-runtime-pre-java-15%2F&amp;title=The+size+of+an+object+instance+at+runtime+%26%238211%3B+pre+Java+1.5" title="Stumble it!"><img src="http://www.stumbleupon.com/favicon.ico" width="16" height="16" alt="[StumbleUpon]" /></a>
</span>]]></content:encoded>
			<wfw:commentRss>http://www.reverttoconsole.com/blog/java/the-size-of-an-object-instance-at-runtime-pre-java-15/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Java Integration Testing with HSQLDB</title>
		<link>http://www.reverttoconsole.com/blog/hsqldb/java-integration-testing-with-hsqldb/</link>
		<comments>http://www.reverttoconsole.com/blog/hsqldb/java-integration-testing-with-hsqldb/#comments</comments>
		<pubDate>Fri, 18 May 2007 18:40:30 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Hsqldb]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Servers]]></category>
		<category><![CDATA[UnitTest]]></category>

		<guid isPermaLink="false">http://reverttoconsole.com/archives/108</guid>
		<description><![CDATA[I&#8217;m a fairly recent convert to unit testing, but still when I&#8217;m under pressure to get something finished, I skip right to integration tests. Despite the guilt I still use the junit framework for these integration tests&#8230; I know the unit testing purists will be rolling their eyes at this point, but this is something [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a fairly recent convert to unit testing, but still when I&#8217;m under pressure to get something finished, I skip right to integration tests. Despite the guilt I still use the <a href="http://www.junit.org/index.htm">junit</a> framework for these integration tests&#8230; I know the unit testing purists will be rolling their eyes at this point, but this is something I&#8217;ve found useful to do.</p>
<p>A while back <a href="http://reverttoconsole.com/archives/90#more-90">I wrote about using hsqldb</a> in unit/integration tests running in memory. The process works well because you can effectively avoid having to clean up the database before running a test, and you don&#8217;t have to worry about closing the connections in the tearDown() method either. Just kill the process and the database goes away. Provide a setup sql script and load that before running your test, and you&#8217;re golden.</p>
<p>But for more involved integration tests, it&#8217;s nice to be able to see the data that&#8217;s been modified. Or write a test that performs a first phase of data manipulation, followed by a second test that relies on the output of the first. This can be accomplished by an in-memory database, but it&#8217;s more satisfying for me to actually see what occurred after the test has run. I&#8217;m probably just not detail-oriented enough to set up a test thoroughly while I&#8217;m doing development, and I like being able to exam the database results visually for that added level of comfort.</p>
<p>Hsqldb provides some really nice configuration options for running the database as a server:</p>
<li>
<ul>There&#8217;s Standard Standalone server.</ul>
<ul>You can also run the standalone server as an in-memory server</ul>
<ul>There&#8217;s also a webserver server mode that makes use of http</ul>
<ul>As of 1.7.2 hsqldb provides the ability to run more than one database in multiple server modes. (ie simultaneous access to multiple in-process and memory-only databases)</ul>
</li>
<p>Read on to see how I set this up for my tests:</p>
<p><span id="more-96"></span></p>
<p>In my tests, I need to read from one db and load into another. The problem is that the database server I could use is in a remote office over a crappy network. So there&#8217;s a strong motivation for me to run my tests locally. I needed to set up two databases, and as I said earlier, I want to be able to see the results of my work. So (this is the part where the ruby on rails people laugh):</p>
<ol>
<li>Configure two databases for my server</li>
<li>Configure my database explorer client</li>
<li>Configure my app to work with hsqldb</li>
</ol>
<h3>Configure two databases for my server</h3>
<p>To configure this in Eclipse, you&#8217;ll need to create a Java App to run. <code>Run->Run, Java Application, right-click New</code>. Main Class will be <strong>org.hsqldb.Server</strong>. I use a separate eclipse project to store my jars, so I selected that as my project. Next add the hsqldb.jar to the classpath. You can also specify arguments, but I opted to use a server.properties file instead. You can apply or save the app now.</p>
<p>Since I specified my jar project for the app I created, I created a file, server.properties in the base directory of that project. The file looks like this:</p>
<pre class="brush: java">
#this is the default so I may not actually need this
server.port=9001

#mounts a file-based (persistent) database with alias &#039;clinicalconcept&#039;
#database connection url would be &#039;jdbc:hsqldb:hsql://localhost/clinicalconcept&#039;
#but &#039;/clinicalconcept&#039; can be omitted because database.0 is the default
server.database.0=file:C:/eceppda/work/schemadb
server.dbname.0=clinicalconcept

#mounts a file-based (persistent) database with the alias &#039;staged&#039;
#datbase connection url would be &#039;jdbc:hsqldb:hsql://localhost/staged&#039;
server.database.1=file:C:/eceppda/work/stagedb
server.dbname.1=staged

#Display messages in console
server.silent=false
</pre>
<p>There are of course more options you can <a href="http://hsqldb.org/doc/guide/ch04.html#N10AA9">view them here</a>.</p>
<h3>Configure my database explorer client</h3>
<p>I use <a href="http://www.myeclipseide.com/">MyEclipse</a>. I like it a lot. It has a <a href="http://www.myeclipseide.com/htmlpages-func-display-pid-16.html">database explorer plugin</a>, <a href="http://www.myeclipseide.com/module-htmlpages-display-pid-1.html">among other things</a>. So I use this database explorer. I had to specify that I was connecting to a hsqldb, and provide it with the hsqldb jar. For connection strings, I used these (one for each database):</p>
<ul>
<li><strong>jdbc:hsqldb:hsql://localhost/clinicalconcept</strong></li>
<li><strong>jdbc:hsqldb:hsql://localhost/staged</strong></li>
</ul>
<h3>Configure my app to work with hsqldb</h3>
<p>Lastly, in the app I&#8217;m writing I created a properties file to hold the database information:</p>
<pre class="brush: java">
# staging database
db.driver=org.hsqldb.jdbcDriver
db.url=jdbc:hsqldb:hsql://localhost/clinicalconcept
db.username=sa
db.password=

stage.driver=org.hsqldb.jdbcDriver
stage.url=jdbc:hsqldb:file:C:/eceppda/stagedb/stagedb
stage.username=sa
stage.password=
</pre>
<p>Then in my spring config I specify the propertyconfigurator and my datasources:</p>
<pre class="brush: xml">

&lt;bean id=&quot;propertyConfigurer&quot; class=&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;&gt;
		&lt;property name=&quot;locations&quot;&gt;
			&lt;list&gt;
				&lt;value&gt;com/my/shite/resource/updater.properties&lt;/value&gt;
			&lt;/list&gt;
		&lt;/property&gt;
	&lt;/bean&gt;

	&lt;bean id=&quot;dbSourceDS&quot; class=&quot;org.apache.commons.dbcp.BasicDataSource&quot;&gt;
		&lt;property name=&quot;driverClassName&quot;&gt;
			&lt;value&gt;${db.driver}&lt;/value&gt;
		&lt;/property&gt;
		&lt;property name=&quot;url&quot;&gt;
			&lt;value&gt;${db.url}&lt;/value&gt;
		&lt;/property&gt;
		&lt;property name=&quot;username&quot;&gt;
			&lt;value&gt;${db.username}&lt;/value&gt;
		&lt;/property&gt;
		&lt;property name=&quot;password&quot;&gt;
			&lt;value&gt;${db.password}&lt;/value&gt;
		&lt;/property&gt;
	&lt;/bean&gt;

	&lt;bean id=&quot;stageSource&quot; class=&quot;org.apache.commons.dbcp.BasicDataSource&quot;&gt;
		&lt;property name=&quot;driverClassName&quot;&gt;
			&lt;value&gt;${stage.driver}&lt;/value&gt;
		&lt;/property&gt;
		&lt;property name=&quot;url&quot;&gt;
			&lt;value&gt;${stage.url}&lt;/value&gt;
		&lt;/property&gt;
		&lt;property name=&quot;username&quot;&gt;
			&lt;value&gt;${stage.username}&lt;/value&gt;
		&lt;/property&gt;
		&lt;property name=&quot;password&quot;&gt;
			&lt;value&gt;${stage.password}&lt;/value&gt;
		&lt;/property&gt;
	&lt;/bean&gt;
</pre>
<p>And that should do it. Run the app you configured in spring, you should see more verbose output that will say that it&#8217;s read the server.properties file. Refer to <a href="http://reverttoconsole.com/archives/90#more-90">my previous post</a> on loading data into the db&#8217;s for your tests&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reverttoconsole.com/blog/hsqldb/java-integration-testing-with-hsqldb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Here&#8217;s an Idea: Beanshell + EasyMock + Junit in Eclipse</title>
		<link>http://www.reverttoconsole.com/blog/java/heres-an-idea-beanshell-easymock-junit-in-eclipse/</link>
		<comments>http://www.reverttoconsole.com/blog/java/heres-an-idea-beanshell-easymock-junit-in-eclipse/#comments</comments>
		<pubDate>Sun, 17 Dec 2006 06:42:49 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[UnitTest]]></category>

		<guid isPermaLink="false">http://reverttoconsole.com/archives/58</guid>
		<description><![CDATA[This is postmodern computing at it&#8217;s finest&#8230; I&#8217;m a fairly recent convert to test driven development. I&#8217;ve always thought it seemed like the right thing to do, but have only recently bought into it whole-heartedly when I took over someone else&#8217;s code base and was told the requirement&#8217;s have changed and I needed to modify [...]]]></description>
			<content:encoded><![CDATA[<p>This is <a href="http://en.wikipedia.org/wiki/Postmodern">postmodern </a> computing at it&#8217;s finest&#8230;</p>
<p>I&#8217;m  a fairly recent convert to test driven development. I&#8217;ve always thought it <em>seemed like the right thing to do</em>, but have only recently bought into it whole-heartedly when I took over someone else&#8217;s code base and was told the requirement&#8217;s have changed and I needed to modify it without knowing what has changed.</p>
<p>After writing 50+ new unit tests, I started looking for a way to streamline the process, in the form of one of the multitudes of dynamic languages now available to java. JRuby, jython, and <a href="http://en.wikipedia.org/wiki/Beanshell">beanshell </a> were the favored options, as I see it (groovy looks like a promising option too, but it seems like they can&#8217;t get a 1.0 release out). It&#8217;s my understanding <a href="http://www.beanshell.org/">that beanshell has passed the latest round of jsr</a> votes and will be a part of the jdk in the future&#8230;</p>
<p>Anyway I woke up in bed tonight thinking there ought to be some way to really streamline this process in Eclipse&#8230;</p>
<p>So imagine this:</p>
<p>You&#8217;re adding a new method to an existing project. Eclipse is managing the set of project dependencies, and will quickly tell you what you&#8217;ve broken after you&#8217;ve written it&#8230; and that&#8217;s great; you&#8217;ve got this wealth of information at your disposal&#8230; but doesn&#8217;t that just beg for a new plugin???</p>
<p>Ok, imagine, you&#8217;re writing this new method&#8230; and you can instantly move into a beanshell with an eclipse context&#8230; so you can dynamically interpret the results of your method&#8230; which is fine for simple methods with primitive signatures, but more than likely you&#8217;ll have complex objects to deal with&#8230; so add something that will generate an <a href="http://www.easymock.org/">Easymock</a> object for you and pass it into your beanshell and dynamically modify the context.. to help you flesh out your method with the benefit of knowing how the method will behave without the test/write/compile step. And once you&#8217;ve done that and are happy with the results, why not generate a Junit test class as a result???</p>
<p>So the flow would be like this; you write your method signature, create your EasyMock object(s), flesh out your method in a beanshell so you know it works correctly, and generate a Junit class with the final product.</p>
<p>The more I think about this, the more beneficial it seems&#8230;</p>

<span class="slashdigglicious">
<a href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fheres-an-idea-beanshell-easymock-junit-in-eclipse%2F&amp;title=Here%26%238217%3Bs+an+Idea%3A+Beanshell+%2B+EasyMock+%2B+Junit+in+Eclipse" title="Slashdot It!"><img src="http://slashdot.org/favicon.ico" height="16" width="16" alt="[Slashdot]" /></a>
<a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fheres-an-idea-beanshell-easymock-junit-in-eclipse%2F&amp;title=Here%26%238217%3Bs+an+Idea%3A+Beanshell+%2B+EasyMock+%2B+Junit+in+Eclipse" title="Digg This Story"><img src="http://digg.com/favicon.ico" width="16" height="16" alt="[Digg]" /></a>
<a href="http://reddit.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fheres-an-idea-beanshell-easymock-junit-in-eclipse%2F&amp;title=Here%26%238217%3Bs+an+Idea%3A+Beanshell+%2B+EasyMock+%2B+Junit+in+Eclipse" title="Reddit"><img src="http://reddit.com/favicon.ico" width="16" height="16" alt="[Reddit]" /></a>
<a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fheres-an-idea-beanshell-easymock-junit-in-eclipse%2F&amp;title=Here%26%238217%3Bs+an+Idea%3A+Beanshell+%2B+EasyMock+%2B+Junit+in+Eclipse" title="Save to del.icio.us" onclick="window.open('http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fheres-an-idea-beanshell-easymock-junit-in-eclipse%2F&amp;title=Here%26%238217%3Bs+an+Idea%3A+Beanshell+%2B+EasyMock+%2B+Junit+in+Eclipse', 'delicious', 'toolbar=no,width=700,height=400'); return false;"><img src="http://images.del.icio.us/static/img/delicious.small.gif" width="16" height="16" alt="[del.icio.us]" /></a>
<a href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fheres-an-idea-beanshell-easymock-junit-in-eclipse%2F" title="Share on Facebook"><img src="http://www.facebook.com/favicon.ico" width="16" height="16" alt="[Facebook]" /></a>
<a href="http://technorati.com/faves?add=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fheres-an-idea-beanshell-easymock-junit-in-eclipse%2F" title="Add to my Technorati Favorites"><img src="http://technorati.com/favicon.ico" width="16" height="16" alt="[Technorati]" /></a>
<a href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fheres-an-idea-beanshell-easymock-junit-in-eclipse%2F&amp;title=Here%26%238217%3Bs+an+Idea%3A+Beanshell+%2B+EasyMock+%2B+Junit+in+Eclipse" title="Save to Google Bookmarks"><img src="http://www.google.com/favicon.ico" width="16" height="16" alt="[Google]" /></a>
<a href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Fheres-an-idea-beanshell-easymock-junit-in-eclipse%2F&amp;title=Here%26%238217%3Bs+an+Idea%3A+Beanshell+%2B+EasyMock+%2B+Junit+in+Eclipse" title="Stumble it!"><img src="http://www.stumbleupon.com/favicon.ico" width="16" height="16" alt="[StumbleUpon]" /></a>
</span>]]></content:encoded>
			<wfw:commentRss>http://www.reverttoconsole.com/blog/java/heres-an-idea-beanshell-easymock-junit-in-eclipse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>EasyMock Challenge</title>
		<link>http://www.reverttoconsole.com/blog/java/easymock-challenge/</link>
		<comments>http://www.reverttoconsole.com/blog/java/easymock-challenge/#comments</comments>
		<pubDate>Thu, 26 Oct 2006 15:33:16 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[UnitTest]]></category>

		<guid isPermaLink="false">http://reverttoconsole.com/archives/37</guid>
		<description><![CDATA[I&#8217;ve been using EasyMock with my unit tests on a current project. Generally, it&#8217;s been useful; to configure my project to run in a test environment on my desktop is a really pain. By using EasyMock I can create a very controlled environment for my tests by faking just enough of it to run. The [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using <a href="http://www.easymock.org/">EasyMock</a> with my unit tests on a current project. Generally, it&#8217;s been useful; to configure my project to run in a test environment on my desktop is a really pain. By using EasyMock I can create a very controlled environment for my tests by faking just enough of it to run.<br />
The documentation is fairly small, <a href="http://www.easymock.org/EasyMock1_2_Java1_3_Documentation.html">here is a link</a>.<br />
Briefly, you create a MockControl object from the base class:</p>
<pre class="brush: java">
MockControl myPencil = MockControl.createControl(Writer.class);
</pre>
<p><strong>Caveat- The mocked class must implement an interface.</strong></p>
<p>Next, you can get a mock implementation of the interface:</p>
<pre class="brush: java">
Writer mockPencil = (Writer)myPencil.getMock();
</pre>
<p>Now, with the mock and mockcontrol in hand, you can tell it how you want it to perform.</p>
<pre class="brush: java">
mockPencil.write(&quot;Something to write&quot;);
myPencil.setReturnType(new OutputStream());
mockPencil.erase(&quot;Something to erase&quot;);
myPencil.setVoidCallable();
</pre>
<p>And when you&#8217;re done, always call:</p>
<pre class="brush: java">
myPencil.replay();
</pre>
<p>Run your test, and your mock object will take the place of the real thing.</p>
<p>A couple other notes:</p>
<ul>
<li>You can specify any return type, including throwables</li>
<li>You must specify the number of times you expect a method to be called, the mock will throw an exception if it receives something other than what you specify</li>
</ul>
<p>Finally, sometimes the exceptions can be difficult to interpret. Take this recent stacktrace I received as an example:</p>
<pre class="brush: java">
junit.framework.AssertionFailedError:
  Unexpected method call lockHistory(Context: user=IOP-10.25.2006.05.05,workflowId=null, ( id = 00000010 (00000010) )):
    lockHistory(Context: user=IOP-10.25.2006.05.05,workflowId=null, ( id = 00000010 (00000010) )): expected: 0, actual: 1
    lockHistory(Context: user=IOP-10.25.2006.05.05,workflowId=null, ( graphUuid = 00000010 (00000010) )): expected: 1, actual: 0
at org.easymock.MockControl$4.invoke(MockControl.java:148)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:44)
at $Proxy0.lockHistory(Unknown Source)
</pre>
<p>It&#8217;s telling me that it expected to receive a call to lockHistory with     user=IOP-10.25.2006.05.05, workflowId=null, ( id = 00000010 (00000010) ). But instead it received&#8230; uh&#8230; the &#8230; same &#8230; thing?</p>
<p>Normally the exception provides different values and it&#8217;s not as tricky to track down the cause of the difference. I normally debug through the code as the test runs to find and fix the problem. I&#8217;ll let you know what I find in this case.</p>

<span class="slashdigglicious">
<a href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Feasymock-challenge%2F&amp;title=EasyMock+Challenge" title="Slashdot It!"><img src="http://slashdot.org/favicon.ico" height="16" width="16" alt="[Slashdot]" /></a>
<a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Feasymock-challenge%2F&amp;title=EasyMock+Challenge" title="Digg This Story"><img src="http://digg.com/favicon.ico" width="16" height="16" alt="[Digg]" /></a>
<a href="http://reddit.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Feasymock-challenge%2F&amp;title=EasyMock+Challenge" title="Reddit"><img src="http://reddit.com/favicon.ico" width="16" height="16" alt="[Reddit]" /></a>
<a href="http://del.icio.us/post?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Feasymock-challenge%2F&amp;title=EasyMock+Challenge" title="Save to del.icio.us" onclick="window.open('http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Feasymock-challenge%2F&amp;title=EasyMock+Challenge', 'delicious', 'toolbar=no,width=700,height=400'); return false;"><img src="http://images.del.icio.us/static/img/delicious.small.gif" width="16" height="16" alt="[del.icio.us]" /></a>
<a href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Feasymock-challenge%2F" title="Share on Facebook"><img src="http://www.facebook.com/favicon.ico" width="16" height="16" alt="[Facebook]" /></a>
<a href="http://technorati.com/faves?add=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Feasymock-challenge%2F" title="Add to my Technorati Favorites"><img src="http://technorati.com/favicon.ico" width="16" height="16" alt="[Technorati]" /></a>
<a href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Feasymock-challenge%2F&amp;title=EasyMock+Challenge" title="Save to Google Bookmarks"><img src="http://www.google.com/favicon.ico" width="16" height="16" alt="[Google]" /></a>
<a href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.reverttoconsole.com%2Fblog%2Fjava%2Feasymock-challenge%2F&amp;title=EasyMock+Challenge" title="Stumble it!"><img src="http://www.stumbleupon.com/favicon.ico" width="16" height="16" alt="[StumbleUpon]" /></a>
</span>]]></content:encoded>
			<wfw:commentRss>http://www.reverttoconsole.com/blog/java/easymock-challenge/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
