I am trying to mock the external call.
ResponseEntity<?> httpResponse = requestGateway.pushNotification(xtifyRequest);
requestGateway is an interface.
public interface RequestGateway
{
ResponseEntity<?> pushNotification(XtifyRequest xtifyRequest);
}
Below is the test method i am trying to do.
@Test
public void test()
{
ResponseEntity<?> r=new ResponseEntity<>(HttpStatus.ACCEPTED);
when(requestGateway.pushNotification(any(XtifyRequest.class))).thenReturn(r);
}
A compilation error is there in the above when statement,saying it as an invalid type.even thougg r is of type ResponseEntity.
Can anyone please help me to solve this issue ?
React + Spring Boot Microservices and Spring Spring Boot provides an easy way to write a Unit Test for Rest Controller file. With the help of SpringJUnit4ClassRunner and MockMvc, we can create a web application context to write Unit Test for Rest Controller file.
You can create a @DataJpaTest and @Autowire your repository into it. For example: @RunWith(SpringRunner. class) @DataJpaTest public class MyJpaTest { @Autowired private ChartRepository chartRepository; @Test public void myTest() { ... } }
You can instead use the type-unsafe method
doReturn(r).when(requestGateway.pushNotification(any(XtifyRequest.class)));
Or you can remove the type info while mocking
ResponseEntity r=new ResponseEntity(HttpStatus.ACCEPTED);
when(requestGateway.pushNotification(any(XtifyRequest.class))).thenReturn(r);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With