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

Java 在运行多个单元测试之前需要对象设置,java,testing,junit,selenium-webdriver,Java,Testing,Junit,Selenium Webdriver,目前,我有以下代码: public class AlterServiceSelT { @Before public void setupAndActivate() { System.out.println("setupAndActivate"); } @Test public void suspendService() { System.out.println("suspendService");

目前,我有以下代码:

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
的一个缺点是必须在静态方法上定义它,因此您设置的所有字段都必须是静态的

好处是,对于类中的所有测试,您的设置只执行一次


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

谢谢。如果不激活服务,我将无法运行像suspend service这样的测试(除非我使用现有的激活服务,但这取决于已经有激活的服务)。激活服务会运行一个自动后台过程,需要15分钟。。你建议我应该怎么做?你可以考虑优化你的背景流程。是否有必要在测试环境中执行所有这些步骤?设置应用程序需要多少数据?通常测试是针对一定数量的测试数据进行的,这是事实。谢谢你的建议,科特