O'Reilly logo

Quality Code: Software Testing Principles, Practices, and Patterns by Stephen Vance

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Bootstrapping Constructors

There is a basic dilemma in testing a well-encapsulated class that is so fundamental it is often overlooked: How do you know you have tested it correctly? You do not have access to the attributes of the class, so how do you verify that the constructor did its work correctly?

Sure, you can start with the most fundamental effect of a constructor and verify that a valid object was constructed. In the process, you also verify that no exceptions were thrown. But how do you verify that the internal state was initialized correctly (Listing 6-1)?

Listing 6-1: Testing object construction and the lack of exceptions

public void testMyObject() {  MyObject sut = new MyObject();  assertNotNull(sut); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required