Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 {
  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?

like image 426
Assaf Adato Avatar asked Mar 16 '11 20:03

Assaf Adato


1 Answers

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.

like image 144
Nilesh Avatar answered Oct 05 '22 23:10

Nilesh