Ensure that all getter methods were called in a JUnit test

Posted by Freiheit on Stack Overflow See other posts from Stack Overflow or by Freiheit
Published on 2010-04-01T18:14:57Z Indexed on 2010/04/01 18:33 UTC
Read the original article Hit count: 160

Filed under:
|
|
|

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!

© Stack Overflow or respective owner

Related posts about java

Related posts about junit