gpt4 book ai didi

java - 模拟 private void 方法给出 AssertionError

转载 作者:太空宇宙 更新时间:2023-11-04 06:31:55 25 4
gpt4 key购买 nike

我正在使用 Powermock 为 Jersey Web 服务构建测试用例,并尝试模拟数据库函数调用,特别是 PUT 和 POST 调用。但是,我在使其正常工作时遇到问题。

以下是其中一个 Web 服务调用的样子:

@Path("/v1.0.0")
public class WebService {

@POST
@Path("application")
@Consumes(MediaType.APPLICATION_JSON)
public Response createApplication(@QueryParam("callback") String callbackFunction, String body)
throws NamingException, SQLException, IllegalStateException, CacheException, UnknownHostException,
IOException {
String query = "exec spInsertApplication ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?";
String[] params = new String[16];
JSONObject json = (JSONObject) JSONValue.parse(body);
params[0] = json.get("name").toString();
params[4] = json.get("software").toString();
params[14] = json.get("customerid").toString();
//Null checks for other params

runUpdateQuery(query, params);

return generateResponse(callbackFunction, null);
}

private void runUpdateQuery(String query, String[] queryParameters) {
//Handles running DB query
}
}

我的测试用例目前的样子:

@RunWith(PowerMockRunner.class)
@PrepareForTest(WebService.class)
@PowerMockIgnore( {"javax.management.*"})
public class TestRestWebService {

@Test
public void test_createApplication_returns_http_success() throws Exception {
String query = "exec spInsertApplication ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?";
String[] params = new String[16];
params[0] = "test_app_name";
params[4] = "test_software";
params[14] = "23";

WebService tested = createPartialMockAndInvokeDefaultConstructor(WebService.class, "runUpdateQuery");
expectPrivate(tested, "runUpdateQuery", query, params).andAnswer(
new IAnswer<Void>() {
@Override
public Void answer() throws Throwable {
return null;
}
});
replay(tested);

String JSONString = "{\"name\":\"test_service_name\",\"software\":\"test_software\",\"customerid\":\"23\"}";
Response output = tested.createApplication("CALLBACK", JSONString);
verify(tested);
assertTrue(output.getStatus() == 200);
}
}

运行时,这会给我一个断言错误:

Unexpected method call WebService.runUpdateQuery("exec spInsertApplication ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?", ["test_service_name", null, null, null, "test_software", null, null, null, null, null, null, null, null, null, "23", null]):

WebService.runUpdateQuery("exec spInsertApplication ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?", ["test_service_name", null, null, null, "test_software", null, null, null, null, null, null, null, null, null, "23", null]): expected: 1, actual: 0

经过进一步挖掘,我发现原因很可能与 PowerMock 比较字符串数组的方式有关。因此,我还尝试采用更通用的方法,因为该函数无论如何都会返回 void,通过使用 EasyMock.anyString() 和 EasyMock.isA(String[].class) 代替我的两个参数,但这会导致 NullPointerException 。以下是该堆栈跟踪的前几行:

java.lang.NullPointerException
at java.lang.Class.isAssignableFrom(Native Method)
at org.powermock.reflect.internal.WhiteboxImpl.checkIfParameterTypesAreSame(WhiteboxImpl.java:2432)
at org.powermock.reflect.internal.WhiteboxImpl.getMethods(WhiteboxImpl.java:1934)
at org.powermock.reflect.internal.WhiteboxImpl.getBestMethodCandidate(WhiteboxImpl.java:1025)
at org.powermock.reflect.internal.WhiteboxImpl.findMethodOrThrowException(WhiteboxImpl.java:948)

如何正确模拟此私有(private) void 方法以避免测试期间的数据库调用?

最佳答案

在深入研究此测试和文档几次后,我发现 expectPrivate 调用找不到我的方法。所以我发现我可以指定该函数,如下:

expectPrivate(tested, WebService.class.getDeclaredMethod("runUpdateQuery", String.class, String[].class),
EasyMock.anyString(), EasyMock.aryEq(params)).andAnswer(new IAnswer<Void>() {
@Override
public Void answer() throws Throwable {
return null;
}
});

这还允许我在使用泛型类型定义函数时与函数调用的数组进行比较,就像我最初想要的那样。

关于java - 模拟 private void 方法给出 AssertionError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26025773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com