【问题标题】:How to test DELETE method in Spring boot using Mockito and JUnit如何使用 Mockito 和 JUnit 在 Spring Boot 中测试 DELETE 方法
【发布时间】:2020-01-20 13:09:16
【问题描述】:

在 Spring Boot 框架中,我发现使用 JUnit 和 Mockito 进行控制器单元测试存在困难。我想测试这个方法。如何测试 DELETE 请求方法:

// 删除应用程序 控制器类

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

// 删除应用程序 服务类

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

// 删除应用程序 道类

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

提前谢谢你。

【问题讨论】:

  • 如何在 Spring Boot 应用程序上测试任何 http 方法?您使用MockMvcRestAssured - 您是否向它发送HTTP 请求?或者您是否尝试直接调用该方法以确保它调用applicationService.deleteById(application.getId());
  • 我正在使用 mockito 和 Junit 进行单元测试,并且我正在使用 MockMvc 来测试我的控制器方法,但是当我尝试调用该方法时,我发现此删除方法存在困难applicationService.deleteById(application.getId());
  • 你能发布你的控制器类的测试类吗
  • 这里,我的测试控制器类@Test public void deleteApplication() throws Exception { Mockito.when(applicationService.removeById(10001L)).thenReturn("SUCCESS"); mockMvc.perform(MockMvcRequestBuilders.delete("/applications")) .andExpect(status().isOk()); }

标签: java spring-boot mockito junit4


【解决方案1】:

一段时间后,我能够找到我的问题的解决方案,即,

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());
    }

【讨论】:

    猜你喜欢
    • 2018-12-23
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多