gpt4 book ai didi

java - 为多个 JUnit 测试类添加一个设置步骤

转载 作者:搜寻专家 更新时间:2023-10-31 20:20:59 27 4
gpt4 key购买 nike

在一个项目中有多个测试类,每个测试类包含多个测试方法。比如说,我想在运行这些测试类中的每一个之前创建一个数据库连接。无论我运行的是单个测试类、多个测试类还是测试套件,都应该建立连接。最重要的是,在多个测试类的情况下,不应一遍又一遍地调用此步骤。无论我正在运行的测试类数量如何,都应该只建立一次连接。

您能建议一种设计或任何 JUnit 技巧来解决这个问题吗?

最佳答案

您可以在测试套件中运行这些类。引用this question以及提供的答案。

或者改变你的设计并使用@BeforeClass在每个测试类之前运行一次设置的注释。

Sometimes several tests need to share computationally expensive setup (like logging into a database). While this can compromise the independence of tests, sometimes it is a necessary optimization. Annotating a public static void no-arg method with @BeforeClass causes it to be run once before any of the test methods in the class. The @BeforeClass methods of superclasses will be run before those the current class.

关于java - 为多个 JUnit 测试类添加一个设置步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18460665/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com