状态应为:<200>但在Junit测试中:<404>已关闭

5ktev3wc  于 2022-09-19  发布在  Spring
关注(0)|答案(1)|浏览(156)

**已关闭。**这个问题是not reproducible or was caused by typos。它目前不接受答案。

这个问题是由打字错误或无法再复制的问题引起的。虽然类似的问题可能是D1C1D,但这个问题的解决方式不太可能帮助未来的读者。
7小时前关闭。
Improve this question
我知道这个问题有很多类似的帖子。我试图实现所有这些,但不适用于我。我得到了这个错误java.lang.AssertionError:状态预期:<200>但是:<404>我不知道问题在哪里,所以我需要帮助,如果有人可以帮助我
我的代码:
控制器类:

package com.globusdigital.patientsmanager.controllers;

import com.globusdigital.patientsmanager.model.Department;
import com.globusdigital.patientsmanager.service.interfaces.DepartmentServices;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {
    private final DepartmentServices departmentServicesImp;

    public DepartmentController(DepartmentServices departmentServicesImp) {
        this.departmentServicesImp = departmentServicesImp;
    }

    @PostMapping("/add")
    public ResponseEntity<Department> addDepartment(@RequestBody Department department) {
        Department newDepartment = departmentServicesImp.addDepartment(department);
        return new ResponseEntity<>(newDepartment, HttpStatus.CREATED);
    }
}

控制器测试类:

package com.globusdigital.patientsmanager.controllers;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.BDDMockito.given;

import org.springframework.test.web.servlet.MockMvc;
import com.globusdigital.patientsmanager.model.Department;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import com.globusdigital.patientsmanager.service.DepartmentServiceImp;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(DepartmentController.class)

public class DepartmentControllerTest {
    @MockBean
    private DepartmentServiceImp departmentServiceImp;
    @Autowired
    private MockMvc mvc;
    @Autowired
    private WebApplicationContext context;
    @Before
    public void setUp() {
        mvc = MockMvcBuilders.webAppContextSetup(context).build();
    }
    @Test
    public void notNullMockMvc() {
        assertNotNull(mvc);
    }
    @Test
    public void addDepartment() throws Exception {
        Department department = new Department();
        department.setDepartmentName("department");
        given(departmentServiceImp.addDepartment(department)).willReturn(department);
        ResultActions result = mvc.perform(MockMvcRequestBuilders.get("/add"));
        result.andExpect(status().isCreated());
    }

}
zysjyyx4

zysjyyx41#

问题是,您正在访问的端点不存在,在您拥有的控制器中:@RequestMapping("/department"),然后是@PostMapping("/add")
您的终点将是/department/add

相关问题