我正在实现这个取自 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()));
}
...
}
最佳答案
我知道这是一个非常古老的话题,但如果其他人遇到同样的问题,这里有一个答案。 andExpect() 需要移到 perform() 函数的右括号之外。
关于java - 无法解析 .andExpect() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45804490/