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(), ...

Get Quality Code: Software Testing Principles, Practices, and Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.