Rest SpringBoot测试用例没有按照预期的方式工作

Rest SpringBoot测试用例没有按照预期的方式工作,rest,spring-boot,jpa,Rest,Spring Boot,Jpa,我一直在开发一个基本的Spring引导应用程序来构建RESTAPI。我已经学会了编写API,现在我正在尝试为它们编写单元测试。我已经为get API编写了一个单元测试,为post API编写了一个单元测试。post-API测试似乎运行良好,但get-API测试失败。我不知道为什么。我不确定get测试是否在post之前运行,因此没有可用的内容,所以它失败了 我尝试过改变测试的编写顺序,以便看到执行顺序的改变,但没有改变 @RunWith(SpringRunner.class) @WebMvcTes

我一直在开发一个基本的Spring引导应用程序来构建RESTAPI。我已经学会了编写API,现在我正在尝试为它们编写单元测试。我已经为get API编写了一个单元测试,为post API编写了一个单元测试。post-API测试似乎运行良好,但get-API测试失败。我不知道为什么。我不确定get测试是否在post之前运行,因此没有可用的内容,所以它失败了

我尝试过改变测试的编写顺序,以便看到执行顺序的改变,但没有改变

@RunWith(SpringRunner.class)
@WebMvcTest(value = ProjectRestController.class)
public class ProjectControllerTest 
{

    private String baseURL = "http://localhost:8080/";

    private String expectedResult = "{id:0, name:\"Testing Course 0\", description: \"This is a test for course 0\"}";

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private ProjectService projectService;

    Project mockProject = new Project(0, "Testing Course 0", "This is a test for course 0");

    @Test
    public void addProject() throws Exception
    {
        mockMvc.perform(MockMvcRequestBuilders.post(baseURL+"/projects")
        .content(asJsonString(new Project(0, "Test 0", "Testing Project 0")))
        .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk());
    }

    //This test does not seem to work. Returns 404
        @Test
        public void getProject() throws Exception
        {
        mockMvc.perform(MockMvcRequestBuilders.get(baseURL+"/projects/0")
                .accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
    }

    public static String asJsonString(final Object obj) 
    {
        try 
        {
            return new ObjectMapper().writeValueAsString(obj);
        } catch (Exception e) 
        {
            throw new RuntimeException(e);
        }
    }
}

我希望GET的状态为200,因为帖子运行正常,但是GET返回404。

问题在于您有一个基本
URL
,您不应该发送基本
URL
,只通过
/project/0

@james抱歉,我更新了答案,删除了基本URL。这应该行得通。很抱歉,它还是不行。这是日志的复制粘贴,也许这能帮上点忙。MockHttpServletRequest:HTTP方法=获取请求URI=/projects/0参数={}头=[Accept:“application/json”]Body=会话属性={}MockHttpServletResponse:Status=404错误消息=空头=[]Content type=null Body=Forwarded URL=null Redirected URL=null Cookies=[]确保您拥有这些端点,您是否在邮递员上进行了测试?请再次查看我的答案@jamesI在邮递员身上测试了两个端点,它们都有效。我尝试从get和post()方法中删除baseURL,但它们仍然无法解决我的问题:(