【问题标题】:JUnit test failing with BufferedWriter throwing an IOExceptionJUnit 测试失败,BufferedWriter 抛出 IOException
【发布时间】:2019-10-16 13:10:44
【问题描述】:

我将 JUnit 5 与 maven 一起使用,并想做一些涉及文件 I/O 的压力测试。

在我的 JUnit 测试类中,我有一个 @BeforeAll 方法,它假设创建要写入的文件。但是,当我运行我的测试时,setup() 方法给出了一个错误

public class StressTest {
    String fileName = "stressTest.txt";
    BufferedWriter bufferedWriter;
    long start, finish, timeElapsed, iteration = 10;

    @BeforeAll
    public void setup() throws IOException {
        bufferedWriter = new BufferedWriter(new FileWriter(new File(fileName), true));
    }
...
}

错误信息由于某种原因没有显示异常的全名

JUnit @BeforeAll method 'public void StressTest.setup() throws ja...

我花了一些时间进行故障排除,但一无所获:( 问题的原因可能是什么?

谢谢

【问题讨论】:

    标签: exception junit junit5 filewriter bufferedwriter


    【解决方案1】:

    这是完整的消息:

    @BeforeAll 方法 'public void StressTest.setup() throws java.io.IOException' 必须是静态的,除非测试类使用 @TestInstance(Lifecycle.PER_CLASS) 注释。

    您应该将其转换为静态方法或使用@Before 而不是@BeforeAll。

    顺便问一下,您使用的是 IDE 吗? 我的显示了完整的异常消息。

    【讨论】:

    • ˋ@Beforeˋ 在木星中不存在。你的意思可能是ˋ@BeforeEachˋ
    • 我使用 Intellij IDEA CE,但也从命令行尝试过。将文件操作添加到@BeforeEach 解决了这个问题。谢谢
    • 是的,对不起,我的意思是@BeforeEach(@Before 在 Junit 4 中)。很高兴听到它解决了您的问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多