我需要模拟私有方法并且应该返回 true。在 ServiceImpl-execute() 中,我的请求将转到 else { },它会调用“eventRequest()”。它是一个私有的布尔 eventRequest(),所以每当 evenRequest() 调用我应该返回 true。谁能帮帮我
ServiceImplTest.java
@RunWith(PowerMockRunner.class)
@PrepareForTest({ServiceImpl.class})
public class ServiceImplTest {
@Test
public void testExecute() {
Response response = serviceImpl.execute(request);
Assert.assertNotNull(pushResponse);
Assert.assertEquals(true, pushResponse.isIsSuccess());
}
}
ServiceImpl.java
public class ServiceImpl {
public Response execute(Request request) {
Response response = null;
boolean isSuccess;
if (returnMockResponse(request, notifyRqst)) {
isSuccess = true;
} else {
isSuccess = eventRequest(notifyXmlRqst);
}
response = ResponseBuilder.createResponse(isSuccess);
return response;
}
// Need to mock below private method and should return true.
private boolean eventRequest(String request) throws Exception {
return eventNotifyResponse.isIsSuccess();
}
}
ResponseBuilder.java
public class ResponseBuilder {
public Response createResponse(boolean result) {
Response response = new Response();
response.setIsSuccess(result);
return response;
}
}