Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve .andExpect() method

I am implementing this test class taken from http://www.lucassaldanha.com/unit-and-integration-tests-in-spring-boot/

My IDE (Intellij) is not resolving the .andExpect() method. I've searched the web but cannot find which jar or class this is part of. Can anyone help me out? Thank you.

@RunWith(SpringRunner.class)
public class ClientControllerTest {

    @Autowired
    MockMvc mockMvc;

    @MockBean
    CreateClientService createClientServiceMock;

    @Autowired
    ObjectMapper objectMapper;

    @Test
    public void testCreateClientSuccessfully() throws Exception {
        given(createClientServiceMock.createClient("Foo")).willReturn(new       Client("Foo"));

    mockMvc.perform(post("/clients")
        .contentType(MediaType.APPLICATION_JSON)
        .content(objectMapper.writeValueAsBytes(new     CreateClientRequest("Foo"))))
        .andExpect(status().isCreated())
        .andExpect(jsonPath("$.name", is("Foo")))
        .andExpect(jsonPath("$.number", notNullValue()));
    }
    ...
}
like image 594
user3010742 Avatar asked Dec 11 '22 10:12

user3010742


2 Answers

I understand that this is a very old thread, but in case anyone else runs into the same issue here's an answer. andExpect() needs to be moved outside of the perform() function's closing brackets.

like image 128
NodziGames Avatar answered Dec 20 '22 22:12

NodziGames


It's part of spring test framework. Implements interface org.springframework.test.web.servlet.ResultActions

Maven artifact : Maven central

like image 23
Anclav Avatar answered Dec 20 '22 23:12

Anclav