An error occurs in the status() function in the test source code below. It would be natural for an error to occur because there is no status() function in the source code. However, I don’t see a separate function being created in the source code in books or on the Internet. What is needed to call the status() function?
package kr.jhansol.blog.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import kr.jhansol.blog.dto.AddArticleRequest;
import kr.jhansol.blog.repository.BlogRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
@SpringBootTest
@AutoConfigureMockMvc
class BlogApiControllerTest {
@Autowired
protected MockMvc mockMvc;
@Autowired
protected ObjectMapper objectMapper;
@Autowired
private WebApplicationContext context;
@Autowired
BlogRepository blogRepository;
@BeforeEach
public void mockMvcSetUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
blogRepository.deleteAll();
}
@DisplayName("Blog write test")
@Test
public void addArticle() throws Exception {
final String url = "/api/articles";
final String title = "test";
final String content = "This is test.";
final AddArticleRequest request = new AddArticleRequest(title, content);
final String requestBody = objectMapper.writeValueAsString(request);
ResultActions result = mockMvc.perform(post(url)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(requestBody));
result.andExpect(status().isCreated());
}
}
When executing the above code, the following error is output.
D:spring_projectsblogsrctestjavakrjhansolblogcontrollerBlogApiControllerTest.java:55: error: cannot find symbol
result.andExpect(status().isCreated());
^
symbol: method status()
location: class BlogApiControllerTest
New contributor
장성현 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.