Archive

Author Archive

Integrating Unit Tests

August 19, 2009 Leave a comment

There’s been a lot of talk about TDD, Responsibility Driven Design, and what constitutes a unit test versus an integration test this week at work.  It got me thinking about the definitions.  I used to think it was very clear cut. Unit tests isolate a single class, integration test don’t.

Is the line so simple though?  What about a unit test that tests a method that uses other methods within the same class to get its work done.  Is testing the results of that method a unit test?

For example:

// Say I wanted to test the method.
String makeDeposit( String xmlData ) {
def transaction = parseRequestXml( request )
def receipt = bankBusinessObject.deposit( transaction )
return makeResponseXml( receipt )
}

To be a unit test, I should at least mock the bankBusinessObject, and should probably ask the Mock to fail my test if the deposit method isn’t called.

But, it seems that to be a true unit test for makeDeposit, I should also mock parseRequestXml and makeResponseXml. And, for the makeDeposit unit test, rather than test that you get the expected xml repsponse data given the controlled input data, just ensure that makeDeposit calls parseRequestXml to parse it, bankBuisinessObject.deposit to act on it, and makeResponseXml to marshal the results.

I did some brain storming and came up with a new classification scheme for organizing my tests.

Unit

  • Macro Unit Tests – Tests isolated to a single class.  This is where you’d see testing of methods which call other methods to get some of their work done.
  • Micro Unit Tests – Tests isolated to a single method.  Completely test the method in isolation, mocking as necessary.   If you work in a language like Groovy and want to unit test a method that calls or depends on other methods in the same class, mock the dependent methods individually and have the mocks fail the test if they’re not called as expected, in the order expected.  I’m not sure if that is possible in Java.  (Maybe with some sort of test harness with AOP involved to intercept the method calls during the test??)

Integration

  • Standard Integration Tests – Integration tests that span other methods, classes, depend on other services, databases, etc.
  • Requirements Tests – Tests for those when you want to test a specific requirement or bug fix.  Very helpful when something changes later that breaks the bug you fixed a few releases back.  Or when the customer opens a ticket asking for functionality that contradicts a previous request.

Thoughts?

Groovy Strings n Things

November 17, 2008 4 comments

I’d like to take a moment to speculate on one of our previous posts,  A quirk in Groovy maps and GString coercion.

This post brought up a good example of some buggy behavior in Groovy when it comes to GStrings and Maps.  However, I don’t think the problem is in the Map, but in mixing/matching java.lang.String’s with groovy.lang.GString’s, and in particular on existing Java class methods that expect Strings.  Taking the two examples from the previous post..  

Exhibit A:

println map[“$aVar”] //still good, this outputs x.

println map.get(”$aVar”) // oh snap! this outputs null!

 

Exhibit B:

println ‘a’ == “$aVar” // true

println ‘a’.equals(”$aVar”) // double snap!! it’s false!!

I think there’s a pattern..  

In the first part of both of these example, we’re taking advantage of the operator overloading that Groovy provides and mix/matching java.lang.Strings and groovy.lang.GStrings, which seem to play nicely together in those cases.  It appears that the GStrings are being evaluated before being coerced into Strings.  The coercion must be happening at runtime for the dynamic GString to have it’s $value translated and put into the resulting String.

However, in the later case of both of these examples, we’re calling existing methods on existing Java classes (java.util.Map and java.lang.String).   These methods take Strings not GStrings.  In these cases, it looks like Groovy still coerces these GStrings into Strings, but my hunch is that it must do so at compile time in order to play nicely with statically typed Java methods on statically typed java objects.  This would mean that it wouldn’t have enough information at that time to replace the $aVar with it’s dynamic runtime value and therefore doesn’t know what else to do but use the literal String value.

This is all speculation of course, but this model makes sense of what we’re seeing here.  Is there anyone who knows enough about the internals of Groovy to confirm or deny this speculation?

Tags: ,

JavaOne Recap II

June 16, 2008 Leave a comment

This is part 2 of 2 of my experience at JavaOne 2008. In my first post, I wrote about coming features in the next versions of Java SE, EE and JSF. In this one, I’ll write about notes I took concerning JavaFX, Project Hydrazine, jMaki, NetBeans, and Scala.

JavaFX

JavaFX got a lot of attention by Sun this year and IDE support is supposed to be here now in Netbeans. I have to say though walking around the conference, the vibe was a little cynical and skeptical about JavaFX. Some of the negative comments I heard concerned the immaturity of IDE support, complaints about the syntax, and questions about competition from Silverlight and Flex.

Project Hydrazine

Project hydrazine seems to be Sun’s product in the whole cloud computing bandwagon. They talked about it, but didn’t really elaborate on it in any detail. The hydrazine marketing site is here.

jMaki

I heard a lot of buzz about this amongst the crowd. It sounded pretty cool. So yes, it’s yet another Javascript library to learn, but the difference is jMaki doesn’t try to re-invent the wheel, but uses multiple Javascript libraries to provide you with one interface to use the features of any of them. You could theoretically combine the features you like in dojo with the things you like in YUI and use only one, jMaki interface.

Netbeans

Netbeans was the IDE of choice for the Hands-On-Labs. I’ve never been a big fan of the Netbeans IDE’s in the past, because despite whatever features they might have had, it always seem rather slow and clucky to me. But it ran stellar during the labs. It had many of the same features I see in Eclipse and Intellij. It’s definitely on par with them. They showcased the javascript support at the conference. It was really good! Much better than I’ve seen in Intellij and Eclipse.

Scala

A lot of buzz around this. There was some unofficial talk about it as a possible replacement for the Java Language (i.e. Java 3). If you haven’t heard about Scala before, it’s a language with similar syntax to Java. It’s often described as both an Object Oriented and a Functional programming language.

JavaOne Recap

May 13, 2008 1 comment

I had the opportunity to attend Java One this year! What follows is part 1 of a 2 part post describing my experience and things I learned.

The Theme

Sun was really pushing JavaFX and Mobile device development this year, although I’ve heard that the mobile push is every year. Netbeans and Glassfish got a lot of attention. Scala was unofficially talked about by some as a potential Java 3 language candidate.

Topics of Interest

Some of the topics and news I found interesting are listed below.

Coming in Java EE 6

  • Reduction of the amount of XML you need to put in your deployment descriptor.
  • Support for annotations for your filters, servlets and listeners. My assumption is that this will facilitate the reduction in XML configuration you need to write. A great side affect of this should be when you want to use any of the numerous web MVC frameworks out there, you should only need to plop the jar in your classpath (WEB-INF/lib) folder and be up and running without the need for web.xml changes.
  • JAX-RS – An API for implementing restful WebServices.
  • Scripting is “going to be treated as a first class citizen” (Not sure what that means really).

Coming in Java SE 7

  • Concept of modules or superpackages to wrap your code for organization, versioning support, and better information hiding than private/protected scoping gives you. See JSR’s 277 and 294 for more information. (Perhaps Groovy could benefit from this :P )
  • Sun says Applets are coming back, that they’re able to shrink the JVM to around 4.5mb, and that the cold start-up time for Applets will be equally as fast (or slow ;) ) as a warm start.
  • One really cool feature they demonstrated of applets is that you can drag and drop them onto the desktop now while they’re running and they’ll run outside of the browser.

Coming in JSF 2.0

The following came from a talk from Ed Burns and another gentleman from Sun on JSF 2.0 where the first 30 minutes they talked a lot about how JSF adoption is steadily increasing and how they’ve listened to the community, understand the pain points, and will improve JSF in version 2.0 of the spec. Some of the specific improvements/changes mentioned were:

  • Better IDE support. (Not sure if this means they’ll release plugins for the major IDE’s or what)
  • Facelets will be part of the spec.
  • JSF Unit will better support testing.
  • Faces Trace will help testing.
  • Integration with Seam and Spring Web Flow. (Not sure how they mean and I haven’t used either of these, though I know the basic concept of what they are/do).
  • Will simplify custom component creation by reducing the number of artifacts you have to produce.

Mobile Device Programming

  • There were plenty of technical sessions on Mobile device API’s for communication, geolocation, etc., but most of the subject material of those were over my head so I haven’t summarized them here. I found those talks very enjoyable though.

Experience

I had a great time at Java One and would like to go back in a few years!

It’s not a conference to go to every year. I didn’t learn as much as I would have at a NFJS conference, but I got a broader range of information about the Java platform and where it’s heading and a great opportunity to meet others.

In part 2 of this post I’ll have notes on JavaFX, Project Hydrazine, jMaki, Netbeans and Scala.