3

事实证明,JUnit 想要@BeforeClass并且是静态的,这与 JerseyTest 的方法覆盖@AfterClass不符。configure是否有一种已知的方法来配置 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();
  }
}

因此beforeClass需要是静态的,target因为它的实例方法性质而不能被调用。在尝试使用构造函数时,事实证明它configure在之后运行constructor,这会阻止执行设置请求,因此自然会失败。

任何建议都非常感谢,谢谢!

4

2 回答 2

1

我们在几种情况下为避免在这种情况下进行繁重的设置所做的是使用布尔标志来有条件地运行该设置。

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();
  }
}
于 2016-07-29T11:24:30.270 回答
-2

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

于 2016-07-29T09:22:36.783 回答