使用eclipse模板创建测试用例

发布于 2024-10-22 02:06:41 字数 1716 浏览 1 评论 0原文

我经常发现自己为 getters\setters、c'tors 和对象方法(hashCode、equals 和 toString)创建相同的单元测试方法。 我试图在 Eclipse IDE 的帮助下实现此过程的自动化。 考虑这个例子:

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 */
}

单元测试类看起来像这样:

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*/ }
}

这个骨架与创建的绝大多数类类似。 可以用 Eclipse 自动化吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

能怎样 2024-10-29 02:06:41

查看快速代码。它是一个 Eclipse 插件,提供了非常好的模板功能,这正是您正在寻找的。在文档页面上查找“创建单元测试”部分。

该插件的一个非常有用的功能是自动创建单元测试。单元测试可以是 Junit 3、Junit 4 或 TestNG 类型。对于 Junit 4 或 TestNG 测试,将自动添加适当的注释。只需要配置一次。

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.

童话 2024-10-29 02:06:41

单元测试旨在表明对象的行为符合其预期行为。它们并不是为了确保 Java 语言正常工作。

这里有一个奇特的数据结构,没有任何行为。在这种情况下,每个赋值都由方法调用介导,并且每个取消引用也由方法调用介导。由于面向对象编程是“数据+行为”=对象,并且该代码缺乏行为,因此它可以被称为非面向对象代码。

有时,Java 使用非面向对象的类来促进信息传输。该类保证在进行序列化时所有信息都作为一个单元进行传输。因此,拥有这样的类并不表明代码是错误的;而是表明代码是错误的。然而,如果你遇到太多这样的类,那就大错特错了。

测试的一个关键要素是,如果测试不会失败,那么它就不是真正的测试。如果测试不能失败,那就是无事可做。假设这些字段之一不能为空,那么 setter 可能看起来像这样

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

,然后你就有了一些东西要测试。否则,您只需检查赋值运算符是否失败。顺便说一句,如果赋值运算符失败,那么我敢打赌,JVM 很快就会崩溃(而不是稍后),而且您也不能相信您的测试会正确报告。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文