Junit Kungfu

A great presentation with audio that talks about Junit 4, test naming and other things.  I liked this presentation because it starts expression Behaviour Driven Development concepts without actually using a Behaviour Driven Development Framework.  Additionally its one of the more in-depth presentations covering the new features of JUnit like Rules.

The presenter is John Ferguson Smart who runs testing and agile tools bootcamps around Australia and NZ (as well as elsewhere) so if you are in the vacinity you should consider looking into em.

My notes from the presentation:

Naming tests

Dont use the name testXXX – you aren’t testing methods of your code

You are testing use cases – what should your class be doing

Take out private methods, maybe extract to a separate class and test those separately if they are getting too big.

Use the word should.  Express an outcome

Class Name: When is a behaviour applicable
Method Name: When at is the behaviour testing

Tests become more readable.

Arrange-Act-Assert

Create the test collaborators – Inputs and Expected Outputs (Arrange)

Test the behaviour (Act)

Verify behaviour is correct (Assert)

Extending Hamcrest Matchers

Combine matchers together – hasItem(someOtherHasPropertyMatcherYouPreviouslyDefined)

Create a new matcher –

  1. Extend TypeSafeMatcher<type of thing you are checking against)
  2. Implement Constructor take in  a matcher of expected value
  3. Implement matchesSafely(type of thing you are checking against)
  4. Implement describeTo – decorate the existing test result/matcher description…  description.appendText(“WuzUp!”); matcher.describeTo(Description);
  5. Create a Factory Class for your matchers with static factory methods to return a new matcher
  6. Use It

Multiple asserts per test are bad (see also Integration Tests are a Scam)

You can combine hamcrest matchers into one test

assertThat(itesm, allOf(hasSize(1), hasItem(“java”)));

assertThat(itesm, hasSize(greaterThan(1)));

The error messages will be cleaner too – expect list size of one, and has item java but received <blah>

Parameterized Tests

Usually just one test per Parameterized test class – they get run once

Ways to get test data

Use an xls spreadsheet source

Use Selenium 2’s WebElement to get a webpage

@FindBy(name="HD_EN") WebElement importerName;
@FindBy(name="HD_EA") WebElement importerAddress;
@FindBy(name="HD_ER") WebElement importerRepresentative;

// getters and setters
 // getter return importerName.getValue();

// setter
public void setImporterName(String value) {
     enter(value, into(importerName));
}

Smoke test to make sure getters and setters are correct

image

Make sure the annotations aren’t wrong

JUnit Rules

Delete folders after test run

@Rule public TemporaryFolder folder = new TemporaryFolder()
folder.newFile(“a file name”);

ErrorCollection,

accumulate errors rather than fail on first.  This saves having to write 20 different tests with large setup that check 20 things on the same page (eg login and load webpage table then verify each cell)

@Rule public ErrorCollector collector = new ErrorCollector();
// in your test
 collector.addError(new Throwable(“blah”));
collector.addError(new Throwable(“something else”));
collector.checkThat(result, yourMatcher);

The result will show “blah”, “something else” and the result of your failed matcher, as well as fail the test.

TimeoutRules

When you know something should have a short response time, a DAO for example should be shorter than 1 second

@Rule public MethodRule globalTimeout = new Timeout(1000);
@Test public void catchInfiniteLoopInTest() { for(;;); }

Catch any major issues before they get into production and become embarassing

Verifier Rule

Something that happens after a test is completed, like an assert

Inject behaviour to make JUnit add checks after each test…. kind of like a test post-condition or invariant from Betrand Meyers OO Software construction, but just for the tests themselves.

private List<String> systemErrorMessages = new ArrayList<String>();
@Rule
public MethodRule verifier = new Verifier() {
    @Override
    public void verify() {
        assertThat(systemErrorMessages.size(), is(0));
    }};

A good example I see would be using it to tie up mock verification calls in EasyMock

Watchman Rule

Called when a test fails or succeeds.  Additional logging perhaps?  How about restarting a GUI, or take a screenshot when a test fails.

Categories

Group tests in your own hierarchy based on your classification of the test rather than writing test suites.  Performance tests  that integration tests.  Slow running or fast running tests?

You can setup plain old normal interfaces for your categories, and have them extend each other via subclassing.  There is no Junit annotation here to indicate its an interface for testing, so you can potentially use any interface in your source.  I’m not sure if this is good practice or not, but say you wanted all your DAO tests that implemented a GenericDAO to be tested, you could do this…. or how about test all classes that implements Serializable?

You can annotate a test class, or tests methods with @Category(InterfaceName.class)

When running a category suite however you still need to include the classes to inspect as well as the category name.

@RunWith(Categories.class)
@IncludeCategory(PerformanceTests.class)
@SuiteClasses( { CellTest.class, WhenYouCreateANewUniverse.class })
public class PerformanceTestSuite {}
You can also exclude from a Run and run a
@RunWith(Categories.class)
@ExcludeCategory(PerformanceTests.class)
@SuiteClasses( { CellTest.class, WhenYouCreateANewUniverse.class })
public class PerformanceTestSuite {}

But how about scanning your whole test bed?  Can we programmatically inject suite classes and use them with Categories?  At this point it is a limitation unless you want to use a classpath hack.

Parallel Tests

If you have fast IO and multicore (like my new work PC Smile) and well written tests that don’t trodd on each others data.

U use Maven’s surefire 2.5 plugin to achieve this, and say methods, classes or both in parallel.  Classes is probably safer since most people write the test slipping thru later tests in the same class depend on earlier test methods accidentally.

Infinitest

This is a tool for IntelliJ and Eclipse that runs tests when you save your source and tells you if you have failed runs.  I remember twittering about how cool this would be if it existed a while back, and I’m glad I wasnt the only one with this idea and that someone actually implemented it Open-mouthed smile.

Also there was a plugin for IntelliJ called Fireworks but I could never get it to run tests properly on my Windows PC; always complaining about not being able to find a JDK home Sad smile.

This tool seems pretty cheap at $29 for an individual license, I’ll check it out and give it a shot.

http://improvingworks.com/products/infinitest/

What would be super cool is if it worked with Categories mentioned above, to be able to exclude GUI tests from being executed.  There may be a feature in Infinitest that handles it but I’d be keen to see.

Mockito

I’m traditionally an EasyMock guy but Mockito has always had good buzz.  At my new job we dont actually have a mocking framework yet so I’m keen to give it a look.

Mockito seems to have less verbose setup of tests, something that when learning EasyMock bashed me around a bit – ever forget to get out of record mode and get a green test accidentally.

As per Integration Tests are a scam ypresso recommends, you can verify interactions, verify a method is being called with certain params.

Other Stuff (comments from the Q&A of the presso)

Hibernate mappings – use an in-memory database

FEST asserts – an alternate to Hamcrest that avoids the Generic issues that plague Hamcrest!!! (boy this frustrates me a lot as a Hamcrest user)

Cobertura – a code coverage tool, alternate to Emma

Private methods shouldn’t be tested explicitly – you should be able to sufficiently test a class by its public facing API.

Testing Presentations

Their has been a presentation I watched last year that absolutely changed my opinion on how I tested and how I designed.  It was one of those presentations that just made sense and I cant believe I haven’t blogged about it until now.

Integration Tests are a Scam by Joe Rainsberger

This was my favourite presentation from last year. It talks about writing the correct type of unit tests to get get fast results and reduce the need for slower integration tests that are generally slower are require a lot of maintenance. He makes a compelling argument about the number of tests in your system don’t improve the sense of security you get from your tests by the same amount. So he talks about what needs to be unit tested from the contract class and the opposite collaborator class and how doing so gives you a better picture of what tests you need to give you a sense of security with quick feedback. The blurb explains it better

Integration tests are a scam. You’re probably writing 2-5% of the integration tests you need to test thoroughly. You’re probably duplicating unit tests all over the place. Your integration tests probably duplicate each other all over the place. When an integration test fails, who knows what’s broken? Learn the two-pronged attack that solves the problem: collaboration tests and contract tests.


TDD of Asynchronous Systems

This presentation by the author of a new book, “growing object oriented software, guided by tests” Nat Pryce, which talks about testing at the integration/functional level and techniques to get around all the painful ‘flickering tests’ & ‘false positives’ issues that occur when you have them. The examples talk about testing legacy systems, swing gui’s and JMS queues.

One of the key ideas is that one must find a point to synchronise on before executing the assertions and those assertions must wait (and retry if not true) until the state has changed or a timeout occurs (and the test then fails)

As a user of UISpec4j with its Assertions that have a timeout, and some perilious test code when someone innocently mixed junit assertions with a UI test, I could relate to this really well.

Tempus library for programmatic thread dumps

Turns out doing a CTRL+Break (or equivalent kill <signal> <pid> on *nix) programmatically is a bit harder than I thought.  This thread talks about ways to do it, but the easiest was to use the Tempus library which has a lot of threading tools, including a simple

ThreadDump.dumpThreads(yourPrintstream);

to get a stacktrace of running threads in a current environment.

Embedding Tomcat 7 in your App

So what if you ship with a plain old Apache server just to run some aging PHP.  If you have an server app running all the time, according to this post, it looks like you can embed Tomcat, just like you can embed Jetty

Strings and intern

Strings, yawn, boring.  They are kinda the bread and butter of our development but lets face it, we can take em for granted sometimes.

So some neat tips from this blog post about Strings in Java. The most useful one being that the compiler builds a string table of all strings that are in code.  The compiler is also smart enough to put strings together, a feature called compiler folding.  Eg “a” + “aa” in code becomes “aaa” in the string table and vars reference the same spot in the string table. All variables reference the premade strings in the string table which leads to faster equals() operations since String#equals() method checks reference equality first before doing a string size or character by character comparison. 

The problem is that if you want the faster equals, you have to have these strings at compile time.  If you have something like

String s1 = “a”; String s2 = “aa”; String s3 = s1 + s2;

then equals performed on s1 and s2 will be reference equals, really fast, but s3 won’t since it will be evaluated at runtime.

Thankfully a simple call to the String’s intern method will register a string in the string table

s3 = s3.intern();

The rest of the blog talks about common mistakes with String equality and understanding immutability. Scarily there was a potential memory leak problem with the substring() method which holds a reference to the original string’s internal value field when providing the sub string.

More importantly talks about different options to compare strings of different locales which have multiple unicode character combinations refer to the same printed character or different characters being considered equal in a locale but not as byte equals.  All things I was never aware of until reading this blog.

Legacy Java Systems

I’ve had my fair share of working with legacy code.  I don’t think legacy code is a bad thing because its existence means it has addressed the majority of the needs of the business successfully enough to stay around.  However, as time proceeds, the business needs to change in order not to become a historic entity and so legacy apps need to be updated.  This is usually where the frustration comes in.  You have to work with older development practices and styles you may not be used to.  You also may have to source old libraries or application containers in order to get things to build and deploy. 

However, I would like to think you can make a green field out of anywhere. A lot of other people may disagree, but the legacy system does have the advantage of addressing business need that your own ‘from scratch’ green fields app may not (provided the business need is still relevant).  There was a recent article on info q: Eight Quick Ways to Improve Java Legacy Systems that talked about the following areas

  • Tip #1: Use a Profiler
  • Tip #2: Monitor Database Usage
  • Tip #3: Automate Your Build and Deployment
  • Tip #4: Automate Your Operations and Use JMX
  • Tip #5: Wrap in a Warm Blanket of Unit Tests
  • Tip #6: Kill Dead Code
  • Tip #7: Adopt a ‘compliance to building codes’ Approach
  • Tip #8: Upgrade Your JRE

I’ve used a number of these techniques, but there were some I hadn’t heard of before such as the jdbcWrapper that simply wraps your jdbc driver with a logger so you can see what part of your code executes what SQL. (Tip 2)

Using JMX to trigger cache cleanups as described in Tip 4 is a great idea.  Another useful thing I’ve seen is using scripts and creating a Javascript or Groovy console within your app so that live maintenance can be performed on production environments.

Tip 5 touches on breaking dependencies, which is what you see a lot of in the code base I use. I’d like to get to know more about how to do this

Tip 6 advised that the Emma code coverage tool can also be used to find production code that isnt run (in addition to stuff thats untested)

Tip 7 really rang true to me.  Its easier to bring in all your toys under the sun, but without a common plan, everyone will bring in their own frameworks to solve the same problem and make the app even more confusing to work with.  I like ‘toys’ (frameworks, tools, etc) but I know I can get carried away Winking smile

UUID’s in Java

UUID’s are useful for making unique reference numbers of things.  In an ideal world these UUIDs are so long, they never collide.

The first place to discuss UUIDs would be RFC 4122.  The next part would be the wikipedia entry for a more concise version.

So why generate UUID’s?  One reason could be to generate a unique code to identify a particular PC (mac address?).  Another is to identify points in time (and can include the machine aka node’s mac address that created the UUID).

Further refs:

Java 6 api UUID entry
Blog that touches on UUID support in Java

Lessons in Jasper (i)Reports

I had some great fun today playing around with iReport. I used Dynamic Jasper before iReport so it was an interesting experience seeing the platform first hand and seeing all the capabilities it offered via the properties panels in iReport for the various report components. It was interesting to see the layout capabilities of a Jasper report and how Dynamic Jasper worked with these.  A quick refresher, a Jasper report is split into different bands: Title, Page Header, Column Header, optional group headers, various detail bands, optional group footers, optional column footer, Page Footer, Last Page Footer, Summary, a No Data and a Background band. DJ follows closely to the Jasper specification & its capabilities. There are some things it can do better than iReport and some it cant.  The most obvious DJ benefit is the programatic table layouts, which is all through code and means your dependent properties are known in advance when you refactor a business class – you should be able to know when your report is going to break.  One issue I’ve had with DJ is that laying out components doesn’t always happen as beautifully as you’d expect.  Sometimes the width of columns totals is too small with unexplained wrapping or sometimes the layout is just wierd.   And there is no way of knowing until runtime that the seemingly simple change has broken the layout of your wonderful report. iReport on the other hand is comprised of datasources, sql or xml, and a jrxml template you design.  You aren’t fixed into a particular table structure, you can have multiple group variables on the same field without any kind of reprocrussions and the WYSIWYG editor and preview allows you to quickly play with report layout and styles and know what you are getting.  You can also start with a SQL query as your datasource to prototype and then at runtime inject a proper JRDataSource (the same DynamicJasper uses) – easy! But in the end of the day both of these compile down to .jasper files that Jasper Reports then fills in with your data and turns into these jrprint files – effectively a softcopy of your final report that you can turn into PDF or feed into a screen viewer.

Column Headers above Group Headers

One neat feature that Dynamic Jasper provides is the ability to put the column labels in a tabular format report below the group label via its setGroupLayout() method. I didn’t realise that the only thing that Jasper supports is having a column header band above the group header band. So in iReport, this means you cant have group title followed by the column headers. The way around this in iReport is that in your Group Header, you move the column headers into the last Group Header band you want.  That way each time the group header gets displayed, so too will your Column header, underneath the Group label.

SubReports for non-data areas

The other trick I learnt was that when generating subreports that dont normally have data in them, for example a Header and Footer section, you need to be change the subreports ‘When No Data’ value to something like ‘All Sections, No Detail’ so that it doesnt simply show a blank page when you preview it and include it in a master report.

One thing that doesn’t work is that you can’t put the total page count in a subreport since its evaluated at report completion time and the subreport is already processed by the time its done.

You can’t do that on Television

Layout wise the iReport stuff really starts taking a big lead over DJ.  The biggest case in point was the Sales Report dashboard demo that shows a 2 x 2 panel dashboard with a graph, table, pie chart and timeline chart.  The right column isn’t as wide as the left and there are also some neat tricks about using the REPORT_COUNT variable thats in each jasper report to limit the size of the dataset to do Top 5, Top 10 style reports.   The video is worth watching just so you know some other little tricks.  For example a foreground alpha channel percentage setting that gives pie charts a futuristic look is worth knowing about.

Pain in the bum Error Messages

Here DJ is better than iReport.   One frustrating experience I had with iReport is an error message about an expression with unexpected ‘+’ token.  I looked through all the expression fields I could find, searched the XML but couldnt find the place it was talking about.  In the end I started the report again from scratch and slowly recreated the components from the old one trying to eliminate which was the problematic component.  One thing that I noticed in the screencasts is that the designers use the Preview button frequently after any change that introduces a new variable, param or field, or datasource, or whatever.  There are a lot of silent traps you can get into and therefore a lot of checks you need to do across the way. Dynamic Jasper is a bit better since

  1. you are limited in what you can do somewhat, you aren’t generally going to get a compile error,
  2. the error messages are a little clearer but not always,
  3. you have the source code and its a little easier to understand.

Conclusion

iReport and DJ both offer various views into the Jasper Reports framework.  Its certainly knowing both.  Since you can combine them, you can use iReport to do the complicated formating and embed DJ reports.

References

http://www.opentaps.org/docs/index.php/Tutorial_iReport
http://www.jasperolap.org/plugins/espforum/view.php?group_id=83&forumid=101&topicid=13489
http://jasperfor
http://jasperforge.org/plugins/mwiki/index.php/Ireport/Tutorialsge.org/uploads/publish/ireportwebsite/IR%20Website/tutorials/Getting%20started%20with%20iReport/Getting%20started%20with%20iReport.html

http://jasperforge.org/plugins/mwiki/index.php/Ireport/Product_Tour
http://jasperforge.org/plugins/mwiki/index.php/Ireport

Apache Commons reflectionHashCode builders

This was my response to this short post about using reflectionHashCode to implement your objects hashCode method dzone.com – overriding equals() and hashcode() – best practice

I’ve come to love the Apache commons builders over the last couple of years and use them where possible.  They avoid a lot of boilerplate.

Wanted to raise something with regard to the hashcode builder.  Today I watched a webinar, ‘Hash hash, away it goes’ (http://www.javaspecialists.eu/webinars/ and also described in text here: http://www.javaspecialists.eu/archive/Issue031.html) which was about writing hash code methods on your objects and using them as key objects in your HashMaps – and what happens when they arent written properly causing elements to not be retrieved from the map! 

One of the summary points of this article was that hashCode methods should be as fast as possible and one of the participants asked about the apache commons reflection hashcode builder.  Because the reflectionHashCode uses reflection it wont be the fastest possible implementation possible that you can write, so if you are concerned about performance, then you should consider using the other non-static implementation of HashCodeBuilder (granted not so dynamic).  The javadoc for HashCodeBuilder (http://commons.apache.org/lang/api/org/apache/commons/lang/builder/HashCodeBuilder.html) mentions reflection is slower than testing explicitly.

One thing you could do is if using dynamic fields though (say in a language like Groovy that allows fields added at runtime) is consider the fields which are immutable identity fields and have your hashcode builder just combine the hashcodes for those.  Presumably (and its a big presumption), your fields that are being added dynamically arent immutable and arent defining the identity of the object.  So the question is, do you really need to add those fields to your hashcode method in the first place?  You could then just define a hashcode builder on the immutable id fields that are in the base class and it shouldnt change how your HashSets and Maps behave regardless of dynamic fields added.  But if your new fields are added from subclasses to a parent domain, there is the appendSuper method on the HashCode builder

Hyperic HQU Monitoring

As a software dev who worked with an ISP, I learnt monitoring is a big thing.  Customers have availability metrics we need to meet, and more importantly customers are paying for a service.  When it goes down, they shouldnt have to be the ones to call it up to let you know, and they should also find out about an outage as early as possible.  Well before their bosses come knocking on their door.  And regardless of size, monitoring is still a big deal for any sized company and support team, and glad to say of interest at the company I work for now.

In the last 12 months I’ve been getting to know the Hyperic Monitoring Platform.  It has an open source community edition with an automatic free barrier to let small teams monitor what is important.  Additionally it is easily configurable via a web UI, which allows non-technical staff to add monitoring, setup alerts and that sort of thing.  Most importantly it is configurable via an api (or two).  There is the HQ API which is a REST api, and the Groovy API. 

I haven’t need to venture into the API at all to date but did recently get asked the question if HQ was able to export views and reports.  The good news after watching this vid is that you can.

The interview with Jon Travis reveals that the API is not only capable of exporting its information, but it also allows developers to add screens into their HQ installs.