There’s been much debate about how much code should be tested. In particular, whether to test accessor methods (here, I’ll call accessor methods both getters and setters, i.e. methods to read or write an object’s property) has often been the subject of such debate.
While I myself think that testing effort should be devoted to more interesting things, others differ. Whether we agree or not, we might be working for a customer whose test coverage requirements are unsatisfiable without testing them. These requirements can be as high as 100%.
In such circumstances there’s an easy way to have your accessor methods tested, thanks to OpenPojo. Please, take a look at their wiki to have an idea of what this library is about.
Now let’s see a way to take advantage of it for our particular scenario.
First of all, we’ll need the following dependency if we’re using maven:
<dependency> <groupId>com.openpojo</groupId> <artifactId>openpojo</artifactId> <version>${openpojo.version}</version> </dependency>
I’ll also include the Cobertura Maven plugin to check the test coverage later on:
<reporting> <plugins> <plugin> <groupId>net.sourceforge.cobertura</groupId> <artifactId>cobertura</artifactId> <version>${cobertura.version}</version> </plugin> </plugins> </reporting>
Now, suppose we have a couple of data transfer objects like these:
public class Attachment { private String filename; private String base64Content; private String contentType; public String getFilename() { return filename; } public String getBase64Content() { return base64Content; } public String getContentType() { return contentType; } public void setFilename(final String filename) { this.filename = filename; } public void setBase64Content(final String contentBytes) { this.base64Content = contentBytes; } public void setContentType(final String contentType) { this.contentType = contentType; } [...]
public class MailMessage { private String from; private List<String> bcc; private List<String> cc; private String replyTo; private String subject; private String text; private List<String> to; private List<Attachment> attachments = new ArrayList<Attachment>(); public String getFrom() { return from; } public List<String> getBcc() { return bcc; } public List<String> getCc() { return cc; } public String getReplyTo() { return replyTo; } public String getSubject() { return subject; } public String getText() { return text; } public List<String> getTo() { return to; } public void setFrom(final String from) { this.from = from; } public void setBcc(final List<String> bcc) { this.bcc = bcc; } public void setCc(final List<String> cc) { this.cc = cc; } public void setReplyTo(final String replyTo) { this.replyTo = replyTo; } public void setSubject(final String subject) { this.subject = subject; } public void setText(final String text) { this.text = text; } public void setTo(final List<String> to) { this.to = to; } public List<Attachment> getAttachments() { return attachments; } [...]
We’re going to write an OpenPojo accessor validator and call it from the JUnit test classes for both Data Transfer Objects.
In order to reuse code, we can write a single validator method in a utility class. This method will receive a single parameter: the class whose accessors will be tested.
public class PojoTestUtils { private static final Validator ACCESSOR_VALIDATOR = ValidatorBuilder.create() .with(new GetterTester()) .with(new SetterTester()) .build(); public static void validateAccessors(final Class<?> clazz) { ACCESSOR_VALIDATOR.validate(PojoClassFactory.getPojoClass(clazz)); } }
Test classes can now use it:
public class AttachmentTest { @Test public void testAccesors_shouldAccessProperField() { PojoTestUtils.validateAccessors(Attachment.class); } [...]
public class MailMessageTest { @Test public void testAccesors_shouldAccessProperField() { PojoTestUtils.validateAccessors(MailMessage.class); } [...]
Time to check our test coverage:
mvn clean cobertura:cobertura
Finally, if you check the generated report you’ll see our longed-for 100% coverage in all its greenness.
That’s all for today. Make sure you read OpenPojo’s documentation. There are more use cases you might be interested in.