Using the eclipse template to create test cases

Often I find that I create the same unit test methods for the getters \ seters, c'tors and Object methods (hashCode, equals and toString). What I'm trying to achieve with the Eclipse IDE is the 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 using eclipse?

+5
source share
2 answers

. eclipse, , , , . Unit Test.

. Junit 3, Junit 4 TestNG. Junit 4 TestNG . .

.

, .

+11

, , . , , Java .

, , - . , . - - " + " = , , -- .

Java -- . , . ; , , .

, , . , . , ,

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

. , . , , , JVM ( ), , .

+1

All Articles