Pages

25 November 2011

Using PODAM - POjo Data Mocker

In each unit testing process, providing various mock objects systematically allows testing diversified cases of software code. In this manner, entity beans or plain old java objects(POJO) can be simply mocked with using PODAM API (developed by Marco Tedone). This library is comprehensive enough to cover the basics of mocking POJOs and also flexible to develop custom data provider strategies with implementing DataProviderStrategy interface. Metadata constraints such as min-max value of an integer attribute can be created with PODAM annotations that will be evaluated at runtime. 

In order to start using Podam mock objects, podam-3.0.x.jar and log4j-1.2.x.jar must be added to buildpath of your project. Since, podam-3.0.x is depending on log4j. I used Gallery and Category classes and used the code snippet to generate mock objects.

CODE SNIPPET

PodamFactory factory = new PodamFactoryImpl();
Gallery gallery = factory.manufacturePojo(Gallery.class);
System.out.println(gallery);

CONSOLE OUTPUT

Gallery [title=GIwKbOEgYN, thumbDir=D640SGrMyz, imageDir=_3kzCUD7SF, random=true, categories=[Category [name=yHwn1687HB], Category [name=esMS2YOUXx], Category [name=jUOU5z7JBV]]]

Gallery Class

public class Gallery {// AN EXAMPLE POJO

 private String title;
 private String thumbDir;
 private String imageDir;
 private boolean random;
 @PodamCollection(nbrElements = 3)// 3 category elements will be created
 private List<category> categories;

 public List<category> getCategories() {
  return categories;
 }

 public void setCategories(List<category> categories) {
  this.categories = categories;
 }

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 public String getThumbDir() {
  return thumbDir;
 }

 public void setThumbDir(String thumbDir) {
  this.thumbDir = thumbDir;
 }

 public String getImageDir() {
  return imageDir;
 }

 public void setImageDir(String imageDir) {
  this.imageDir = imageDir;
 }

 public boolean isRandom() {
  return random;
 }

 public void setRandom(boolean random) {
  this.random = random;
 }

 @Override
 public String toString() {
  return "Gallery [title=" + title + ", thumbDir=" + thumbDir
    + ", imageDir=" + imageDir + ", random=" + random
    + ", categories=" + categories + "]";
 }
}

Category Class

public class Category {

 private String name;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 @Override
 public String toString() {
  return "Category [name=" + name + "]";
 }
}

No comments:

Post a Comment

Thx for reading! Comments are appreciated...