在DispatcherServlet中找不到带有URI []的HTTP请求的映射,名称为''

问题描述 投票:0回答:1

我修理了我的测试:

@SpringBootTest
@WebAppConfiguration
@ContextConfiguration(classes = {CoreTestSpringConfiguration.class})
@RunWith(SpringRunner.class)

public class NewStudentTest {


    @Autowired
    protected WebApplicationContext wac;
    protected MockMvc mockMvc;



    @MockBean
    SaveStudentCommand saveStudentCommand;


    @Before
    public void setupMockMvc() {
        mockMvc = MockMvcBuilders
                .webAppContextSetup(wac)
                .build();
    }


    @Test
    public void createStudentTest() throws Exception {

        String jsonLineTest = "[{username:\"532g326\"} ,{name:\"Franco\"} ,{username:\"432ih4j\"} ,{name:\"Ciccio\"} ]";

        Student s1 = new Student();
        Student s2 = new Student();
        List<Student> students = new ArrayList<>();

        ((Student) s1).setUsername("532g326");
        ((Student) s1).setName("Franco");
        ((Student) s2).setUsername("432ih4j");
        ((Student) s2).setName("Ciccio");

        students.add(s1);
        students.add(s2);


        when(saveStudentCommand.execute()).thenReturn(jsonLineTest);


        MvcResult result=mockMvc.perform(post("/credentials/student")
                .accept(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).content(jsonLineTest))
                .andDo(print())
                .andExpect(status().isOk())   
                .andReturn();

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



    }  


}

现在,根据这个新配置,测试失败,显示:java.lang.AssertionError:预期状态:<200>但是<404>。

我想这个答案是由控制台中显示的同一问题引起的:

在DispatcherServlet中找不到带有URI [/ credentials / student]的HTTP请求的映射,名称为'';

我无法理解上面的最后一条消息。无法找到哪个DispatcherServlet?我该如何解决这个问题? Thx提前

junit spring-tool-suite
1个回答
0
投票

有关Spring.io Getting Started pages for testing requests的非常好的教程,如果你刚开始学习Spring,如果你正在进行测试,我会从这里开始。

它会逐步完成为HTTP请求构建整个测试套件的过程,并且可以为您提供帮助。 (他们在指南的中途引入了MockMvc并使用@AutoConfigureMockMvc注释而不是自己进行任何配置)

© www.soinside.com 2019 - 2024. All rights reserved.