目前我有以下代码:
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/