【发布时间】:2015-12-01 17:11:38
【问题描述】:
我编写了一个 JUnit 测试,它使用 Mockito 和 PowerMock 来模拟一些类。我正在尝试将其转换为 Cucumber 测试,但静态 PowerMock 功能不起作用。
两个相关 Cucumber 类的摘录:
跑步者
@RunWith(Cucumber.class)
public class JWTValidatorBDDTest {
}
步骤类
public class JWTValidatorCukeTest {
String tokenValue;
JWTValidator jwtValidator;
MockHttpServletRequest mockRequest;
@Before
public void before() throws IOException {
this.mockRequest = new MockHttpServletRequest();
PowerMockito.mockStatic(JWTAuthConnectionManager.class);
BDDMockito.given(JWTAuthConnectionManager.postToken(anyString(), anyString(), anyString())).willReturn(200);
Mockito.doReturn(200).when(JWTAuthConnectionManager.postToken(anyString(), anyString(), anyString()));
}
@Given("^a JWT token with the value (.*)")
public void a_JWT_token_with_the_value_(String token) {
this.jwtValidator = new JWTValidator("https://test.7uj67hgfh.com/openam", "Authorization", "Bearer");
this.tokenValue = token;
}
虽然此代码在 JUnit 测试中有效,但在此处失败 - 它进入应该模拟的 JWTAuthConnectionManager.postToken() 方法,然后通过在其中执行代码而失败。我试过添加这些行:
@RunWith(PowerMockRunner.class)
@PrepareForTest(JWTAuthConnectionManager.class)
对上述两个类(当然我不能在Runner 类中使用RunWith,因为它已经有一个RunWith 注释),但这并没有改变任何东西。
如何让 PowerMock 在 Cucumber 中工作?
【问题讨论】:
标签: java unit-testing junit cucumber powermock