Java 在rest-assured测试中使用PowerMock模拟静态类
有没有办法将PowerMock与rest-assured结合使用,因为当我试图用rest-assured测试restfulapi时 我想模拟一个静态调用 操作规程:Java 在rest-assured测试中使用PowerMock模拟静态类,java,junit4,powermock,rest-assured,Java,Junit4,Powermock,Rest Assured,有没有办法将PowerMock与rest-assured结合使用,因为当我试图用rest-assured测试restfulapi时 我想模拟一个静态调用 操作规程: @POST @Produces("application/json") @Consumes(MediaType.APPLICATION_JSON) public Response createEntity(@Context HttpHeaders hh, String body) { . . . String s
@POST
@Produces("application/json")
@Consumes(MediaType.APPLICATION_JSON)
public Response createEntity(@Context HttpHeaders hh, String body) {
. . .
String sec = MDI.check(Some_string, ..., ...);
if (sec == null) throw ...
return Response....
}
以及测试:
@RunWith(PowerMockRunner.class)
@PrepareForTest(MDI.class)
public class createSOTest {
@Test
public void testStatic() {
mockStatic(MDI.class);
expect(MDI.check(Some_string, ..., ...).andReturn(Some_String);
replay(MDI.class)
given().
contentType(ContentType.JSON).
header("SomeHeader", "something").
body(root).
when().
post("/").
then().
statusCode(...);
}
}
问题是,当测试尝试运行rest-assured
代码(given()…
)时,我获得了一个异常:
这似乎是PowerMockRunner.class的问题 这看起来不像powermock问题。通过临时删除测试代码中的静态依赖项来缩小测试范围-将其剔除,使其返回任何内容。请在没有任何嘲弄的情况下重试测试。您可能会遇到相同的错误,在这种情况下,您可以调查该错误。我遇到了相同的问题,并在以下位置找到了解决方案: 这与从上游类加载器加载SSLCOntext相关,这是powermock在运行测试时的一个类加载器 解决方案:在测试类顶部使用@PowerMockIgnore注释: @PowerMockIgnore(“javax.net.ssl.*”)
奥斯卡的回答很有帮助。我必须排除多个名称空间才能让它工作。 我最终通过了以下几点:
@PowerMockIgnore({"javax.management.*", "org.apache.http.conn.ssl.*", "com.amazonaws.http.conn.ssl.*", "javax.net.ssl.*"})
嗨@ashley frieze,没有任何嘲弄,我没有错误。但是我想模拟静态方法。。。
@PowerMockIgnore({"javax.management.*", "org.apache.http.conn.ssl.*", "com.amazonaws.http.conn.ssl.*", "javax.net.ssl.*"})