Junit test method for getters and setters

I have a lot of java beans in my project. I need to create a JUnit Test class for them. Test methods created using Eclipse 3.2 and junit 4.4 are as follows:

public void testGetName() { // fail("Not yet implemented"); } @Test public void testSetName() { // fail("Not yet implemented"); } @Test public void testGetEmployeeid() { // fail("Not yet implemented"); } @Test public void testSetEmployeeid() { // fail("Not yet implemented"); } 

some of my beans have over 100 fields ...

Is there a way by which I can get the only testing method for both getters and setters like testEmployeeid() , testName() , so that in these methods I can test both my setters and getters, and not use 2 diff . test methods for them ...

How do I configure eclipse for this?

+6
java unit-testing junit getter-setter
source share
4 answers

The Test Driven Development philosophy says: "Experience everything that can break." That is, focus on useful tests, instead of writing tests just for the sake of it.

Getters and seters are almost always trivial code, which in itself is not worth testing.

I know that this is not a direct answer to your request, but I thought that it would help to point it out anyway ;-) So, why do you really have to write tests for all these getters and setters first?

+25
source share

If you have 100 fields in a class (with corresponding setters / getters), I suspect your object model is not decomposed correctly. More than 100 fields sound like an unusual number of fields for an object, and I would suggest that it has several responsibilities that can be divided into several more specialized objects.

+11
source share

Perhaps you can use beanutils for Apache Commons to help automate this:

http://commons.apache.org/beanutils/apidocs/org/apache/commons/beanutils/PropertyUtils.html#getSimpleProperty%28java.lang.Object,java.lang.String%29

For example, there is a describe(Object bean) method that will return a map of all readable attributes (i.e. getters).

Then repeat this card and call:

 setSimpleProperty(Object bean, String name, Object value) 

and

 public static Object getSimpleProperty(Object bean, String name) 

And although I agree with another poster, since getters / seters are pretty trivial - I think it’s worth testing them anyway - eliminate typos, listen to changes in test properties, etc.

For example, this will dynamically extract bean getters:

 import java.io.Serializable; import java.util.Set; import org.apache.commons.beanutils.PropertyUtils; public class MyTestBean implements Serializable { private int a; private int b; private int c; private String x; private String y; private String z; public static void main(String[] args) throws Exception { MyTestBean bean=new MyTestBean(); Set prop=PropertyUtils.describe(bean).keySet(); for (Object o : prop) { System.out.println((String)o); } } public int getA() { return a; } public void setA(int a) { this.a = a; } public int getB() { return b; } public void setB(int b) { this.b = b; } public int getC() { return c; } public void setC(int c) { this.c = c; } public String getX() { return x; } public void setX(String x) { this.x = x; } public String getY() { return y; } public void setY(String y) { this.y = y; } public String getZ() { return z; } public void setZ(String z) { this.z = z; }} 

To run this code you need to load both BeanUtils, CommonsLogging and JAR for both libraries into your project.

+5
source share

I think this library is the answer to your question: http://outsidemybox.github.com/testUtils/

it tests all initial values ​​of bean, setters, getters, hashCode (), equals () and toString (). All you have to do is define a default value map and default properties / values.

It can also test beans objects with additional constructors that do not use defaults.

+3
source share

All Articles