开发者

using eclipse template to create test cases

Often do I find myself creating the same unit tests methods to getters\setters, c'tors and Object methods (hashCode, equals and toString). What I'm trying to achieve, with the help of Eclipse IDE, is automation of this procedure. consider this example:

public Class Person {开发者_JAVA百科
  private String id;
  private String name;

  public Person(String id, String name){
    this.id = id;
    this.name = name;
  }

  public String getId() { return id; }
  public void setId(String id) {
    this.id = id;
  }

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

  @override
  public int hashCode(){ ... }
  public boolean equals(Person other){ ... }
  public String toString(){ ... }

  /* this class may implement other logic which is irrelevant for the sake of question */
}

The unit test class will look something like this:

public class PersonTest extends TestCase
{
  @override
  public void setup() {
    Person p1 = new Person("1","Dave");
    Person p2 = new Person("2","David");
  }

  @override
  public void tearDown() {
    Person p1 = null;
    Person p2 = null;
  }

  public void testGetId() {
    p1.setId("11");
    assertEquals("Incorrect ID: ", "11", p1.getId());
  }

  public void testGetName() { /* same as above */ }

  public void testEquals_NotEquals() { /* verify that differently initialized instances are not equals */ }

  public void testEquals_Equals() { /* verify that an object is equals to itself*/ }

  public void testHashCode_Valid() { /* verify that an object has the same hashcode as a similar object*/ }

  public void testHashCode_NotValid() { /* verify that different objects has different hashcodes*/ }

  public void testToString() { /* verify that all properties exist in the output*/ }
}

This skeleton is similar to the vast majority of classes created. can it be automated with Eclipse?


Have a look at Fast Code. It is an eclipse plugin that provides very nice feature of templating stuff which is what you seem to be looking for. On the documentation page look for Create Unit Test section.

A very useful feature of this plugin is to create unit tests automatically. Unit tests can be of type Junit 3, Junit 4 or TestNG. For Junit 4 or TestNG tests, appropriate annotations will be automatically added. One needs to configure it just once.


Unit tests are meant to show that an Object's behaviour is conforming to it's expected behaviour. They are not meant to make sure that the Java language is working correctly.

What you have here is a fancy data structure, with no behaviour. In that case every assignment is mediated by a method call, and every dereference is also mediated by a method call. Since Object Oriented programming is "data + behaviour" = objects, and this code lacks behaviour, it's a candidate for being called non-object-oriented code.

Sometimes Java uses non-object-oriented classes to facilitate transfer of information. The class guarantees that all information gets transferred as one unit when doing serialization. So having such a class isn't an indicator that the code is wrong; however, if you run into too many classes like this then something is very wrong.

One key element of testing is that it's not really a test if the test cannot fail. If the test cannot fail, it's just busywork. Assuming that one of these fields cannot be null then the setter might look like

public void setName(String name) {
  if (name == null) throw new IllegalArgumentException("name cannot be null");
  this.name = name;
}

And then you have something to test. Otherwise, your just checking to see if the assignment operator failed. As an aside, if the assignment operator failed, then I'd wager that the JVM is going to come down pretty hard sooner (rather than later) and you can't trust your tests to report correctly either.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜