
[英]JUnit & Mockito - thenReturn is returning null when using on WebServiceTemplate
[英]Method is returning null even after using when/thenReturn
正如标题所述,即使使用我正在尝试测试的 class 的模拟实例,我对我所做的呼叫的响应,我也无法断言响应不是 null。 即使我使用 when/thenReturn,它也无法正常工作。
我正在写的测试用例看起来像这样
@Mock Client clientMock;
@Mock DocumentType documentMock;
@Mock LambdaLogger logger;
@InjectMocks Handler handler; // I have annotated the handler class with @ExtendWith(MockitoExtension.class and @RunWith(PowerMockRunner.class)
@Test
public void handleRequest() {
Handler handler = new Handler();
EventRequest request = buildEventRequest();
String property1 = "abcd";
String property2 = "1234";
PowerMockito.whenNew(Client.class).withAnyArguments().thenReturn(clientMock);
PowerMockito.when(clientMock.getToken(logger, "sampleId").thenReturn("sampleToken");
PowerMockito.when(clientMock.getDocument(property1, property2, logger).thenReturn(document);
Object result = handler.handleRequest(request, context);
assertNotNull(result);
}
我正在尝试测试的方法:
public class Handler{
public Object handleRequest(Object object, Context context) {
DocumentType document = getTheDocument(object.getProperties()); //calls a private method within the same class
if (document != null) {
document.setNewProperties(abc);
}
return document;
}
private DocumentType getTheDocument(String property1, String property2, LambdaLogger.class) {
Client client = new Client(); //trying to mock this client to get a document
DocumentType document = new DocumentType();
document = client.getDocument(property1, property2);
return document;
}
}
客户端 class 是这样的
public class Client{
public Object getDocument(String property1, String property2, LambdaLogger logger) {
String token = getToken(logger, property1);
// Makes post call to get CloseableHttpResponse, uses this to call a static method to get the document
Document document = AnotherClass.getDocument(closeableHttpResponse);
// Creates an Unmarshaller from JAXBContext, for the DocumentType.class, get's data as inputstream
document = (DocumentType) unmarshaller.unmarshall(inputstream);
return document;
}
public String getToken(LambdaLogger logger, String id) {
AWSSecretsManager client = AWSSecretsManagerClientBuilder.standard().build();
GetSecretValueRequest request = new GetSecretValueRequest().withSecretId(id);
GetSecretValueResult result = null;
result = client.getSecretValue(request);
response = result.getSecretString();
return response;
}
}
我想知道我是否甚至需要模拟 getToken 之类的内部方法,以及为什么when(clientMock.getDocument(property1, property2, logger).thenReturn(document);
不起作用。我使用 PowerMockito 的 whenNew 作为新的 object类型的 Client 是在私有方法中创建的,但这似乎不能正确地模拟客户端。
我是否需要使用 ReflectionUtils 来调用处理程序 class 中的 Private 方法并执行类似的操作?
Method getTheDocument = Handler.class.getDeclaredMethod("getTheDocument", String.class, String.class, LambdaLogger.class)
getTheDocument.setAccessible(true);
getTheDocument.invoke(handler, property1, property2, logger); //want to stub a return value for getDocument but not sure how to, so left it as it is here
unmarshaller.unmarshall(inputstream); 是数据流中返回 null 的点,如果有帮助的话。 我尝试了很多东西,并模拟了内部方法以返回模拟值,但此时没有任何效果。 如果我在这里遗漏了什么,或者您有任何建议,请告诉我。 (PS我无法修改原代码,只能添加测试用例)
首先,@RunWith 是一个 Junit4 注解,而不是 Junit5 与@ExtendWith 相反。 因此,您的 Powermock 配置将无法在 JUnit5 上下文中工作。
我想知道我是否需要模拟像 getToken 这样的内部方法
实际上,您不需要模拟getToken
因为您已经模拟了调用getToken
的getDocument
。 getToken
将永远无法到达。
您可以查看此线程,了解如何使用 Junit5 和 Powermock 模拟新实例创建: 每当在没有 PowerMockito JUnit5 的情况下创建新实例时进行模拟
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.