Mocking resttemplate.exchange()不起作用。restTemplate.exchange()mocking的响应在BDS适配器类中给出null值。我的测试用例由于BDSAdapter类中的空指针异常而失败。(response.getStatusCodeValue()提供空指针异常。.mockito提示)
未使用...->at com..policydetails_adapters.bdsadaptertest.getInsuranceHoldings(bdsadaptertest.java:56)[MockitoHint]...args好吗?->在com.policydetails_adapters.bdsadapter.fetchinsurancedetails(bdsadapter.java:77)
Below are my classes.
@RunWith(MockitoJUnitRunner.class)
public class BDSAdapterTest {
@InjectMocks
private BDSAdapter bdsAdapter;
@Mock
private BDSFetchInsuranceDetailsRequest bdsFetchInsuranceDetailsRequest;
@Mock
private RestTemplate restTemplate;
@Mock
Environment env;
@Test
public void getInsuranceHoldings() throws InsuranceHoldingsException {
Mockito.when(restTemplate.exchange(
ArgumentMatchers.anyString(),
ArgumentMatchers.any(HttpMethod.class),
ArgumentMatchers.any(),
ArgumentMatchers.<Class<BDSFetchInsuranceDetailsResponse>>any()))
.thenReturn(sampleBDSCustomerInsuranceHoldings());
BDSFetchInsuranceDetailsResponse bdsFetchInsuranceDetailsResponse = bdsAdapter.fetchInsuranceDetails("MBSG", "S6564318I", "1234", "007");
assertNotNull("response is not null", bdsFetchInsuranceDetailsResponse);
}
public static ResponseEntity<BDSFetchInsuranceDetailsResponse> sampleBDSCustomerInsuranceHoldings() {
BDSFetchInsuranceDetailsResponse bdsResponse = new BDSFetchInsuranceDetailsResponse();
Header header = new Header();
header.setChannelId("MBSG");
header.setMsgId("4aBE50ZrQtjVuXfTyALJ");
bdsResponse.setHeader(header);
ResponseEntity<BDSFetchInsuranceDetailsResponse> response = new ResponseEntity<BDSFetchInsuranceDetailsResponse>(bdsResponse, HttpStatus.ACCEPTED);
return response;
}
}
@组件公共类BDSAdapter{
@Autowired
RestTemplate restTemplate;
@Autowired
BDSFetchInsuranceDetailsRequest bdsFetchInsuranceDetailsRequest;
@Autowired
Environment env;
public BDSFetchInsuranceDetailsResponse fetchInsuranceDetails(String channelId, String customerId,
String insurerCode, String policyNumber) throws InsuranceHoldingsException {
BDSFetchInsuranceDetailsResponse bdsFetchInsuranceDetailsResponse = null;
try {
logger.info("Inside BDSAdapter::fetchInsuranceDetails");
Header header = new Header();
header.setMsgId(RandomStringUtils.randomAlphanumeric(20));
header.setChannelId(channelId);
bdsFetchInsuranceDetailsRequest.setHeader(header);
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Accept", MediaType.APPLICATION_JSON_VALUE);
HttpEntity<BDSFetchInsuranceDetailsRequest> requestEntity = new HttpEntity<>(bdsFetchInsuranceDetailsRequest, requestHeaders);
ResponseEntity<BDSFetchInsuranceDetailsResponse> response = restTemplate.exchange(env.getProperty("bds_fetch_insurance_details_url"),HttpMethod.POST, requestEntity, BDSFetchInsuranceDetailsResponse.class);
if(response.getStatusCodeValue() == 204) {
throw new InsuranceHoldingsException(response.getStatusCode().toString(), "No Content");
}
bdsFetchInsuranceDetailsResponse = response.getBody();
} catch (Exception e) {
e.printStackTrace();
}
}
return bdsFetchInsuranceDetailsResponse;
}
}
也许,因为BDSFetChinSuranceDetailsResponse
的mock将返回的不是BDSFetChinSuranceDetailsResponse
的类,而是一些mockclass
我是新的mockito和jUnit我不明白为什么我得到错误和如何模仿这个服务类方法 请帮我解决这个问题。是否需要模拟可分页和切片对象。为什么在嘲笑PartnerEventRepository之后,我也会说 “需要但未调用:partnerEventRepository.findAll(Cassandra页面请求[编号:0,大小5,排序:未排序,分页状态:null]) 莫基托
无法找出正确的方法来使用匹配器来识别我要处理的exchange方法的重载。我正在打的电话:
我正在使用Powermock为Jersey web服务构建测试用例,并试图模拟数据库函数调用,特别是PUT和POST调用。然而,我在这方面遇到了一些问题。 下面是其中一个web服务调用的样子: 以及我的测试用例当前的样子: 当运行时,这给了我一个断言错误: 意外的方法调用WebService。runUpdateQuery(“exec spInsertApplication,[“测试服务名称”,空,
上面还有第二个问题。当我在Expects块中定义mock类时(如上),似乎只调用了构造函数,而不是,因此没有正确初始化对象。我通过将它移到方法中并在那里实例化该类来解决这个问题。看起来是这样的: 因此,这似乎得到了要调用的正确构造函数,但似乎还在调用。有什么见解吗?
我试图在测试中模拟一个调用,但我得到了一个错误,因为它调用了真正的方法,而不是模拟它。 这是我的方法 } 这是我的测试课 测试实际上调用了受保护的方法config Setter,并在设置代理时失败。帮助我理解我在这里做错了什么。