Constant MissingServletRequestParameterException



出于某种原因,我继续获取错误消息:

MissingServletRequestParameTreexception:必需字符串[]参数 " IDS"不存在

我有一个可以从/products?ids=["item1", "item2"]

调用的端点

我的集成测试类看起来像:

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(MockitoJUnitRunner.class)
public class ProductControllerTest {
    private MockMvc mockMvc;
    @InjectMocks
    private ProductController controller;
    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
    }
    @Test
    public void shouldFetchProducts() throws Exception {
        mockMvc.perform(get("/products")
                .param("ids[]", "["item"]"))
                .andExpect(status().is2xxSuccessful());
    }
}

我尝试了什么:

  1. 使用 param

    @Test
    public void shouldFetchProducts() throws Exception {
        mockMvc.perform(get("/products")
                .param("ids[]", "["item"]"))
                .andExpect(status().is2xxSuccessful());
    }
    
  2. 使用 requestattr

    @Test
    public void shouldFetchProducts() throws Exception {
        mockMvc.perform(get("/products")
            .requestAttr("ids[]", "["item"]"))
            .andExpect(status().is2xxSuccessful());
    }
    

我在做什么错?

您尝试过

是否尝试过
@Test
public void shouldFetchProducts() throws Exception {
    mockMvc.perform(get("/products")
            .param("ids", "item1", "item2"))
            .andExpect(status().is2xxSuccessful());
}

可以在此处找到param的文档。

尝试使用MultiValueMap

MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("ids", "item1");
params.add("ids", "item2");

现在编写测试案例时,请使用以下内容:

mockMvc.perform(get("/products")
            .params(params))
            .andExpect(status().is2xxSuccessful());

相关内容

  • 没有找到相关文章

最新更新