无法在我的postInvoiceByRestService方法中模拟Rest Template静态对象我已经尝试了很多方法,包括Power Mockito Mockito spy,但对我不起作用什么策略需要使用模拟静态Rest Template对象?
Java类
public class PeopleSoftInvoiceRestPublisher {
private static final String UPDATED_BY = "DFIINVP";
private static final String TAX_ACCOUNT = "241900";
private static URL serverURL;
private static RestTemplate restTemplate = new RestTemplate();
public static void main(String[] args) throws DfiException, SQLException {
getWLConnection();
PeopleSoftInvoiceRestPublisher peopleSoftInvoiceRestPublisher = new PeopleSoftInvoiceRestPublisher();
peopleSoftInvoiceRestPublisher.publishInvoices();
}
public void postInvoiceByRestService(PeopleSoftRecordBuilder builder) {
HttpHeaders header = new HttpHeaders();
header.add("Authorization", "Basic " + Base64.getEncoder().encodeToString(
(DfiResources.getResource("PS_USER") + ":" + DfiResources.getResource("PS_PASSWORD")).getBytes()));
PostInvoiceRequestDTO postInvoiceRequestDTO = getPostInvoiceRequestDTO(builder);
HttpEntity<PostInvoiceRequestDTO> httpEntity = new HttpEntity<PostInvoiceRequestDTO>(postInvoiceRequestDTO,
header);
try {
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
DfiLog.info("Request JSON Payload :: " + ow.writeValueAsString(postInvoiceRequestDTO));
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
}
DfiLog.info("Request Payload :: " + postInvoiceRequestDTO.toString());
String urlTemplate = UriComponentsBuilder.fromHttpUrl(DfiResources.getResource("PS_POST_URL")).toUriString();
// .queryParam("From_date", getYesterdayDate()).queryParam("To_date",
// modifiedDate).toUriString();
DfiLog.info("Request Rest endpoint :: " + urlTemplate);
ResponseEntity<String> invoiceResponseEntity = restTemplate.exchange(urlTemplate, HttpMethod.POST, httpEntity,String.class);
DfiLog.info("Response Entity :: " + invoiceResponseEntity.toString());
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest({ DfiResources.class, DfiLog.class, UriComponentsBuilder.class })
//@PrepareEverythingForTest
@PowerMockIgnore("javax.swing.*")
public class PeopleSoftInvoiceRestPublisherTest {
@InjectMocks
private PeopleSoftInvoiceRestPublisher peopleSoftInvoiceRestPublisher;
/*@Mock
RestTemplate restTemplate;
*/
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testCase() throws Exception {
PeopleSoftRecordBuilder peopleSoftRecordBuilder = new PeopleSoftRecordBuilder();
PowerMockito.mockStatic(DfiResources.class);
when(DfiResources.getResource("PS_USER")).thenReturn("USER");
when(DfiResources.getResource("PS_PASSWORD")).thenReturn("PASSWORD");
when(DfiResources.getResource(Mockito.anyString())).thenReturn("PASSWORD");
when(DfiResources.getResource("PS_POST_URL")).thenReturn("https://example.com/hotels/42?filter={value}");
PowerMockito.mock(DfiLog.class);
peopleSoftRecordBuilder.invoiceAmt(new BigDecimal(10.0));
peopleSoftRecordBuilder.invoiceNum("1234");
peopleSoftRecordBuilder.supplierNumber("567");
peopleSoftRecordBuilder.setQtyDelivered(new BigDecimal(210.0));
PowerMockito.mock(UriComponentsBuilder.class);
//PowerMockito.mockStatic(RestTemplate.class);
//RestTemplate t1=new RestTemplate();
ResponseEntity<String> str=new ResponseEntity<String>("dsffdsd",HttpStatus.ACCEPTED);
RestTemplate restTemplate= Mockito.mock(RestTemplate.class);
when(restTemplate.exchange(Matchers.any(String.class),Matchers.any(HttpMethod.class),Matchers.<HttpEntity<?>> any() ,Matchers.<Class<?>>any())
).thenReturn(null);
//doNothing().when(restTemplate).exchange(Matchers.any(String.class),Matchers.any(HttpMethod.class),Matchers.<HttpEntity<?>> any() ,Matchers.<Class<?>>any());
peopleSoftInvoiceRestPublisher.postInvoiceByRestService(peopleSoftRecordBuilder);
}
}
无法在我的postInvoiceByRestService方法中模拟RestTemplate静态对象?
我尝试了多种方法使用PowerMockito & Mockito,间谍,但没有得到输出。请让我知道我做错了什么。
1条答案
按热度按时间ogq8wdun1#
Power Mock允许你访问mock静态方法、构造函数等,这意味着你的代码没有遵循最佳编程原则。
Power Mock应该用于遗留应用程序中,在这些应用程序中,您无法更改已提供给您的代码。通常,此类代码没有单元/集成测试,即使是很小的更改也可能导致应用程序中的错误。因此请小心。
我认为你在嘲笑www.example.com()时给出的值不正确restTemplate.exchange。