无法解析 .andExpect() 方法

use*_*742 4 java testing rest spring intellij-idea

我正在实施从http://www.lucassaldanha.com/unit-and-integration-tests-in-spring-boot/获取的这个测试类

我的 IDE (Intellij) 没有解析 .andExpect() 方法。我在网上搜索过,但找不到这是哪个 jar 或类的一部分。谁能帮我吗?谢谢你。

@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()));
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

Nod*_*mes 11

我知道这是一个非常古老的线程,但如果其他人遇到同样的问题,这里有一个答案。andExpect() 需要移到 perform() 函数的右括号之外。