S
S
sur-pavel2020-07-09 11:40:42
Spring
sur-pavel, 2020-07-09 11:40:42

How to fix a bug in @SpringBootTest?

Spring Boot h2.
There is a class:

package ru.surpavel.bugtrackingsystem;

imports...

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { BugTrackingSystemSpringBootApplication.class, 
        ProjectRepository.class, UserRepository.class,
        TaskRepository.class, ProjectController.class })
@AutoConfigureMockMvc
public class ProjectRestControllerIntegrationTest {

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private ProjectRepository projectRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private TaskRepository taskRepository;

    @Autowired
    private ProjectController projectController;
    
    @Test
    public void createProject() throws Exception {
        String title = "third";
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = gson.toJson(new Project(title));
        mockMvc.perform(post("/projects").content(json).contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath(title).exists());
    }
}

with the createProject method which throws an error:
java.lang.AssertionError: No value at JSON path "third"
  at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:295)
  at org.springframework.test.util.JsonPathExpectationsHelper.assertExistsAndReturn(JsonPathExpectationsHelper.java:319)
  at org.springframework.test.util.JsonPathExpectationsHelper.exists(JsonPathExpectationsHelper.java:183)
  at org.springframework.test.web.servlet.result.JsonPathResultMatchers.lambda$exists$3(JsonPathResultMatchers.java:123)
  at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:196)
  at ru.surpavel.bugtrackingsystem.ProjectRestControllerIntegrationTest.createProject(ProjectRestControllerIntegrationTest.java:86)
  ...


log:
MockHttpServletRequest:
      HTTP Method = POST
      Request URI = /projects
       Parameters = {}
          Headers = [Content-Type:"application/json;charset=UTF-8", Accept:"application/json", Content-Length:"22"]
             Body = {
  "title": "third"
}
    Session Attrs = {}

Handler:
             Type = ru.surpavel.bugtrackingsystem.controller.ProjectController
           Method = ru.surpavel.bugtrackingsystem.controller.ProjectController#createProject(Project)

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = [Content-Type:"application/json"]
     Content type = application/json
             Body = {"id":3,"title":null}
    Forwarded URL = null
   Redirected URL = null
          Cookies = []


The usual post-request to the running SpringBootApp passes without problems. All data is saved.
RestContoller has a method:
@PostMapping("/projects")
    public Project createProject(@Valid Project project) {
        return projectRepository.save(project);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BorLaze, 2020-07-18
@BorLaze

Because you don't have to check.

.andExpect(MockMvcResultMatchers.jsonPath(title).exists());

a
.andExpect(MockMvcResultMatchers.jsonPath("title").exists());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question