在我的springrestapi中,我得到了一个被分页的端点——作为输入参数,我接收到了pageable。默认情况下,分页参数的名称为“page”和“size”。我想使用名为“limit”的参数,而不是“size”。要更改此设置,我使用了spring配置参数:
spring.data.web.pageable.size-parameter = limit
当我启动spring应用程序并手动测试时,一切都正常工作。但是当我想进行单元测试时,mockmvc总是接受参数“size”来进行分页,而不是“limit”。我在测试和普通应用程序的spring配置中添加了config param,但它仍然不起作用。
mockMvc.perform(get(API_BASE + "/users")
.param("page", "1")
.param("limit", "1")
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.PRAGMATIC_API_VALUE))
.andExpect(content().json(readFile("pragmatic/response/ok-single-user-list.json"), true));
编辑:我还添加了我的测试类
@Test(groups = "unit")
@WebAppConfiguration
@EnableWebMvc
@ContextConfiguration(classes = {
//spring context - real implementation classes
PragmaticApiControllerTestSupportConfiguration.class,
//mocking classes
ApiControllerMockConfiguration.class,
//real implementation classes - business related
DeviceApiController.class,
})
public class DeviceApiControllerTest extends AbstractTestNGSpringContextTests {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Autowired
private DeviceApiCreateService deviceApiService;
@BeforeClass
public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@BeforeMethod
public void resetMocks() {
reset(mocks());
}
private Object[] mocks() {
return new Object[]{deviceApiService};
}
@Test
public void test() throws Exception {
expectTenantAuthorizationCall();
expectCreateCallContextCall();
expectDeviceListFindCall();
expectDeviceResourceConversionCall();
replayMocks();
mockMvc.perform(get(API_BASE + "/users")
.param("page", "1")
.param("limit", "1")
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.PRAGMATIC_API_VALUE))
.andExpect(content().json(readFile("pragmatic/response/ok-single-user-list.json"), true));
verifyMocks();
}
}
暂无答案!
目前还没有任何答案,快来回答吧!