jonm.dev

The Art of Writing Software



Unit Testing Boolean Setters and Getters

Series [ Unit Testing Setters and Getters ] Tags [ boolean, Java, unit test ]

It’s crunchtime down on the ranch, so just a quick post today:

public class Foo {
  protected boolean theBool;
  public void setTheBool(boolean b) {
    theBool = b;
  }
  public boolean getTheBool() {
    return theBool;
  }
}

with:

public class TestFoo extends TestCase {
  private Foo foo;
  
  public void setUp() {
    foo = new Foo();
  }
  
  public void testSetsOwnTheBoolForSetTheBool() {
    foo.theBool = true;
    foo.setTheBool(true);
    assertEquals(true, foo.theBool);
    foo.theBool = true;
    foo.setTheBool(false);
    assertEquals(false, foo.theBool);
    foo.theBool = false;
    foo.setTheBool(true);
    assertEquals(true, foo.theBool);
    foo.theBool = false;
    foo.setTheBool(false);
    assertEquals(false, foo.theBool);
  }

  public void testReturnsOwnTheBoolForGetTheBool() {
    foo.theBool = true;
    assertEquals(true, foo.getTheBool());
    foo.theBool = false;
    assertEquals(false, foo.getTheBool());
  }
}

Again, this is in the context of creating an IDE command insert-getter-setter-tests which would take the property name (theBool), property type (boolean), and variable name of the class under test (foo) and generate the text for the two unit tests shown above. Did I miss anything for this one?