<?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>Bob McCune &#187; Java</title>
	<atom:link href="http://www.bobmccune.com/category/technology/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bobmccune.com</link>
	<description>iOS and Java Design, Development, Training, and Mentoring</description>
	<lastBuildDate>Thu, 15 Sep 2011 06:55:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>TCJUG &#8211; What&#039;s New in Spring 2.5?</title>
		<link>http://www.bobmccune.com/2008/01/21/tcjug-whats-new-in-spring-25/</link>
		<comments>http://www.bobmccune.com/2008/01/21/tcjug-whats-new-in-spring-25/#comments</comments>
		<pubDate>Mon, 21 Jan 2008 18:23:42 +0000</pubDate>
		<dc:creator>Bob McCune</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Presentation]]></category>

		<guid isPermaLink="false">http://www.bobmccune.com/2008/01/21/tcjug-whats-new-in-spring-25/</guid>
		<description><![CDATA[I&#8217;d like to say thanks to everyone who attended my Spring 2.5 presentation at last week&#8217;s TCJUG meeting. We had another good turnout, which is always nice to see. Jeff Jensen at Intertech is always a great host and I had a fun time presenting. I hope the info was useful to you. I&#8217;ve attached [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>I&#8217;d like to say thanks to everyone who attended my <a href="http://www.springframework.org/node/561">Spring 2.5</a> presentation at last week&#8217;s <a href="http://www.intertech-inc.com/Resource/JavaUserGroup.aspx">TCJUG</a> meeting.  We had another good turnout, which is always nice to see.  Jeff Jensen at <a href="http://www.intertech-inc.com/">Intertech</a> is always a great host and I had a fun time presenting.  I hope the info was useful to you.  I&#8217;ve attached copies of the presentation and sample code for your reference.</p>
<div>
<a href='http://www.bobmccune.com/wp-content/uploads/2008/01/whats-new-in-spring-25.pdf' title='whats-new-in-spring-25.pdf'>What&#8217;s New in Spring 2.5?</a>
</div>
<div>
<a href='http://www.bobmccune.com/wp-content/uploads/2008/01/spring-web.zip' title='spring-web.zip'>Spring 2.5 Contacts Demo Application</a>
</div>
<p>
<br/><br />
Thanks again!</p>
<p>-Bob</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bobmccune.com/2008/01/21/tcjug-whats-new-in-spring-25/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TCJUG &#8211; JPA Presentation</title>
		<link>http://www.bobmccune.com/2007/03/23/tcjug-jpa-presentation/</link>
		<comments>http://www.bobmccune.com/2007/03/23/tcjug-jpa-presentation/#comments</comments>
		<pubDate>Fri, 23 Mar 2007 03:16:07 +0000</pubDate>
		<dc:creator>Bob McCune</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Presentation]]></category>

		<guid isPermaLink="false">http://www.bobmccune.com/2007/03/23/tcjug-jpa-presentation/</guid>
		<description><![CDATA[I&#8217;d like to say thanks to everyone who attended my JPA presentation at last night&#8217;s TCJUG meeting. It was a full house and there was a lot of good conversation during and after the presentation. As always, Jeff Jensen at Intertech is a great host and I had a fun time. I hope the info [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>I&#8217;d like to say thanks to everyone who attended my <a href="http://java.sun.com/javaee/technologies/persistence.jsp">JPA</a> presentation at last night&#8217;s <a href="http://www.intertech-inc.com/Resource/JavaUserGroup.aspx">TCJUG</a> meeting.  It was a full house and there was a lot of good conversation during and after the presentation.  As always, Jeff Jensen at <a href="http://www.intertech-inc.com/">Intertech</a> is a great host and I had a fun time.  I hope the info was useful to you.  I&#8217;ll post the slides and demo application as soon as I finish making some Maven tweaks.  Look for it early next week.</p>
<p>Thanks again!</p>
<p>-Bob</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bobmccune.com/2007/03/23/tcjug-jpa-presentation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit Testing Best Practices</title>
		<link>http://www.bobmccune.com/2006/12/09/unit-testing-best-practices/</link>
		<comments>http://www.bobmccune.com/2006/12/09/unit-testing-best-practices/#comments</comments>
		<pubDate>Sun, 10 Dec 2006 00:49:46 +0000</pubDate>
		<dc:creator>Bob McCune</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://bobmccune.com/2006/12/09/unit-testing-best-practices/</guid>
		<description><![CDATA[I was recently compiling a list of Test-driven Development (TDD) &#8220;Best Practices&#8221; for my client project&#8217;s wiki site and thought this would probably be useful to the community at large. I&#8217;ve been an avid practitioner and proponent of test-driven development for a few years now and have found it to be one of the most [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>I was recently compiling a list of Test-driven Development (TDD) &#8220;Best Practices&#8221; for my client project&#8217;s wiki site and thought this would probably be useful to the community at large.  I&#8217;ve been an avid practitioner and proponent of test-driven development for a few years now and have found it to be one of the most important practices I&#8217;ve adopted.  If practiced correctly, it will have a significantly positive impact on the design and quality of your code.</p>
<p><span id="more-32"></span></p>
<p>Without further ado, here is my list of key concerns to keep in mind while practicing TDD&#8230;</p>
<h4>Test Behavior, Not Methods</h4>
<p>It is important to write test cases that test how a client object would interact with the class under test.  A common question asked is &#8220;should I write unit tests for private or package-private methods?&#8221;, and my general response is, no.  Private methods should not be directly tested as they do not comprise an object&#8217;s publicly exported API and instead only exist to support its publicly observable behavior.  This certainly isn&#8217;t a hard and fast rule.  If a private method is sufficiently complex or would be difficult to fully test via its public methods it can make sense to test these directly.  If you choose to do so you&#8217;ll need to write some reflection code to invoke these methods or use something like the utilities provided by the <a href="http://junit-addons.sourceforge.net/">JUnit Add-ons</a> project.</p>
<h4>Apply the &#8220;Too Simple to Break&#8221; Rule</h4>
<p>Although maintaining a high degree of code coverage across the test suite is important, striving for 100% coverage is not advisable.  There is a point of diminishing returns that needs to be considered.  A good rule of thumb is if a method is too simple to break, don&#8217;t write a unit test for it.  The question of what constitutes &#8220;to simple to break&#8221; is certainly up to interpretation, however, it&#8217;s rarely useful to write a unit test for a method like the following:</p>
<pre>
public String getName() {
    return name;
}
</pre>
<p>Testing simple getter and setter methods is generally not advisable.</p>
<h4>Use Object-Oriented Best Practices In Your Test Cases</h4>
<p>It is important to view your test cases as first class code assets.  The same design techniques you would apply when writing your production code should be applied to test cases as well.  This means you should look for opportunities to refactor your test cases to pull up common functionality into a base class, remove duplicate code, decompose long methods, etc.  Failing to do so will drive up the cost of change and will contribute to the test suite losing value over time.</p>
<h4>Use Appropriate Method Names</h4>
<p>This may seem obvious, but it is all too common to see test methods with the following signatures:</p>
<pre>
public void testGetUser1() {
}

public void testGetUser2() {
}
</pre>
<p>Instead, use method names that clearly describe the purpose of the test.  Doing so makes your test cases more understandable and easier for the team to maintain.  For instance:</p>
<pre>
public void testGetUserWithInvalidUsername() {
}

public void testGetUserThrowsExpiredCredentialsException() {
}
</pre>
<h4>Don&#8217;t Use Try/Catch Blocks to Catch Unexpected Exceptions</h4>
<p>The keyword with this practice is <i>unexpected</i>.  The JUnit framework automatically handles any exceptions that bubble up the call stack.  It is important that you don&#8217;t circumvent this behavior by using a try/catch block around a test that <u>should not</u> throw an exception.  Doing so can mask bugs that won&#8217;t be caught until the code is in production.  For instance, consider the following implementation of the <code>UserService</code> interface:</p>
<pre>
public class UserServiceImpl implements UserService {

  private UserDao dao;

  public User getUser(String username)
                          throws UserNotFoundException {
    User user = dao.getUser(username);
    if (user == null) {
      String msg = "No User found for: '" + username + "'";
      throw new UserNotFoundException(msg);
    }
    return user;
  }

  public void setUserDao(UserDao dao) {
    this.dao = dao;
  }

}
</pre>
<p>There are two primary scenarios that need to be tested:</p>
<ol>
<li>A valid <code>User</code> instance is returned for the specified username.</li>
<li>A <code>UserNotFoundException</code> is thrown for the specified username.</li>
</ol>
<p>In the first scenario, no exception should be thrown so we should not attempt to catch it.  Instead, we should declare, in the test method&#8217;s signature, that an exception is thrown.  This will allow any exceptions to bubble up to the JUnit framework in the event that our setup code was incorrect and inadvertently caused this exception.</p>
<p>In the second scenario, we are explicitly checking for the case where an exception <u>is</u> thrown.  This means we need to wrap this in a try/catch block and fail the test if the exception <u>is not</u> thrown.</p>
<p>The following test case implementation shows how to appropriately test these two scenarios.  I&#8217;m using <a href="http://www.easymock.org">EasyMock</a> to mock the interaction with my DAO.</p>
<pre>
public class UserServiceImplTest extends TestCase {

  private UserDao dao;
  private MockControl ctrl;

  private UserServiceImpl service;

  protected void setUp() throws Exception {
    ctrl = MockControl.createControl(UserDao.class);
    dao = (UserDao) ctrl.getMock();
    service = new UserServiceImpl();
    service.setUserDao(dao);
  }

  public void testGetUserWithValidUserName()
                                  throws Exception {

    User expected = new User();
    String validUser = "validUser";
    ctrl.expectAndReturn(dao.getUser(validUser), expected);
    ctrl.replay();

    User result = service.getUser(validUser);
    assertSame(expected, result);

    ctrl.verify();
  }

  public void testGetUserThrowsUserNotFoundException() {

    String bogusUser = "bogusUser";
    ctrl.expectAndReturn(dao.getUser(bogusUser), null);
    ctrl.replay();

    try {
        service.getUser(bogusUser);
        fail("Should have thrown a UserNotFoundException");
    } catch (UserNotFoundException e) {
        // should be here
    }

    ctrl.verify();
  }

}
</pre>
<h4>Don&#8217;t Rely on Visual Inspection</h4>
<p>The success or failure of a test should not be predicated on a developer &#8220;eyeballing&#8221; the console to determine the result.  This means you shouldn&#8217;t use <code>System.out.println()</code> or <code>Logger.debug()</code> statements to determine your test&#8217;s success state.  Instead, you should exclusively rely on JUnit&#8217;s various <code>assertXXX()</code> methods so the test can be run in an unattended, automated manner.</p>
<h4>Use A Code Coverage Tool</h4>
<p>It is often difficult to gauge how completely your test cases are exercising a piece of code.  Failing to fully test all execution paths could result in you missing a critical application bug that causes a system failure.  Coverage utilities can easily be incorporated into your Ant or Maven build processes which will generate a visual coverage report showing you precisely which lines of code were tested.</p>
<p>Here are a few open source tools I use for this purpose:<br />
<a href="http://cobertura.sourceforge.net/">Cobertura</a><br />
<a href="http://emma.sourceforge.net/">Emma</a><br />
<a href="http://www.eclemma.org/">EclEmma</a></p>
<h4>Use The Appropriate Tool for the Testing Scenario</h4>
<p>JUnit provides a solid framework for unit testing in a Java environment, however, JUnit alone does not provide you all the capabilities you need to effectively test your code.  It is important to find the right tool for the job.  Luckily, the community has developed a wide variety of extension to JUnit that will help you test virtually any scenario you might come across.</p>
<p>Here are a few unit and integration testing tools I commonly use:<br />
<a href="http://www.dbunit.org/">DBUnit</a><br />
<a href="http://mockrunner.sourceforge.net/">MockRunner</a><br />
<a href="http://www.easymock.org/">EasyMock</a><br />
<a href="http://www.jmock.org/">JMock</a><br />
<a href="http://www.openqa.org/selenium/">Selenium</a></p>
<p>That&#8217;s it for now.  Hopefully this will provide you with some useful things to keep in mind while writing your test cases.  If you have any additions to this list, please comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bobmccune.com/2006/12/09/unit-testing-best-practices/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>EclEmma</title>
		<link>http://www.bobmccune.com/2006/11/22/eclemma/</link>
		<comments>http://www.bobmccune.com/2006/11/22/eclemma/#comments</comments>
		<pubDate>Wed, 22 Nov 2006 01:59:41 +0000</pubDate>
		<dc:creator>Bob McCune</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://bobmccune.com/?p=20</guid>
		<description><![CDATA[I recently came across a new Eclipse plugin you should definitely check out if you&#8217;re doing test-driven development (TDD). It&#8217;s called EclEmma and is based on Emma, the open source code coverage tool. Code coverage reporting tools like Emma or Cobertura are a must if you are doing TDD, but the one downside these tools [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>I recently came across a new Eclipse plugin you should definitely check out if you&#8217;re doing test-driven development (TDD). It&#8217;s called <a href="http://www.eclemma.org/">EclEmma</a> and is based on Emma, the open source code coverage tool. Code coverage reporting tools like <a href="http://emma.sourceforge.net/">Emma</a> or <a href="http://cobertura.sourceforge.net/">Cobertura</a> are a must if you are doing TDD, but the one downside these tools have is it&#8217;s not easy to run them against an individual class while you&#8217;re writing your test. EclEmma solves this problem by nicely integrating with Eclipse which makes getting coverage feedback as easy as firing up the IDE&#8217;s JUnit test runner.  It&#8217;s still an early release, so you may find some rough edges here and there, however, I&#8217;ve been quite impressed with what I&#8217;ve seen so far.  It definitely looks promising.</p>
<p><a href="http://www.eclemma.org/">EclEmma Homepage</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bobmccune.com/2006/11/22/eclemma/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Twin Cities Code Camp – Fall 2006</title>
		<link>http://www.bobmccune.com/2006/11/11/twin-cities-code-camp-fall-2006/</link>
		<comments>http://www.bobmccune.com/2006/11/11/twin-cities-code-camp-fall-2006/#comments</comments>
		<pubDate>Sun, 12 Nov 2006 00:10:20 +0000</pubDate>
		<dc:creator>Bob McCune</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Presentation]]></category>

		<guid isPermaLink="false">http://bobmccune.com/?p=10</guid>
		<description><![CDATA[I just finished giving a presentation on Hibernate 3.2 at the first Twin Cities Code Camp event in Edina, MN. You can grab a copy of the presentation and source code examples below. As I was attempting to get set up I realized I had forgotten to bring my DVI to VGA adapter for my [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>I just finished giving a presentation on Hibernate 3.2 at the first <a href="http://www.twincitiescodecamp.com">Twin Cities Code Camp</a> event in Edina, MN.  You can grab a copy of the presentation and source code examples below.</p>
<p>As I was attempting to get set up I realized I had forgotten to bring my DVI to VGA adapter for my MacBook Pro.  Once I recovered from the mini stroke I was having, I asked one of the organizers if they could help me track down someone with a Mac laptop.  Thankfully they did and things were able to move forward.  I don&#8217;t know the anonymous Mac user whose adapter I borrowed, but thank you very much!!</p>
<p>I&#8217;d like to say thanks to everyone who attended.    I hope you found the presentation useful.</p>
<p><a id="p15" href="http://bobmccune.com/wp-content/uploads/2006/11/tccc-2006-hibernate-presentation.pdf">Hibernate Presentation</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bobmccune.com/2006/11/11/twin-cities-code-camp-fall-2006/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Java Persistence with Hibernate</title>
		<link>http://www.bobmccune.com/2006/11/11/java-persistence-with-hibernate/</link>
		<comments>http://www.bobmccune.com/2006/11/11/java-persistence-with-hibernate/#comments</comments>
		<pubDate>Sat, 11 Nov 2006 18:09:38 +0000</pubDate>
		<dc:creator>Bob McCune</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://bobmccune.com/?p=11</guid>
		<description><![CDATA[The long awaited update to Hibernate in Action has been released! HiA was the definitive guide on all things Hibernate, but unfortunately was written for version 2.x. The new version has been updated for version 3.x and additionally covers using Hibernate as the persistence provider for JPA and EJB3. I&#8217;ve been reading the preview release [...]]]></description>
			<content:encoded><![CDATA[<p></p><p><a href="http://www.bobmccune.com/wp-content/uploads/2010/06/bauer2_cover150.jpg"><img src="http://www.bobmccune.com/wp-content/uploads/2010/06/bauer2_cover150.jpg" alt="" title="Java Persistence with Hibernate" width="150" height="187" class="alignleft size-full wp-image-184" /></a>The long awaited update to Hibernate in Action has been released!  HiA was the definitive guide on all things Hibernate, but unfortunately was written for version 2.x.  The new version has been updated for version 3.x and additionally covers using Hibernate as the persistence provider for JPA and EJB3.  I&#8217;ve been reading the preview release of this book for the past few weeks and am enjoying the updated and new content.  It&#8217;s a must have book if you&#8217;re working with Hibernate.</p>
<p>The eBook is available now and the print version will be available in a couple of weeks.  You can get a copy from <a href="http://www.manning.com/bauer2/">Manning&#8217;s website</a>.  You can also pre-order the book from <a href="http://www.amazon.com/Java-Persistence-Hibernate-Christian-Bauer/dp/1932394885/sr=8-1/qid=1163287224/ref=pd_bbs_sr_1/102-3411463-2635349?ie=UTF8&#038;s=books">Amazon</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bobmccune.com/2006/11/11/java-persistence-with-hibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

