Mean bean is a Java open source library
used to test bean and POJOs. Mean bean is useful to do unit testing, and get
coverage.
Features Offered
a. Test Java beans and POJOs
b. Tests equals and hashCode contract
compliance
c. Supports all java primitive types, collections
such as Lists, Maps, sets etc.,
You may ask me why do we require this library?
In an Enterprise application, all POJOs,
bean and model classes take lot of code. Testing these beans, POJOs is
important, Once you are confident enough with your beans, POJOs, model classes,
you can straight away go and test your business logic. If you use library like
lombok (It provides annotations for getters, setters, toString, equals and
hashCode methods), I don't think so we still require this library. But if your application don't use libraries
like lombok, mean bean saves lot of time by unit testing beans and improves unit
test code coverage.
I am using following maven dependencies.
<dependencies> <dependency> <groupId>org.meanbean</groupId> <artifactId>meanbean</artifactId> <version>2.0.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
Theory is apart;
let me explain with an example. For example, I am going to test getter and
setter methods of Employee bean.
Employee.java
public class Employee { private String name; private int id; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Employee [name=").append(name).append(", id=") .append(id).append("]"); return builder.toString(); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Employee other = (Employee) obj; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }
BeanTester class provides testBean
method to test getters and setter methods of a bean. Following statements are
used to test Employee bean setter and getter methods.
BeanTester beanTester = new
BeanTester();
beanTester.testBean(Employee.class);
TestBean.java
import org.junit.Test; import org.meanbean.test.BeanTester; public class TestBean { @Test public void testEmployee() { BeanTester beanTester = new BeanTester(); beanTester.testBean(Employee.class); } }
I am going
to use EclEmma code coverage tool (You can install it as Eclipse plugin),
Following post explains you how to use this tool.
After
setting up EclEmma plugin, Right click on the file TestBean -> Coverage As
-> JUnit Test. By getting the coverage report, you can make sure meanbean
library tested getter and setter methods.
Lines in
green color are covered while doing unit testing, where as lines in red color
are not covered while doing unit testing. Later posts explain you,
No comments:
Post a Comment