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

Testing Simple Getters and Setters

The bootstrapping dilemma applies equally to testing setters and getters, but the strategy presented in the previous section gives us a plan of attack. The encapsulation of the attributes prevents us from verifying the actions of the mutators without invoking the getters. It also inhibits us from setting the attributes to request through the getters without using the setters. Therefore, we are left with using getters to test setters and vice versa, effectively testing them together as shown in Listing 6-3.

Listing 6-3: Testing setters and getters together

1 public void testSetGetAttribute() {2   MyObject sut = new MyObject();3   assertNotNull(sut);4   int expectedValue = 17;5   assertThat(sut.getAttribute(), ...

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