Junit tests are not designed to test small programming concepts:  

public void testSetTargets()
{
    List<Target> list = new ArrayList<Target>();
    list.add(new Target());
    list.add(new Target());
    list.add(new Target());

    config.setTargets(list);

    assertEquals("Expecting " + list.size() + " targets.",
                 list.size(), config.getTargets().size());
}

public void testName()
{
    customer.setName("Jenny");
    assert(customer.getName().equals("Jenny"));
}

There is no need to use junit to directly test:

  • getters

  • setters

  • default values

  • integration with third parties

  • anything that requires state

Since getters and setters are auto-generated, they typically do not contain errors.  Requiring junit tests for them all is wasteful.  The above code would be better written to test an overall concept which in the process tests the getters and setters.

If the rewritten example, the method tests the ability of the class to stream to XML, which conveniently tests all the getters and setters:

public void testTargets()
{
    List<Target> list = new ArrayList<Target>();
    list.add(new Target("mp3"));
    list.add(new Target("wav"));
    list.add(new Target("ogg"));

    config.setTargets(list);
    String xmlConfig = config.streamToXml();
    Config restoredConfig = new Config(xmlConfig);

    assertEquals("Expecting " + list.size() + " targets.",
                 list.size(), restoredConfig.getTargets().size());
    assertEquals("Expecting wav", "wav",
                 restoredConfig.getTargets().get(1).getName());
}

Do test:

  • concepts

  • complex methods

  • calculations

  • database calls

Exception: If getters or setters contain unusual or non-standard logic, or initializations use static initializers, they need to be unit tested explicitly.

blog comments powered by Disqus