How to test DELETE method in Spring boot using Mockito and JUnit

14,003

After a while i'm able to find a solution of my question which is,

ApplicationControllerTest.class

package com.spring.addapplication.test.controller;

import static org.mockito.MockitoAnnotations.initMocks;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.spring.addapplication.controller.ApplicationController;
import com.spring.addapplication.model.Application;
import com.spring.addapplication.service.ApplicationService;
import com.spring.addapplication.url.UrlChecker;

@RunWith(SpringJUnit4ClassRunner.class)
public class ApplicationControllerTest {

    @Mock
    ApplicationService applicationService;

    private MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {
        initMocks(this);// this is needed for inititalization of mocks, if you use @Mock 
        ApplicationController controller = new ApplicationController(applicationService,urlChecker);
        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
    }

    @Test
    public void deleteApplication() throws Exception {      
        Mockito.when(applicationService.removeById(10001L)).thenReturn("SUCCESS");
        mockMvc.perform(MockMvcRequestBuilders.delete("/applications", 10001L))
                .andExpect(status().isOk());
    }
Share:
14,003
Ravi Mengar
Author by

Ravi Mengar

Java Software Developer, constant learner, tech enthusiast.

Updated on June 13, 2022

Comments

  • Ravi Mengar
    Ravi Mengar almost 2 years

    In Spring boot framework, I'm finding a difficulty with the controller Unit testing using JUnit and Mockito. I want to test this method. How to test DELETE Request method:

    // delete application Controller class

        @DeleteMapping("/applications")
        public String deleteApplicationByObject(@RequestBody Application application) {
            applicationService.deleteById(application.getId());
            return "Deleted";
        }
    

    // delete application Service class

        @Override
        @Transactional
        public String removeById(Long id) {
            dao.deleteById(id);
            return "SUCCESS";
        }
    

    // delete application Dao class

        @Override
        public void deleteById(Long id) {
            Application application = findById(id);
            em.remove(application);
        }
    

    Thank you in advance.

    • Ashley Frieze
      Ashley Frieze over 4 years
      How do you test ANY http method on your Spring boot app? Do you use MockMvc or RestAssured - do you send it an HTTP request? Or are you trying to call the method directly to make sure it calls applicationService.deleteById(application.getId());?
    • Ravi Mengar
      Ravi Mengar over 4 years
      I'm using a mockito and Junit for unit testing and I'm using MockMvc for testing my controller methods but i'm finding a difficulty with this delete method when i'm trying to call the method applicationService.deleteById(application.getId());
    • Praveen Kumar Mekala
      Praveen Kumar Mekala over 4 years
      can you post your test class for controller class
    • Ravi Mengar
      Ravi Mengar over 4 years
      Here, my test Controller class @Test public void deleteApplication() throws Exception { Mockito.when(applicationService.removeById(10001L)).thenRe‌​turn("SUCCESS"); mockMvc.perform(MockMvcRequestBuilders.delete("/applicatio‌​ns")) .andExpect(status().isOk()); }