Chapter 9

Testing with JUnit

Testing is the cornerstone of any professional software product. Testing itself comes in many forms, at many different levels. Software is tested as a whole, complete artifact, but when broken down, each of the individual components should be tested, and therefore be testable, too.

By thinking of testing at the start of the development process, the software as a whole becomes easier to test, both for any full testing teams, if you are lucky enough to have one, and also for any automated tests. Plenty of rich, full-featured, complete testing solutions are available for testing code written for Java. One of the most ubiquitous and well-understood libraries for this is JUnit. JUnit provides a lightweight and simple interface for creating tests on any level, including unit tests, integration tests, system tests, or something more exotic like user interface tests. JUnit itself is properly integrated into many build tools such as Ant and Maven, and the more up-to-date ones such as Gradle and SBT. These tools automatically stop builds and flag errors should any test fail.

By writing tests first and making sure the tests are fully integrated into a build means that the tests will run any time a build is created, such as before code is checked in, or when a release artifact is built. This, in turn, builds confidence that the system is working correctly. Any time a test fails for a seemingly unknown reason; this can often be due to new code introducing bugs elsewhere ...

Get Java Programming Interviews Exposed now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.