Main Methods in Java Training Labs

A common pattern when going through labs for various java classes is the pattern of building some main() method, performing a number of operations and spitting out a bunch of System.out.println()s. It’s an easy way to get feedback and avoid having to start with much context. Unfortunately it’s really a procedural style and requires eyeballing the output to see if things worked as expected.

This isn’t a novel concept, I’m aware that Jeff Langr and Mike Clark have talked about using this technique to pick up new languages. The concept is to remap these sort of lab exercises as TDD style means writing some tests using JUnit or TestNG instead. It takes a few minutes potentially to explain to students how JUnit works, but the concept isn’t much more than understanding executing a main method. There are some good bonuses:

  • If they haven’t done TDD, the students get a chance to experiment with it in a classroom environment when they’re already prepared to learn new things.
  • They don’t have to do strict TDD to get a benefit, but they can still take a learning test approach. Not sure what a given method will return just write a quick assertion and check.
  • You have executable examples to look at long after you leave the class. This can be a benefit when you need to troubleshoot some issue you’re having.
  • You don’t have to eyeball the results.

Next time you’re in a lab showing main() methods and getting feedback from System.out.println() just start with a test instead.