java - setUp/tearDown (@Before/@After) 为什么我们在 JUnit 中需要它们?

标签 java junit junit4 junit3

相信大家都知道setUp(@Before)会在任何测试方法之前执行,而tearDown(@After)会在测试方法之后执行。

我们还知道,Junit 每个测试方法会创建一个 Test 实例。

我的问题是我们可以将 setUp 方法内容移动到类 Constructor 并删除 setUp 方法吗?保留setUp方法有什么具体原因吗?

最佳答案

这个(旧)JUnit best practices文章是这样写的:

Do not use the test-case constructor to set up a test case

Setting up a test case in the constructor is not a good idea. Consider:

public class SomeTest extends TestCase
   public SomeTest (String testName) {
      super (testName);
      // Perform test set-up
   }
}

Imagine that while performing the setup, the setup code throws an IllegalStateException. In response, JUnit would throw an AssertionFailedError, indicating that the test case could not be instantiated. Here is an example of the resulting stack trace:

junit.framework.AssertionFailedError: Cannot instantiate test case: test1   
    at junit.framework.Assert.fail(Assert.java:143)
    at junit.framework.TestSuite.runTest(TestSuite.java:178)
    at junit.framework.TestCase.runBare(TestCase.java:129)
    at junit.framework.TestResult.protect(TestResult.java:100)
    at junit.framework.TestResult.runProtected(TestResult.java:117)
    at junit.framework.TestResult.run(TestResult.java:103)
    at junit.framework.TestCase.run(TestCase.java:120)
    at junit.framework.TestSuite.run(TestSuite.java, Compiled Code)
    at junit.ui.TestRunner2.run(TestRunner.java:429)

This stack trace proves rather uninformative; it only indicates that the test case could not be instantiated. It doesn't detail the original error's location or place of origin. This lack of information makes it hard to deduce the exception's underlying cause.

Instead of setting up the data in the constructor, perform test setup by overriding setUp(). Any exception thrown within setUp() is reported correctly. Compare this stack trace with the previous example:

java.lang.IllegalStateException: Oops
    at bp.DTC.setUp(DTC.java:34) 
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult.protect(TestResult.java:100)
    at junit.framework.TestResult.runProtected(TestResult.java:117)
    at junit.framework.TestResult.run(TestResult.java:103)
    ...

This stack trace is much more informative; it shows which exception was thrown (IllegalStateException) and from where. That makes it far easier to explain the test setup's failure.

关于java - setUp/tearDown (@Before/@After) 为什么我们在 JUnit 中需要它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3648712/

相关文章:

Java 子字符串操作似乎导致 Java 1.8 中出现内存不足错误

java - 使用 java 的 Calendar 类比较日期

java - 如何对无效且不带任何参数的方法进行单元测试?

java - 使用 Mockito 模拟文件、FileReader 和 BufferedReader 类

java - 将数据从 Activity 传递到 Fragment Class Cast Exception

java - groupingBy 带 boolean 值,但添加自定义字符串作为键

java - Spring Hibernate H2 Junit 测试 - 如何在启动时加载模式

java - 如何模拟 hasNext 并获得高返回

android - 如何在 android 中使用 robolectric 在 vi​​ewpager 中滑动 frgaments

android - 错误 :Execution failed for task ':app:transformClassesWithMultidexlistForDebugAndroidTest'