views:

130

answers:

5

I have a class that uses XStream and is used as a transfer format for my application. I am writing tests for other classes that map this transfer format to and from a different messaging standard. I would like to ensure that all getters on my class are called within a test to ensure that if a new field is added, my test properly checks for it.

A rough outline of the XStream class

@XStreamAlias("thing")
public class Thing implements Serializable {
 private int id;
 private int someField;

 public int getId(){ ... }
 public int someField() { ... }
}

So now if I update that class to be:

@XStreamAlias("thing")
public class Thing implements Serializable {
 private int id;
 private int someField;
 private String newField;

 public int getId(){ ... }
 public int getSomeField() { ... }
 public String getNewField(){ ... }
}

I would want my test to fail because the old tests are not calling getNewField().

The goal is to ensure that if new getters are added, that we have some way of ensuring that the tests check them.

Ideally, this would be contained entirely in the test and not require modifying the underlying Thing class.

Any ideas? Thanks for looking!

+5  A: 

May be code coverage tools is what you need. If you have 100% code coverage, all get methods have been called. If you are using eclipse, check EclEmma plugin.

Ha
Crap. I need the "stupid-helmet" badge from StackOverflow. I have Emma installed and am using it in other projects. Thank you for the reminder Ha.
Freiheit
A: 

You can use Sonar. I find it very useful and easy to set up. Also easy to integrate with hudson. They also have an eclipse plugin.

CoolBeans
A: 

If you would like to test your tests automatically than JUnit is not a good option. How you would like to ensure that your "special test" will be invoked always as last after all other tests? (I don't say it is not possible but it is against JUnit model/philosophy saying test cases should be independent of each other).

If by any chance you will decide to do this one of the possibilities is to generate Cobertura code coverage report and parse it to find if all getters were covered during the tests. Possibly there is also a way to intercat with Cobertura through some API instead of depending on output report files.

Example algorithm:

  1. run regular test suite
  2. run Cobertura
  3. run special test case
koppernickus
A: 

It sounds like you are talking about a meta test - a test of a test. IMHO it doesn't make a lot of sense. The test is the spec. So the developer of the test case should be updating the test when they add new fields. That said, here's a ghetto way to call all the getter methods of a Java bean.

public class GhettoBeanTest {
    private static class Subclass extends SomeObject {
        @Override public String toString() {
            return org.apache.commons.lang.builder.ToStringBuilder.reflectionToString(this);
        }
    }

    @Test public void ensureToStringMethodExecutes() {
        String value = new Subclass().toString();

        // teset passed - all getter methods were executed by the toString method above
    }
}
LES2
A: 

Consider Java Bean Tester: http://sourceforge.net/projects/javabeantester/

We use it very often. And we like it.

Andriy Sholokh