将 PowerMock 与黄瓜一起使用
Using PowerMock with Cucumber
我编写了一个 JUnit 测试,它使用 Mockito 和 PowerMock 来模拟一些 classes。我正在尝试将其转换为 Cucumber 测试,但静态 PowerMock 功能不起作用。
两个相关 Cucumber 的摘录 classes:
亚军
@RunWith(Cucumber.class)
public class JWTValidatorBDDTest {
}
步数Class
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)
以上两个 classes(当然我不能在 Runner
class 中使用 RunWith
因为它已经有一个 RunWith
注释),但这并没有改变任何东西。
如何让 PowerMock 在 Cucumber 中工作?
你不能使用 PowerMockRunner
因为测试只能有一个跑步者(在你的情况下 Cucumber
)。但是 AFAIK 你可以使用 PowerMockRule 而不是 PowerMockRunner
.
似乎现在可以使用@PowerMockRunnerDelegate 注释。我使用 @RunWith(PowerMockRunner.class) 和 @PowerMockRunnerDelegate(Cucumber.class) 并且它正在工作。从这里获取建议:https://medium.com/@WZNote/how-to-make-spock-and-powermock-work-together-a1889e9c5692
Since version 1.6.0 PowerMock has support for delegating the test execution to another JUnit runner without using a JUnit Rule. This leaves the actual test-execution to another runner of your choice. For example tests can delegate to “SpringJUnit4ClassRunner”, “Parameterized” or the “Enclosed” runner.
还有使用@Rule的选项:PowerMockRule rule = new PowerMockRule();而不是 @RunWith(PowerMockRunner.class) (所以 Runner 可以是别的东西) - 但 Stefan Birkner 的评论表明 Cucumber runner 应该支持使用它的规则,我不确定它是否(现在)。
希望对大家有所帮助。
我编写了一个 JUnit 测试,它使用 Mockito 和 PowerMock 来模拟一些 classes。我正在尝试将其转换为 Cucumber 测试,但静态 PowerMock 功能不起作用。
两个相关 Cucumber 的摘录 classes:
亚军
@RunWith(Cucumber.class)
public class JWTValidatorBDDTest {
}
步数Class
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)
以上两个 classes(当然我不能在 Runner
class 中使用 RunWith
因为它已经有一个 RunWith
注释),但这并没有改变任何东西。
如何让 PowerMock 在 Cucumber 中工作?
你不能使用 PowerMockRunner
因为测试只能有一个跑步者(在你的情况下 Cucumber
)。但是 AFAIK 你可以使用 PowerMockRule 而不是 PowerMockRunner
.
似乎现在可以使用@PowerMockRunnerDelegate 注释。我使用 @RunWith(PowerMockRunner.class) 和 @PowerMockRunnerDelegate(Cucumber.class) 并且它正在工作。从这里获取建议:https://medium.com/@WZNote/how-to-make-spock-and-powermock-work-together-a1889e9c5692
Since version 1.6.0 PowerMock has support for delegating the test execution to another JUnit runner without using a JUnit Rule. This leaves the actual test-execution to another runner of your choice. For example tests can delegate to “SpringJUnit4ClassRunner”, “Parameterized” or the “Enclosed” runner.
还有使用@Rule的选项:PowerMockRule rule = new PowerMockRule();而不是 @RunWith(PowerMockRunner.class) (所以 Runner 可以是别的东西) - 但 Stefan Birkner 的评论表明 Cucumber runner 应该支持使用它的规则,我不确定它是否(现在)。
希望对大家有所帮助。