java - 在运行多个单元测试之前需要设置对象

标签 java testing junit selenium-webdriver

目前我有以下代码:

public class AlterServiceSelT  
{

    @Before
    public void setupAndActivate()
    {
        System.out.println("setupAndActivate");
    }

    @Test
    public void suspendService()
    {
        System.out.println("suspendService");
    }

    @Test
    public void reActivateService()
    {
        System.out.println("reActivateService");
    }

    @After
    public void terminateService()
    {
        System.out.println("terminateService");
    }
}

运行时,我在控制台中得到以下信息:

setupAndActivate
reActivateService
terminateService
setupAndActivate
suspendService
terminateService

问题是 setupAndActivate() 的完整代码需要 15 分钟,并且需要其输出才能运行测试。理想情况下,我希望控制台输出:

setupAndActivate
reActivateService
suspendService
terminateService

这是怎么做到的?

最佳答案

尝试查看 @BeforeClass 而不是使用 @Before

BeforeClass 的缺点之一是它必须在静态方法上定义,因此您设置的所有字段都必须是静态的。

好处是您的设置只需为您类(class)中的所有测试完成一次。

您确定 15 分钟的设置最适合您的应用吗?

关于java - 在运行多个单元测试之前需要设置对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26463741/

相关文章:

java - 在andReturn中使用expect的方法参数

java - 类未找到异常 : Didn't find class "java.sql.SQLType" on path: DexPathList

java - 字符串内存分配

java - 使用 OpenJPA 时 ORM 出现死锁

testing - 如何在测试中模拟外部依赖?

java - 使用 Gradle 和 TestNG 跨多个节点划分测试

python - Django 测试运行程序忽略 --settings 选项

java - 这是对该方法进行单元测试的好方法吗?

java - 日期和时间相减

java - 在需要 Maven 预集成测试执行的 Eclipse 中运行 JUnit 集成测试?