【问题标题】:Do i need to prefix 'test' to every unit test method i create with GWTTestCase?我是否需要为使用 GWTTestCase 创建的每个单元测试方法添加“测试”前缀?
【发布时间】:2016-04-28 16:23:12
【问题描述】:

我正在学习如何在 GWT 中执行单元测试。所以我创建了一个扩展 GWTTestCase 类的类,并在其中编写测试方法。

问题是,如果除了继承的“getModuleName”方法之外,我什至没有在这个类(公共或私有)中的一个函数前加上“test”这个词,我会得到一个“初始化错误”。

现在我做错了什么还是添加这个前缀是一个规则?如果它实际上是一个规则,这是什么原因?

public class hellotest extends GWTTestCase{
@Override
public String getModuleName() {
    // TODO Auto-generated method stub
    return "hellopackage.Hello1";
}

public void test()
{
    assertTrue(true);
}

public void test2()
{
    assertTrue(true);
}

public void notTest3()     //Produces initialization error
{
    assertTrue(true);
}
}

“初始化错误”的堆栈跟踪:-

// Compiled from Filter.java (version 1.5 : 49.0, super bit)
public abstract class org.junit.runner.manipulation.Filter {

  // Field descriptor #14 Lorg/junit/runner/manipulation/Filter;
  public static final org.junit.runner.manipulation.Filter ALL;

  // Method descriptor #16 ()V
  // Stack: 1, Locals: 1
  public Filter();
    0  aload_0 [this]
    1  invokespecial java.lang.Object() [1]
    4  return
      Line numbers:
        [pc: 0, line: 17]
      Local variable table:
        [pc: 0, pc: 5] local: this index: 0 type: org.junit.runner.manipulation.Filter

  // Method descriptor #22 (Lorg/junit/runner/Description;)Lorg/junit/runner/manipulation/Filter;
  // Stack: 3, Locals: 1
  public static org.junit.runner.manipulation.Filter matchMethodDescription(org.junit.runner.Description desiredDescription);
    0  new org.junit.runner.manipulation.Filter$2 [2]
    3  dup
    4  aload_0 [desiredDescription]
    5  invokespecial org.junit.runner.manipulation.Filter$2(org.junit.runner.Description) [3]
    8  areturn
      Line numbers:
        [pc: 0, line: 48]
      Local variable table:
        [pc: 0, pc: 9] local: desiredDescription index: 0 type: org.junit.runner.Description

  // Method descriptor #26 (Lorg/junit/runner/Description;)Z
  public abstract boolean shouldRun(org.junit.runner.Description arg0);

  // Method descriptor #28 ()Ljava/lang/String;
  public abstract java.lang.String describe();

  // Method descriptor #30 (Ljava/lang/Object;)V
  // Stack: 2, Locals: 3
  public void apply(java.lang.Object child) throws org.junit.runner.manipulation.NoTestsRemainException;
     0  aload_1 [child]
     1  instanceof org.junit.runner.manipulation.Filterable [4]
     4  ifne 8
     7  return
     8  aload_1 [child]
     9  checkcast org.junit.runner.manipulation.Filterable [4]
    12  astore_2 [filterable]
    13  aload_2 [filterable]
    14  aload_0 [this]
    15  invokeinterface org.junit.runner.manipulation.Filterable.filter(org.junit.runner.manipulation.Filter) : void [5] [nargs: 2]
    20  return
      Line numbers:
        [pc: 0, line: 93]
        [pc: 7, line: 94]
        [pc: 8, line: 96]
        [pc: 13, line: 97]
        [pc: 20, line: 98]
      Local variable table:
        [pc: 0, pc: 21] local: this index: 0 type: org.junit.runner.manipulation.Filter
        [pc: 0, pc: 21] local: child index: 1 type: java.lang.Object
        [pc: 13, pc: 21] local: filterable index: 2 type: org.junit.runner.manipulation.Filterable

  // Method descriptor #38 (Lorg/junit/runner/manipulation/Filter;)Lorg/junit/runner/manipulation/Filter;
  // Stack: 5, Locals: 3
  public org.junit.runner.manipulation.Filter intersect(org.junit.runner.manipulation.Filter second);
     0  aload_1 [second]
     1  aload_0 [this]
     2  if_acmpeq 12
     5  aload_1 [second]
     6  getstatic org.junit.runner.manipulation.Filter.ALL : org.junit.runner.manipulation.Filter [6]
     9  if_acmpne 14
    12  aload_0 [this]
    13  areturn
    14  aload_0 [this]
    15  astore_2 [first]
    16  new org.junit.runner.manipulation.Filter$3 [7]
    19  dup
    20  aload_0 [this]
    21  aload_2 [first]
    22  aload_1 [second]
    23  invokespecial org.junit.runner.manipulation.Filter$3(org.junit.runner.manipulation.Filter, org.junit.runner.manipulation.Filter, org.junit.runner.manipulation.Filter) [8]
    26  areturn
      Line numbers:
        [pc: 0, line: 105]
        [pc: 12, line: 106]
        [pc: 14, line: 108]
        [pc: 16, line: 109]
      Local variable table:
        [pc: 0, pc: 27] local: this index: 0 type: org.junit.runner.manipulation.Filter
        [pc: 0, pc: 27] local: second index: 1 type: org.junit.runner.manipulation.Filter
        [pc: 16, pc: 27] local: first index: 2 type: org.junit.runner.manipulation.Filter

  // Method descriptor #16 ()V
  // Stack: 2, Locals: 0
  static {};
     0  new org.junit.runner.manipulation.Filter$1 [9]
     3  dup
     4  invokespecial org.junit.runner.manipulation.Filter$1() [10]
     7  putstatic org.junit.runner.manipulation.Filter.ALL : org.junit.runner.manipulation.Filter [6]
    10  return
      Line numbers:
        [pc: 0, line: 21]

  Inner classes:
    [inner class info: #2 org/junit/runner/manipulation/Filter$2, outer class info: #0
     inner name: #0, accessflags: 8 static],
    [inner class info: #7 org/junit/runner/manipulation/Filter$3, outer class info: #0
     inner name: #0, accessflags: 0 default],
    [inner class info: #9 org/junit/runner/manipulation/Filter$1, outer class info: #0
     inner name: #0, accessflags: 8 static]
}

【问题讨论】:

    标签: java gwt junit gwttestcase


    【解决方案1】:

    GWTTestCase 扩展了 JUnit 3 TestCase,在 JUnit 3 中,测试方法确实应该以 test 开头。

    原因是 JUnit 是这样设计的:它在测试用例中搜索以 test 开头的公共 void 方法。这是在 Java 5 之前完成的,当时还没有注释。如今,JUnit 4(和 5)使用注解来“标记”测试方法。

    【讨论】:

    • 如果我想要扩展 gwtTestCase 的类中的“非测试用例”方法怎么办。这可能吗?
    • 如果我在该类中创建任何没有前缀“test”的函数,不管它是否私有,我都会收到初始化错误。
    • 如果您遇到错误,并且您的问题是关于错误的,您的问题应该包含错误的完整堆栈跟踪。自从我上次使用 JUnit 3 以来已经有好几年了,它真的已经过时了,而且我从来没有使用过 GWT,但是 AFAIK,JUnit 不应该关心不以 test 开头的方法。
    • 先生,我已经添加了堆栈跟踪
    • 没有。您发布的内容看起来像是反编译 JUnit Filter 类的结果
    【解决方案2】:

    除了已经回答的“技术上是否需要”这个问题;你也可以考虑约定。

    我见过的大多数单元测试......都是从 testWhatever() 开始的。

    重点是:命名是编写干净、可读、可维护的代码的核心元素。生产代码也是如此;但是,嘿,很多人认为你的测试代码比生产代码更重要……所以编写干净、可读、可维护的测试代码是软件开发的重要组成部分。

    换句话说:可能大多数人都期望测试方法以“test”开头。但最后:重点是名称说明了实际测试的内容;所以 test(), test1(), ... 等等对于测试方法来说真的很糟糕。

    (附注:如果您对编写好的测试感兴趣,请查看 assertThat;忘记使用任何其他断言调用)。

    【讨论】:

    • 好点。实际上,我在这里使用的名称只是为了让事情变得更简单。我实际上并没有使用像 test,test1 这样的名称。
    猜你喜欢
    • 2021-11-16
    • 2010-11-28
    • 2012-10-19
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多