Tuesday, March 17, 2015

Getting Started with JUnit

I had some fun learning about JUnit recently.  I've always believed that it's important to develop incrementally.  The neat thing (to me) about unit testing is that it encourages incremental development -- if you have lots of tests that don't pass, then the natural thing to do is to pick them off, one at a time, and fix them.  In grad school, and now as a professor, I've had a fair number of occasions where someone said "I'm almost done writing it up, I should be ready to compile in a day or two".  Perhaps encouraging students to develop their tests first will discourage them from falling into that pattern of behavior.

Anyhow, I built a tutorial about JUnit, for use in my CSE398 class.  Feel free to share your thoughts on the tutorial, JUnit, and test-driven development in the comments!


  1. Hello, I have read most of your JUnit tutorial and I would like to suggest some improvements.

    1) there are some well-known patterns for exception testing - e.g. http://www.mkyong.com/unittest/junit-4-tutorial-2-expected-exception-test/ or https://code.google.com/p/catch-exception/ or with Java 8 lambdas
    2) I was kind of surprised to see ant still being used for new projects. :)
    3) Would be nice to have more informative names of test methods, e.g. shouldThrowExceptionWhenAccessingEmptyCollection instead of expectException1 - yeah, I know these were just examples but still.
    4) There are very nice assertions frameworks - i.e. http://assertj.org/ - which are much nicer to user than the original JUnit assertions

    I wrote a - free! - book about writing high quality tests - maybe you would like to see it. It is available at http://practicalunittesting.com/btgt.php


  2. Thanks for the advice, and the links!