简体   繁体   English

如何在JerseyTest套件中使用@BeforeClass和@AfterClass

[英]How to use @BeforeClass and @AfterClass within a JerseyTest suite

It turns out that JUnit wants @BeforeClass and @AfterClass to be static and this doesn't get along well with JerseyTest's configure method override. 事实证明,JUnit希望@BeforeClass@AfterClass是静态的,这与JerseyTest的configure方法覆盖不相容。 Is there a known way to configure the Jersey application while still being able to access JUnit's utility methods? 有没有一种已知的方法来配置Jersey应用程序,同时仍然能够访问JUnit的实用程序方法?

public class MyControllerTest  extends JerseyTest {
  @BeforeClass
  public static void setup() throws Exception {
    target("myRoute").request().post(Entity.json("{}"));
  }
  @Override
  protected Application configure() {
      return new AppConfiguration();
  }
}

Hence beforeClass needs to be static, target cannot be called because of its instance-method nature. 因此beforeClass需要是静态的,因为它的实例方法性质不能调用target While trying to use the constructor instead, it turns out that configure is run after the constructor and this prevents the setup-request to be executed and will therefor fail naturally. 在尝试使用构造函数时,结果是在constructor之后运行configure ,这会阻止执行setup-request并因此自然失败。

Any advice is more than appreciated, thanks! 任何建议都非常感谢,谢谢!

What we did in several cases to avoid heavy setups in such situations is to use a boolean flag to run that setup conditionally. 我们在几种情况下所做的是为了避免在这种情况下进行繁重的设置是使用布尔标志来有条件地运行该设置。

public class MyControllerTest extends JerseyTest {

  private static myRouteSetupDone = false;

  @Before
  public void setup() throws Exception {
    if (!myRouteSetupDone) {
      target("myRoute").request().post(Entity.json("{}"));
      myRouteSetupDone = true;
    }
  }
  @Override
  protected Application configure() {
      return new AppConfiguration();
  }
}

@Before不需要静态修饰符,并且将在每个测试方法之前执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JunitPerf中使用@BeforeClass和@AfterClass? - How to use @BeforeClass and @AfterClass in JunitPerf? @BeforeClass 和@AfterClass 方法没有被执行 - @BeforeClass and @AfterClass methodis not executed 如何在TestSuite界面中使用junit @Beforeclass && @Afterclass? - How can I have a junit @Beforeclass && @Afterclass in my TestSuite interface? 如何在Junit3中获得@BeforeClass和@AfterClass等价物? - How can I get @BeforeClass and @AfterClass equivalent in Junit3? 如何使用TestNG在循环中运行@beforeclass @test和@Afterclass案例 - How to run @beforeclass @test and @Afterclass cases in a loop using TestNG 如何要求JUnit测试实现@ BeforeClass / @ AfterClass“抽象方法”? - How to require that JUnit tests implement a @BeforeClass/@AfterClass “abstract method”? 将参数传递给@BeforeClass和@AfterClass方法 - Passing params to @BeforeClass and @AfterClass methods 如何在TestNG框架中的@BeforeClass方法中使用datadriven测试 - How to use datadriven test within @BeforeClass method in TestNG framework 如何使用静态@AfterClass 注入 - How to use injection with static @AfterClass 使用Failsafe + Cucumber Parallel插件时无法使用@BeforeClass @AfterClass等Junit或TestNG注释 - Not able to use Junit or TestNG Annotations like @BeforeClass @AfterClass while using Failsafe + Cucumber Parallel plugin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM