Unable to mock Rest Template static object in my postInvoiceByRestService method I have tried lots of ways including Power Mockito Mockito spy but did not work for me What strategy need to use mock static Rest Template object?
Java Class
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);
}
}
Unable to mock RestTemplate static object in my postInvoiceByRestService method ?
I tried multiple ways using PowerMockito & Mockito , spy but did not get output. PLs let me know what is wrong I am doing.