<feed xmlns="http://www.w3.org/2005/Atom">
  <div class="info" xmlns="http://www.w3.org/1999/xhtml">
         This is an Atom formatted XML site feed.
         It is intended to be viewed in a Newsreader or syndicated to another site.
         Please visit <a href="http://www.atomenabled.org/">atomenabled.org</a> for more info.
  </div>
  <title>Blogging Pubbitch</title>
  <link href="http://www.pubbitch.org/blog/index.atom" rel="self"/>
  <link href="http://www.pubbitch.org/blog/index.html" rel="alternate" type="text/html"/>
  <id>http://www.pubbitch.org/blog/tech/java.atom</id>
  <updated>2008-10-12T09:09:22+00:00</updated>
  <author>
    <name>Simon Stewart</name>
  </author>
  <entry>
    <id>tag:pubbitch.org,2008:entry,121611553503022</id>
    <title>Building TestNG</title>
    <link href="http://www.pubbitch.org/blog/2008/07/15/building_testng" rel="alternate"/>
    <updated>2008-07-15T09:52:15Z</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
<p>For whatever reason, I'm having a hunt through the <a href="http://testng.org/">TestNG</a> source code. It's got &quot;.classpath&quot; and &quot;.project&quot; files, which is generally an indicator that the project can be imported into <a href="http://www.eclipse.org/">eclipse</a>. Sure enough, it can, but there's a missing variable that needs to be set up: <b>TOOLS15_LIB</b> should point to your JDK's &quot;tools.jar&quot; file.
</p><p>
I wonder how that works on OS X, where certainly the older JDK installations didn't have a &quot;tools.jar&quot; to point at.</p>      </div>
    </content>
  </entry>
  <entry>
    <id>tag:pubbitch.org,2007:entry,11858716390</id>
    <title>Snapshots of Failing Builds</title>
    <link href="http://www.pubbitch.org/blog/2007/07/31/snapshots_of_failing_builds" rel="alternate"/>
    <updated>2007-07-31T08:49:19Z</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
<p>The problem with acceptance and regression testing is that the errors coming back from an automated build don't really help identify the problem. A stacktrace letting you know that some element wasn't present on the page doesn't necessarily tell you why that is. However, the stacktrace dumped out to the screen by the app server (or the wedged UI of your desktop app, or... you get the idea) tells you an awful lot. 
</p><p>
Wouldn't it be nice to capture a screen-shot when a test fails? And did you know that it's ridiculously simple to do this using bog-standard Java? The code snippet (without error checking) you need goes something like:
</p><p>
<pre>
public void grabScreenShot(String testName) {
    File dir = System.getProperty("place.to.dump.images", "/tmp/build");
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    BufferedImage image = new Robot().createScreenCapture(new Rectangle(screenSize));
    ImageIO.write(image, "jpeg", new File(dir + "/" + testName + ".jpeg"));
}
</pre>
</p><p>
If you're using JUnit 3, just set up a new TestListener that does this when a test fails or has an error. If you're using some other test framework then you'll need to figure out what to do. How hard can it be?</p>      </div>
    </content>
  </entry>
  <entry>
    <id>tag:pubbitch.org,2007:entry,11856479060</id>
    <title>I'm Liking This Java 5 Stuff</title>
    <link href="http://www.pubbitch.org/blog/2007/07/28/im_liking_this_java_5_stuff" rel="alternate"/>
    <updated>2007-07-28T18:39:49Z</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
<p>Now, I know that I'm a little late to the party, but I'm on my first Java 5 project and I'm liking it. Given the shiny goodness in it, from Doug Lea's <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/package-summary.html">concurrency library</a> through <a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html">generics</a> and covariant return types to the extra insight that can be gained through using <a href="http://java.sun.com/j2se/1.5.0/docs/guide/management/agent.html">JMX</a> on the virtual machine, there's not much reason not to use it. Even <a href="http://publib.boulder.ibm.com/infocenter/wasinfo/v6r1/index.jsp?topic=/com.ibm.websphere.base.doc/info/aes/ae/welc_newinrelease.html">WebSphere</a>, notoriously slow as it is to pick up on new-fangled goodness in Java, has finally caught up.
</p><p>
Now, if only I had to chance to explore those <a href="http://java.sun.com/j2se/1.5.0/docs/guide/concurrency/index.html">further reaches</a> of the API....</p>      </div>
    </content>
  </entry>
  <entry>
    <id>tag:pubbitch.org,2006:entry,11501978030</id>
    <title>Scary Diagrams</title>
    <link href="http://www.pubbitch.org/blog/2006/06/13/scary_diagrams" rel="alternate"/>
    <updated>2006-06-13T11:24:44Z</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
<p>One of the great things about programming in Java is the number of abstractions available to you. These let you think about a problem in a very specific way depending on the layer of the application you happen to be sitting in. Hibernate, for example, let's you think in terms of Objects rather than database tables (though, obviously, as you progress you need to be aware that it is just an ORM tool and you can't hide from the "R" forever) Spring's Inversion of Control container allows you focus exclusively on the relationships between things, and then ignore it for the rest of your coding. And so on.
</p><p>
Of course, there is a down-side, as <a href="http://ptrthomas.wordpress.com/2006/06/06/java-call-stack-from-http-upto-jdbc-as-a-picture/">the diagram</a> that Tim Bray linked to demonstrates. That's an awfully huge stack trace....</p>      </div>
    </content>
  </entry>
  <entry>
    <id>tag:pubbitch.org,2006:entry,11492889800</id>
    <title>Points of Intervention</title>
    <link href="http://www.pubbitch.org/blog/2006/06/02/points_of_intervention" rel="alternate"/>
    <updated>2006-06-03T10:59:24Z</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
<p>We've got a problem. The problem is that there's this library that we have to use which needs to be configured using something other than code. Through an XML file, perhaps, or an LDAP schema. This is fine if the configuration is set in stone, but it's not. For the sake of efficient integration testing you want to be able to replace chunks of the system's configuration between tests, or perhaps you want to only set up a subset of the configuration. What can you do? 
</p><p>
Let's take a step back and think about the various points at which we can intervene in this system:
</p><p>
<ul>
<li>By using the normal configuration mechanisms, making the library re-read these with every test</li>
<li>By somehow subverting the configuration mechanism, perhaps by noticing that certain system properties are used for default values</li>
<li>By plugging in a new configuration mechanism which can be manipulated in code</li>
<li>By inheriting from key classes in the library and configuring it to use those instead</li>
<li>By inheriting from key classes in the library and forcefully injecting them using reflection</li>
<li>By re-writing part of the library and submitting those changes back to the main development trunk</li>
<li>By taking advantage of class-loader ordering, and placing overridden but binary compatible version of key library classes in an earlier class-laoder (that is, the class has the same public interface, name and package as one in the library)</li>
<li>By the "nuclear option" of using AOP and wrapping any offending pain points</li>
</ul>
</p><p>
I'm sure that this list isn't complete (doubtless I'll wake up at 4 in the morning with another bright idea) but it's enough to get us going. 
</p><p>
The interesting thing about this list is that the amount of political resistance tends to increase as you head further down it --- it tends to be easier to get acceptance of the use of "official" configuration mechanisms or extension points than it is to get buy-in for minor tweaks to the library. This in turn is easier than trying to persuade people that the use of AOP is a sane course of action. The use of AOP can be a lot like trying to trim your nails with a chainsaw: you can do it, but it's more than likely going to end up in a bloody mess unless you're very careful.
</p><p>
So, we have our list of intervention points. How to use them?
</p><p>
My preference would tend to be something that's obvious and which fits into the overall philosophy of the library. As such, providing a new configuration mechanism through an existing extension point or contributing a change to the library back would be the avenues that I'd want to explore first. Constantly spooling files to disk seems a little distasteful to me, and sometimes libraries are written in such a way as to prevent reloading once they're up and running. Additionally, the reading of config files can take some time, as anyone who's tried it with Hibernate can probably tell you, so it's not an ideal solution.
</p><p>
Once those possibilities have been exhausted, my Perlish tendencies tend to show and I start to reach for reflection. If the library is lousy with final classes, then AOP or class-loader abuse starts to look like a decent option. I'd want to step outside and get some air before pressing on with that plan....
</p><p>
Where am I going with this? Good question. I'm not sure, but I've been thinking about the various points at which a developer can intervene in a system and thought it might be nice to share. What do you like to do? How do you deal with these issues?</p>      </div>
    </content>
  </entry>
  <entry>
    <id>tag:pubbitch.org,2006:entry,113724081835687</id>
    <title>WebWork 2.2 Is Out</title>
    <link href="http://www.pubbitch.org/blog/2006/01/14/webwork_22_is_out" rel="alternate"/>
    <updated>2006-01-14T12:19:30Z</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
<p>Ths is good news! <a href="http://blogs.opensymphony.com/webwork/2006/01/webwork_22_released_and_ready.html">WebWork 2.2</a> is out. The big thing that I like about it is that there's now native support for Spring rolled into the core distribution. I've been using the Spring/WebWork integration since it first appeared and it's pretty slick --- try it, you might like it. Obviously, there's a bunch of other Good Stuff in there too.
</p><p>
This is also the last release of WebWork under the aegis of the <a href="http://www.opensymphony.com/">OpenSymphony</a> group. After this, we'll see a migration to the <a href="http://struts.apache.org/struts-action/index.html">Struts Action Framework</a>. As a long time WebWork user, I find this new direction interesting. On theone hand, it makes it far easier to introduce a kitten-friendly webapp framework into clients who insist that Struts is the only answer (for whatever reason) On the other hand, WebWork will now acquire an inertia which will make it harder to address some of the underlying issues with WebWork. In addition, it'll be interesting to see if the "best practices" of the WebWork community infect the Struts community or the other way round. We'll see.</p>      </div>
    </content>
  </entry>
  <entry>
    <id>tag:pubbitch.org,2006:entry,113633237755358</id>
    <title>Take It From the Top, Part Four</title>
    <link href="http://www.pubbitch.org/blog/2006/01/03/take_it_from_the_top_part_four" rel="alternate"/>
    <updated>2006-01-03T23:52:57Z</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
<p>A couple more thoughts about the <a href="/blog/2006/01/02/take_it_from_the_top_part_three">TestEnvironments</a> that I wrote about previously. Most of my feelings and thoughts about them is derived from <a href="http://www.amazon.co.uk/exec/obidos/ASIN/0321125215/qid=1136330713/sr=8-1/ref=sr_8_xs_ap_i1_xgl/026-0635506-6822018">Domain Driven Design's</a> ubiquitous language.
</p><p>
When talking to a developer or a QA they tend not to talk about "the acceptance test environment where the web server is bound to port 8080 and the database should be in-memory and contain a certain amount of test data" They tend to talk about the "acceptance test environment" or the "functional test environment", and by using those labels they know that one has an appserver running remotely and a database that's pre-loaded with test data and the other one is just an in-memory database. As such, I like my code to talk about the AcceptanceTestEnvironment and the FunctionalTestEnvironment.
</p><p>
Which leads on to another thing that's been buzzing around my head, and that's the fact that there's normally at least two domain models that are extant within a code-base; one in the application itself, and one in the tests that surround and support this code. The interesting thing is that quite often the same domain concept, such as User or Account, gets referred to in both places. Does this mean that the same classes representing these concepts be used in both places? I'd suggest not. Although they both happen to have the same name, they typically refer to completely different things. One refers to the abstract concept within the application, and this has radically different roles and responsibilities to the other, the on-screen representation that's used within the acceptance test code.
</p><p>
The logical extension of this is that the test code should have its own ubiquitous language that may mirror that of the application but may be very different. This means that testing code, which sometimes doesn't get the TLC that it deserves, needs to be approached with the same sort of rigour that production code should be approached with.
</p><p>
Well, that's what I think anyway. I should really get comments up and running shouldn't I?</p>      </div>
    </content>
  </entry>
  <entry>
    <id>tag:pubbitch.org,2006:entry,113621781165293</id>
    <title>Take It From the Top, Part Three</title>
    <link href="http://www.pubbitch.org/blog/2006/01/02/take_it_from_the_top_part_three" rel="alternate"/>
    <updated>2006-01-02T16:04:28Z</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
<p><a href="/blog/2006/01/01/take_it_from_the_top_part_two">Yesterday</a> I wrote about the moving parts that allow you to write acceptance tests for a Java webapp without going insane. We talked about XPath and how wonderful it is, as well as the roles and responsibilities of the various classes within the system. I also mentioned that I was undecided about what to blog about because there were two things that I really liked and wanted to tell you, my dear reader, all about. Well, now it's time for this mysterious second thing.
</p><p>
I can tell that you're on tenterhooks. It's why I blog so often.
</p><p>
Let's take a step back from  our webapp and think for a second. We want the acceptance tests to be fast, but we'd also like to be able to run them against a stack as similar to that used in production as possible. Unless you happen to be deploying to <a href="http://jetty.mortbay.com/">Jetty</a> it's likely that deployment takes the better part of 5-10 minutes and dozens of mouse clicks (yes, WebSphere, I'm looking at you) Our two goals appear to be mutually exclusive. And what about running our integration tests? You know, the ones that check that we're using Hibernate correctly. At the other end of the scale, those pesky QA people will probably have their own test environments that they'll want to use, and we'd like to be able to run our automated acceptance tests against those too. 
</p><p>
Hang on! "Test environments"? There's a pattern in there somewhere (bear with me, I've not written one of these before)....
</p><p>
<i>Automated tests could be run in a variety of test environments</i>
</p><p>
<b>Therefore, create a class to represent each expected test environment</b>. This class can be used to obtain information about resources used or presented by the application, such as DataSources or the base URL of a webapp.
</p><p>
Ideally, a TestEnvironment should be created by calling the default constructor, and before being created a check should be performed to see whether or not another TestEnvironment is in use:
</p><p>
<pre>
private TestEnvironment setUpEnvironment() throws Exception {
    if (TestEnvironment.isEnvironmentAlreadySetUp()) {
        return TestEnvironment.getCurrentEnvironment();
    }
    return new AcceptanceTestEnvironment();
}
</pre>
</p><p>
The smarter amongst you will probably tell me not to use statics for this sort of thing, and will suggest a better solution. I can live with a bit of constructive criticism.
</p><p>
As an aside, if a particular environment doesn't require a resource (such as an app server in your integration tests) then I've found it best for the TestEnvironment to throw an exception rather than simply returning null. YMMV. You'll also find that instantiating the TestEnvironment in a <a href="http://www.junit.org/junit/javadoc/3.8.1/junit/extensions/TestSetup.html">TestSetup</a> decorator or in a TestSuite is a Good Thing.</p>      </div>
    </content>
  </entry>
  <entry>
    <id>tag:pubbitch.org,2006:entry,113613893467582</id>
    <title>Take It From the Top, Part Two</title>
    <link href="http://www.pubbitch.org/blog/2006/01/01/take_it_from_the_top_part_two" rel="alternate"/>
    <updated>2006-01-01T23:54:07Z</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
<p>Previously, I'd <a href="/blog/2005/12/08/take_it_from_the_top">posted about testing a webapp</a> We'd started at the top level, looking at how we could write our acceptance tests. This is all very well, but what's sitting underneath that? I'm not quite sure where to start here because there are two things that I've seen that I really like. Let's do the one that saves the most lines of code, and then, in another post, the other one.
</p><p>
Our application appears to the user as a series of pages. It makes sense that the acceptance tests should therefore query <b>Page Objects</b> about what it expects to see. Each Page Object represents a single page within your system and might be composed of smaller elements, such as a NavigationMenu. Personally, I like to query the Page Objects using objects representing what I expect to see on the page, other people like to simply use strings. Either way is fine, but the trick is to avoid exposing the raw structure of the page to the asking object, therefore:
</p><p>
<pre>
accountPage.isAccountSummaryDisplayedFor("Barry");
</pre>
</p><p>
is infinitely preferable to:
</p><p>
<pre>
Iterator i = accountPage.getAccountSummaryTable().rows().iterator();
...
</pre>
</p><p>
The Page Object relies on an interface wrapping our browser implementation (<b>WebDriver</b> and <b>Browser</b> are both names that I've seen) that exposes the current DOM which can then be queried using XPath. Because you're simply exposing the DOM and providing a few simple methods to submit forms and click links, the WebDriver interface can be pretty slim, reducing the pain of writing new implementations. In order for this to work, your webapp should be generating valid XHTML. At the very least, it should be returning well-formed XML.
</p><p>
There are several reasons to use XPath, but the most important ones are that it helps reduce the lines of code required to write tests, improving the maintainability of the tests, it's easy to read (especially if you stick with a limited subset of XPath), and it means that there aren't dozens of barely used classes cluttering your system ("Table", "TableRow", "TableCell", *shudder*) In addition, because these cluttering classes don't exist, you're far less tempted to break the encapsulation of the Page Objects. Hurrah! 
</p><p>
Let's now think about the roles and responsibilities of the various layers we've just introduced. The acceptance tests are the only places where you should be making assertions (which also makes it clearer exactly where a test is failing because you don't need to dig through a stack trace). This means that the deeper levels should simply provide boolean methods for testing the (lack of) presence of constructs within the page. The acceptance tests have no idea of the structure of the page. This knowledge is held within the Page Objects, which know how to <tt>clickOnLinkForAccount("foo")</tt> and tell you <tt>isRecordDisplayedFor(barry)</tt>. The Page Objects use XPath to query the DOM, which is obtained from the WebDriver. The WebDriver knows how to communicate with the outside world, how to click on links and submit forms.
</p><p>
So, looking back, the stack looks something like:
</p><p>
<b>Acceptance Test</b> makes assertions based on boolean methods on <b>Page Objects</b>, which use XPath to query the DOM returned by the <b>WebDriver</b>.
</p><p>
BTW, If you're interested, Mike Roberts has a post about <a href="http://www.mikebroberts.com/blog/archive/Tech/Tools/WikisandSourceControl(withFitnesse,SubversionandCruiseControl.NET).html">using wikis to store acceptance tests</a>.</p>      </div>
    </content>
  </entry>
  <entry>
    <id>tag:pubbitch.org,2005:entry,250</id>
    <title>Take It From the Top</title>
    <link href="http://www.pubbitch.org/blog/2005/12/08/take_it_from_the_top" rel="alternate"/>
    <updated>2005-12-14T21:19:36Z</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
<p>It's time to talk testing. Specifically, it's time to talk about a strategy for testing java web apps. Let's take it from the top, from the level of the person writing the initial tests, down to the place where the developers live. Let's not rush ourselves; we've got a while so more than one part is okay.
</p><p>
At the top, there's one of two types of acceptance test. The initial type is a normal looking JUnit test, with several "testFoo" methods. The difference is that these tests are written in the language of the business:
</p><p>
<pre>
public void testNavigationLinksShouldMaintainTheQueryUsedToGenerateTheResultList() throws Exception {
        loginAs("Barry White");
        selectDJ("Dave Lee Travis");
        searchForMyRecordsOwnedByTheDJStartingWith("t");
        countNumberOfPagesOfResults();
        gotoNextPage();
        countNumberOfPagesOfResultsAgain();
        verifyThatTheNumberOfPagesSeenWasTheSameBothTimes();
}
</pre>
</p><p>
Even my mum could read that. These are written by the QAs using an IDE that generates stub methods so that the thing compiles cleanly. The cleanly compiling tests are then checked in to our source control system and run as part of our normal build. 
</p><p>
But wait! Surely those tests won't pass, and will therefore cause the build to fail horribly? Not if you <b>use a TestSuite that supports allowing a test to fail until the first time it's passed, and then never allows it to fail again</b>. You could even produce a nice summary page in your Continuous Integration environment to allow the business to track the progress of the project.
</p><p>
The other type of test that we run is a FIT test. These can work really well, and they can also bring an entire project to its knees; you just need to know what's good and what's not. What's not include "clicky" tests, that interact directly and extensively with the UI. These swiftly become very brittle and require a lot of TLC to work properly as the GUI changes. What works well? I'm still exploring, but something nice and high level, that deals with the intent is clearly the way forwards. Something like
</p><p>
<pre>
| login as | Barry White |
</pre>
</p><p>
rather than 
</p><p>
<pre>
| goto |  home url |
| click | button with id | login |
| select element | username |
| enter | Barry White |
| select element | password |
| enter | secret password |
</pre>
</p><p>
One of those is clearly a suboptimal way of doing things. Guess which!
</p><p>
So, we've written our high level acceptance tests. What next? You'll just have to wait....</p>      </div>
    </content>
  </entry>
</feed>
 
