In my previous post, I explained about
mean bean and given you an example that tests Employee bean. In this post, I am
going to explain the implementation of testBean method.
As per the documentation of mean bean,
the testBean method implemented like below.
for i in 1 .. numberOfIterations do for each property in public getter/setter method pairs do generate suitable test data for property invoke setter with test data invoke getter test that getter returned same value as passed to setter end for end for
For every iteration, ‘mean bean’ sets a
public property and call the respective getter property. Test will pass, if the
getter method return same value as passed to setter. The default iterations are
100.
You can set number of iterations to
perform by using setIterations method.
BeanTester beanTester = new
BeanTester();
beanTester.setIterations(15);
If the getter method doesn’t return the
same value that is set by setter method, test will fail with proper message.
Employee.java
public class Employee { private String name; private int id; public String getName() { return name.toUpperCase(); } 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; } }
getName() function is implemented like
below. It converts the name to uppercase and return.
public String getName() {
return
name.toUpperCase();
}
When you run testBean method on Employee
class, test will fail, because getName() function don’t return the same value
set by setName() method, it is returning the uppercase version of string.
import org.junit.Test; import org.meanbean.test.BeanTester; public class TestBean { @Test public void testEmployee() { BeanTester beanTester = new BeanTester(); beanTester.testBean(Employee.class); } }
Run
TestBean.java, you will get following error in console.
Apr 15, 2016
6:16:15 PM org.meanbean.test.BeanPropertyTester testProperty
INFO:
testProperty: Property [name] getter did not return test value. Expected [TestString:[6857449200479584740]] but
getter returned [TESTSTRING:[6857449200479584740]].
No comments:
Post a Comment