How to write Unit test for REST Controller Endpoint without mocking Service class in Spring-boot
<p>I have to write Unit test for Web Service in Spring-boot which should perform testing from Controller and also flow into Service class.
I am using @Mock annotation for Service class and @InjectMock for Controller, therefore the application flow never goes inside Service class,( it returns a null).</p>

<p>Without the @Mock annotation on Service class, I receive a null point exception.</p>

<p>I don't want to Mock the Service class,I want to test the Service class as well with this same test unit.</p>

<p>This is my REST Controller.</p>

<pre><code> @RestController
 public class AddressController {
 @Autowired
 private GetAddressService getAddressService;

@RequestMapping(value=ApplicationConstant.ADDRESS_URI,method=RequestMethod.G    ET,produces = { ApplicationConstant.APP_RESP_TYPE })
public @ResponseBody SampleResponse getAddress(
        @RequestParam(value="id",required=true) String hcId,
        @RequestParam(value="mbrNbrbr",required=true) String mbrNbr,
        ){

    System.out.println("Request ID in Controller :"+Id);
    String responseid = getAddressService.getAddress(Id);

    SampleResponse smapleResp = new SampleResponse();
    System.out.println("Response ID in Controller :"+Id);
    smapleResp.setResponseid(responseid);
    return smapleResp;
</code></pre>

<p>This is my Service Class</p>

<pre><code> @Service
public class GetAddressServiceImpl implements GetAddressService {
@Override
public String getAddress(String id) {

    System.out.println("ID in Service : " + id);
    return id;
}
}
</code></pre>

<p>I have an Abstract test class which extends to Abstract Controller test class and Finally my Address Controller Test class which has the @Test methods.</p>

<p>Abstarct Test class:</p>

<pre><code> @RunWith(SpringJUnit4ClassRunner.class)
 @SpringApplicationConfiguration(classes = Application.class)
 public abstract class AbstractTest {
  }
</code></pre>

<p>Abstarct Controller Test class:</p>

<pre><code>@WebAppConfiguration 
public abstract class AbstractControllerTest extends AbstractTest {

//mockmvc stimulates Http interactions
protected MockMvc mockMvc; 

@Autowired
protected WebApplicationContext webApplicationContext;


protected void setUp(){
    mockMvc =   MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}


protected void setUp(AddressController addressController){
    mockMvc = MockMvcBuilders.standaloneSetup(addressController).build();
}
}
</code></pre>

<p>Finally My Address Test class:</p>

<pre><code>   @TestExecutionListeners
    public class AddressMockControllerTest extends AbstractControllerTest  {

    @Mock
    private GetAddressService getAddressService;

    @InjectMocks
    private AddressController addressController;

    private String MockURI ="/v1/legacy/member/Contact/Address? id=55555&amp;mbrNbr=20&amp;id=ABC&amp;productid=1FGQ&amp;sourceid=STAR&amp;addressuid=adasdsa";

    @Before
    public void setUp(){
        MockitoAnnotations.initMocks(this);
        setUp(addressController);       
        }


    @Test
    public void test(){
        System.out.println("In Unit test");

    }

    @Test
    public void testgetAddress() throws Exception {


     System.out.println("In Unit test");

     MvcResult result =  this.mockMvc.perform(
                   MockMvcRequestBuilders.get(MockURI)
                  .accept(MediaType.APPLICATION_JSON))
                  .andReturn(); 


     String content = result.getResponse().getContentAsString();

        System.out.println("In Unit test and content is " + content);   
    }
</code></pre>

<p>Boot Main Class:</p>

<pre><code> public class Application extends SpringBootServletInitializer{

public static void main(String[] args) {
    SpringApplication.run(ApplicationInitializer.class, args);
}

@Override
protected final SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
    return application.sources(ApplicationInitializer.class);
}
</code></pre>

<p>Application Initializer:</p>

<pre><code>    @Configuration

@EnableAutoConfiguration
@ComponentScan(value={"com.ABC"})
@EnableConfigurationProperties
public class ApplicationInitializer {

    @Bean
    @ConditionalOnMissingBean(RequestContextListener.class)
    public RequestContextListener requestContextListener() {
        return new RequestContextListener();
    }

    @Bean
    public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
        ServletRegistrationBean registration = new ServletRegistrationBean(
                dispatcherServlet);
        registration.addUrlMappings("/");
        return registration;
    }

    @Bean
    public ServletRegistrationBean dynamicLogbackLevelServletRegistration() {

        return new ServletRegistrationBean(new DynamicLogbackLevelServlet(),"/loglevel");

    }


    @Order(Ordered.HIGHEST_PRECEDENCE)
    public FilterRegistrationBean contextFilterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        RequestFilter contextFilter = new RequestFilter();
        registrationBean.setFilter(contextFilter);
        registrationBean.setOrder(1);
        return registrationBean;
    }


    @Bean
    public ApplicationContextProvider applicationContextProvider(){
        return new ApplicationContextProvider();
    }

}
</code></pre>