[英]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.