【问题标题】:How to call a set-up method before certain JUnit tests如何在某些 JUnit 测试之前调用设置方法
【发布时间】:2018-07-12 20:31:48
【问题描述】:

我正在使用 JUnit 为操作 JSON 对象的方法创建单元测试。我想在我的测试文件中创建一个方法,该方法设置将在我的一些(但不是全部)测试方法中使用的对象。如何有选择地运行设置方法并将其生成的 JSONObject 传输到测试方法?有一个更好的方法吗? 这是我对设置方法的想法(使用 gson):

    public static JsonObject prepareTestJson(){
        JsonObject testJson_container = new JsonObject();
        JsonObject testJson_inner = new JsonObject();
        String ContactInfo = "+1 111 111 1111";
        testJson_inner.addProperty("Contact", ContactInfo);
        testJson_container.add("ID", testJson_inner);
        return testJson_container;
    }

【问题讨论】:

  • 你可以做一个不同的测试类。在您的情况下,这可能更合乎逻辑。您也可以只创建一个非测试用例方法并在每次需要时调用它。可能还有一种 JUnit 方式可以做到这一点,类似于 C++ 中的Boost Test's suites,但我并不熟悉。
  • 为什么不简单地从他们需要的测试中调用该方法呢?另一种可能性是将需要这种准备的测试移动到一个单独的测试类中,其中准备是在一个普通的@Before 方法中完成的。 (是的,每个测试单元拥有多个 TestClass 是合法的...)

标签: java unit-testing junit gson junit4


【解决方案1】:

我如何有选择地运行设置方法并将其转移到 生成 JSONObject 到测试方法?

不,您不能选择应应用带有@Before 注释的方法的测试方法。

有没有更好的方法来做到这一点?

您可以在每次测试之前简单地调用此方法(您也可以将其设为private 和一个实例方法以减少其访问:

private JsonObject prepareTestJson(){
    JsonObject testJson_container = new JsonObject();
    JsonObject testJson_inner = new JsonObject();
    String ContactInfo = "+1 111 111 1111";
    testJson_inner.addProperty("Contact", ContactInfo);
    testJson_container.add("ID", testJson_inner);
    return testJson_container;
}

如:

@Test
public void foo(){
  JsonObject json = prepareTestJson();
  ...
}

另一种选择是将您的测试类分成两部分:一个包含需要 setup() 的测试,另一个包含不需要它的测试。
通过这种方式,您可以使用带有 @Before 的 init 方法,或者简单地将这些语句放在类构造函数中。
例如:

private JsonObject testJson_container;

@Before
public void prepareTestJson(){
    testJson_container = new JsonObject();
    JsonObject testJson_inner = new JsonObject();
    String ContactInfo = "+1 111 111 1111";
    testJson_inner.addProperty("Contact", ContactInfo);
    testJson_container.add("ID", testJson_inner);       
}

但是这种方式是人为/技术上的分割,它可能会降低测试代码的可读性。
所以我认为在大多数情况下,我会坚持第一种方式。


此外,使用 JUnit 5,您有一个有趣的解决方法:使用 @MethodSource 记录为:

提供对方法返回值的访问的 ArgumentsSource 声明此注解的类。

这设计用于@ParameterizedTest,但它可能适合您的情况。

你可以写:

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;    

public class FooTest {

  @ParameterizedTest
  @MethodSource("prepareTestJson")
  void add(JsonObject testJson_container) {
    // do your logic
  }

  private static Arguments prepareTestJson() {
     JsonObject testJson_container = new JsonObject();
     JsonObject testJson_inner = new JsonObject();
     String ContactInfo = "+1 111 111 1111";
     testJson_inner.addProperty("Contact", ContactInfo);
     testJson_container.add("ID", testJson_inner);
     return Arguments.of(testJson_container);
  }
} 

【讨论】:

  • 那我应该把私有 prepareTestJson() 方法放在哪里呢?当我将它包含在 JUnit 测试类 java.lang.Exception: No tests found matching Method prepareTestJson() 中时出现此错误
猜你喜欢
  • 2010-12-05
  • 1970-01-01
  • 2017-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
  • 1970-01-01
相关资源
最近更新 更多