Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit Mockito test case for ResponseEntity<?> in spring integration framework

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 ?

like image 646
Jill Avatar asked Aug 18 '16 10:08

Jill


People also ask

How do you write unit test cases for REST API in spring boot?

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.

How do you write a JUnit test case for a repository?

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() { ... } }


1 Answers

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);
like image 177
Nithish Thomas Avatar answered Oct 20 '22 20:10

Nithish Thomas