【问题标题】:Spring Boot + Mockito - MockMvc lacking methodsSpring Boot + Mockito - MockMvc 缺少方法
【发布时间】:2017-07-11 19:53:25
【问题描述】:

我有一个简单的测试:

@RunWith(SpringRunner.class)
@WebMvcTest(MainController.class)
public class MainControllerTest extends ControllerTest {


@Autowired
private MockMvc mvc;

@MockBean
private Storage storage;

@MockBean
private PersonListMarshaller marshaller;


@Test
public void getTest() throws Exception{
    mvc.perform(get("/"))
            .andExpect(status().isOk())
            .andExpect(view().name("index"));
}

@Test
public void postTest() throws Exception{


}

}

在我想调用的 postTest() 方法中: mvc.perform(post("/")).param(...); 我对“param()”部分有疑问,因为 intelliJ Idea 无法识别该方法。我搜索了文档,也没有找到。不过,我已经看到人们在各种与 spring 相关的站点中使用它(以及其他一些我无法使用的方法)。为什么我不能使用它?

【问题讨论】:

    标签: spring spring-mvc junit mockito


    【解决方案1】:

    param 的呼叫需要关闭。更具体地说,post 返回具有 param 方法的 MockHttpServletRequestBuilder。应该是这样的

    mvc.perform(post("/").param("", ""))
        .andExpect(...)
    

    【讨论】:

    • 确实如此。没注意到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    • 2023-03-31
    • 2016-02-14
    • 1970-01-01
    • 2022-01-22
    • 2018-05-21
    • 2021-08-21
    相关资源
    最近更新 更多