<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>
                                <td>
                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">Community</a></h1>
                                                                </td>
                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
Test Driven Development (TDD) and Moving Forward with Less Technical Debt
</h3>
<span style="margin-bottom: 10px;">
modified by <a href="http://community.jboss.org/people/bfitzpat">Brian Fitzpatrick</a> in <i>JBoss Tools</i> - <a href="http://community.jboss.org/docs/DOC-15096">View the full document</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p>Note: This is a work in progress, not gospel. So know that it will most likely change over time. <span> :) </span></p><h2>Overview</h2><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Before we get into TDD and how it will eventually make our lives easier once we adjust to using it, I want to go over a bit of what we do now.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Right now we use the traditional waterfall type of pattern...</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>   Design -> Implement -> Test</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>And this works, but all too often we skip the last step because we're out of time or have to fix something someone else found. Testing shouldn't be a skippable step, but it ends up that way far too often.<br/> <br/> By pushing tests to the end of the process and then not getting them done, we end up with painful test cycles with QA and the field as we've been doing the last few releases. As a result, we end up with a pile of technical debt - bugs start to pile up as QA reports them, incompatibilities with previous releases creep in,and so on...</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><h2>Test Driven Development (TDD)</h2><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Instead, we should be using the TDD model, which changes the pattern a bit...</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>   Design -> Test -> Implement -> Test -> Implement -> and so on</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>By building the tests into the pattern earlier, we should reduce the frequency of having to go back in and fix bugs later because we're testing as we go and creating more stable code all the way along.<br/> <br clear="none"/>How this works is something like this (for new features):</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>   1) Design: Figure out what you want to do<br clear="none"/>   2) Test: Write a test to express the design<br clear="none"/>      * IT SHOULD FAIL<br clear="none"/>   3) Implement: Write the code<br clear="none"/>   4) Test again<br clear="none"/>      * IT SHOULD PASS</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>For bug fixes, it's a little different:<br/> <br/>    1) Get some sample code (that breaks)<br clear="none"/>    2) Turn it into a test<br clear="none"/>       * IT SHOULD FAIL<br clear="none"/>    3) Fix the bug<br clear="none"/>    4) Test again<br clear="none"/>       * IT SHOULD PASS<br clear="none"/> <br/> These patterns should be used for all bug fixes and new features we implement from now on. And it's not going to be easy to get used to this model for some of us, but it will get easier with time and practice. <br/> <br/> Hopefully by writing more of this type of focused unit test, we:<br/> <br/>    * Write tests once<br/>    * Keep them somewhere sensible (like a separate plug-in with the name ".test" appended to the end)<br/>    * Run frequently (one click) as a developer and run tests as part of every build<br/>    * Don't require human input (which makes tests faster)<br/>    * And we provide machine-parsable output like JUnit logs that say whether a test passed/failed</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>The benefits are many, but they boil down to two main ones:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>   * We get tests as part of the process, rather than an afterthought.<br clear="none"/>   * We get more stable code.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><h2>Moving Forward...</h2><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Now how are we going to do this in Eclipse?</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Well, we're going to use JUnit or testNG (until a decision is made, focus on JUnit) as much as possible to test the underlying code. The tests should be specific to a particular problem and run quickly. Use mock objects where possible to mock up whatever you need, reducing the runtime dependencies.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>It's been suggested that we use Mockito, JMock, or<span style="font-size: 10pt;"> testNG + PowerMock</span> to handle testing with mock objects.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Once we've tested as much as we can of the frameworks, APIs, and underlying code with JUnit, we should look at building a suite of SWTBot tests. <br/> <br/> The SWTBot tests should not only test individual bits of functionality, such as clicking this button should generate this result, but should also start focusing on specific use cases and doing more end to end testing. These two types of tests should be kept separate. <br/> <br/> So we're looking at three types of tests:<br/> <br/>    * JUnit Tests and Test Suites<br/>    * SWTBot functional tests<br/>    * SWTBot integration tests<br/> <br/> The first two types of tests should be run as a regular part of the nightly builds. And the integration tests should probably be run weekly as part of the weekly integration builds to make sure nothing was broken.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><h2>Interesting Testing Resources for Reading...</h2><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><ul><li>TestNG + Eclipse: <a class="jive-link-external-small" href="http://testng.org/eclipse.html">http://testng.org/eclipse.html</a></li><li>TestNG - Migrating from JUnit: <a class="jive-link-external-small" href="http://testng.org/migrating.html">http://testng.org/migrating.html</a></li><li>TestNG + Maven: <a class="jive-link-external-small" href="http://testng.org/maven.html">http://testng.org/maven.html</a></li><li>TestNG + PowerMock: <a class="jive-link-external-small" href="http://blog.jayway.com/2009/12/14/powermock-testng-true/">http://blog.jayway.com/2009/12/14/powermock-testng-true/</a></li><li>More TestNG Reading: <a class="jive-link-external-small" href="http://testng.org/misc.html">http://testng.org/misc.html</a></li><li>JUnit4 vs. TestNG comparison: <a class="jive-link-external-small" href="http://www.mkyong.com/unittest/junit-4-vs-testng-comparison/">http://www.mkyong.com/unittest/junit-4-vs-testng-comparison/</a></li></ul><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><h3>Additional Resources</h3><ul><li>Making your JUnit Tests run faster in Eclipse: <a class="" href="http://community.jboss.org/wiki/HowtoMakeYourJUnitTestsRunFasterinEclipse">http://community.jboss.org/wiki/HowtoMakeYourJUnitTestsRunFasterinEclipse</a></li></ul></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Comment by <a href="http://community.jboss.org/docs/DOC-15096">going to Community</a></p>
        <p style="margin: 0;">Create a new document in JBoss Tools at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2128">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>