Core Java

JUnit in a Nutshell: Yet Another JUnit Tutorial

Why Another JUnit Tutorial?

JUnit seems to be the most popular testing tool for developers within the Java world. So it is no wonder that there have been written some good books about this topic. But I still meet quite often programmers, who at most have a vague understanding of the tool and its proper usage.

Hence I had the idea to write a couple of posts that introduce the essential techniques from my point of view. The intention is to provide a reasonable starting point, but avoid daunting information flooding à la xUnit Test Patterns1.

 
Instead there are pointers to in depth articles, book chapters or dissenting opinions for further reading whenever suitable. The chapters are complemented by an consistent example to clarify and deepen the topics covered by each post.

So despite of the existing books and articles about testing with the tool, maybe the hands-on approach of this mini-series might be appropriate to get one or two additional developers interested in unit testing – which would make the effort worthwhile.

Let the games begin!
Idiom

Table of Contents

  1. Hello World

    cog-wheel-measurement-300x144
    Introcuction of the very basics of a test: how it is written, executed and evaluated.

  2. Test Structure

    four-phase-test-300x144
    Explanation of the four phases (setup, exercise, verify and teardown) commonly used to structure unit tests.

  3. Test Isolation

    test-isolation-300x143
    Illustration of the isolation principle based on test doubles and indirect in- and outputs.

  4. Test Runners

    runner-300x144
    Explanation of JUnit’s exchangable test runners architecture and introduction of some of the available implementations.

  5. JUnit Rules

    rule-300x144
    While not originally written for this JUnit tutorial the post gives an introduction to rules and explains how custom rules can be implemented.

  6. Unit Test Assertions

    unit-test-assertions-300x144
    Coverage of various unit test assertion techniques like the built-in mechanism, Hamcrest matchers and AssertJ.

In case you seek for assistance in TDD or JUnit testing in general, note that we provide profound training courses on that topic.

Conclusion

Although JUnit comes with an assessable amount of API, writing unit tests is anything but trivial. This JUnit tutorial explaines the basic techniques of writing well structured, isolated unit tests. It elaborates on the tool’s extensible features and introduces some useful third party supplementals.

Overall it is outlined why unit tests should be developed with the highest possible coding standards one could think of. Hopefully the ongoing example is well-balanced enough to provide a comprehensible introduction without being trivial. Suggestions for improvements are of course highly appreciated.

So thank you for reading that far! And if you happen to like this tutorial, don’t be shy and spread the word around on your preferred social media channel!

1. Do not get me wrong – I like the book very much, but the general purpose approach is probably not the best way for getting started: xUnit Test Patterns, Gerard Meszaros, 2007

Reference: JUnit in a Nutshell: Yet Another JUnit Tutorial from our JCG partner Rudiger Herrmann at the Code Affine blog.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button