事实证明,JUnit 希望 @BeforeClass
和 @AfterClass
是静态的,这与 JerseyTest 的 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
之后运行,这会阻止设置请求的执行,因此自然会失败。
非常感谢任何建议,谢谢!
最佳答案
为了避免在这种情况下进行繁重的设置,我们在几种情况下所做的是使用 boolean 标志有条件地运行该设置。
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();
}
}
关于java - 如何在 JerseyTest 套件中使用 @BeforeClass 和 @AfterClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38654948/