代码之家  ›  专栏  ›  技术社区  ›  Ayoub k

弹簧启动测试错误:java.lang.IllegalArgumentException异常:页不能为空

  •  -1
  • Ayoub k  · 技术社区  · 6 年前

    @RepositoryRestController
    @RequestMapping("movies")
    public class MovieController {
    
        private MovieService movieService;
        private PagedResourcesAssembler<Movie> pagedAssembler;
        private MovieResourceAssembler movieResourceAssembler;
    
        @Autowired
        public void setMovieService(MovieService movieService) {
            this.movieService = movieService;
        }
    
        @Autowired
        public void setPagedAssembler(PagedResourcesAssembler<Movie> pagedAssembler) {
            this.pagedAssembler = pagedAssembler;
        }
    
        @Autowired
        public void setMovieResourceAssembler(MovieResourceAssembler movieResourceAssembler) {
            this.movieResourceAssembler = movieResourceAssembler;
        }
    
        // Return all movies with pagination
        @GetMapping
        public ResponseEntity<?> getAllMovies(Pageable pageable) {
            Page<Movie> moviePage = this.movieService.getAllMovies(pageable);
            // Remove some unnecessary fields
            //this.movieService.removeUndesirableFieldsFromListing(moviePage);
            return ResponseEntity.ok(this.pagedAssembler.toResource(moviePage, this.movieResourceAssembler));
        }
     }
    

    @RunWith(SpringRunner.class)
    @SpringBootTest
    @AutoConfigureMockMvc
    public class MovieControllerTest {
    
        @Autowired
        private MockMvc mockMvc;
    
        @MockBean
        private MovieService movieService;
    
        @Test
        public void getAllMovies_PageableGiven_ShouldReturnMoviesPage() throws Exception {
            List<Movie> movieList = new ArrayList<>();
            movieList.add(new Movie());
            movieList.add(new Movie());
            Page<Movie> moviePage = new PageImpl<>(movieList);
            given(this.movieService.getAllMovies(PageRequest.of(0,2)))
                    .willReturn(moviePage);
            this.mockMvc.perform(get("http://localhost:8080/api/movies?page=1"))
                    .andExpect(status().isOk());
        }
    }
    

    我得到以下错误:

    org.springframework.web网站.util.NestedServletException异常:请求处理失败;嵌套异常为java.lang.IllegalArgumentException异常:页不能为空!

    2 回复  |  直到 6 年前
        1
  •  1
  •   CRISTIAN ROMERO MATESANZ    6 年前

    您可以使用Spring的mockMvc对象将其注入测试类:

     @Autowired
     private MockMvc mockMvc;
    

    我刚刚创建了一个测试方法,使用mockMvc.perform命令发送页面请求的方法:

    @GetMapping(produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<BaseResponse> listAllBase(
            @PageableDefault(size = 50, page = 2) Pageable pageable) {
    
        // logger.debug("paginación recibida :{}",pageable);
        List<BaseResponse> findAllBases = baseService.findAllBases();
        return findAllBases;
    
    }
    

    我的测试代码:

    mockMvc.perform(get("/base/?size=2&page=0")).andExpect(status().isOk())
                 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))                        .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
                 .andExpect(jsonPath("$", hasSize(2)))                       .andExpect(jsonPath("$", hasSize(2)))
                 .andExpect(jsonPath("$[0].name", equalToIgnoringCase("margarita")))
    

    请参阅my GitHub repo中的完整类代码:

    控制器:

    https://github.com/cristianprofile/spring-boot-mvc-complete-example/blob/develop/spring-boot-mvc-rest/src/main/java/com/mylab/cromero/controller/HelloWorldController.java#L53

    https://github.com/cristianprofile/spring-boot-mvc-complete-example/blob/develop/spring-boot-mvc-rest/src/test/java/com/mylab/cromero/controller/RestTestIT.java#L66

    可以在您的项目中随意使用:)

        2
  •  0
  •   Fabio Manzano    6 年前

    通过读取这两个Stackoverflow线程( [1] [2] Spring documentation ,似乎应该使用带有存储库的页面。例子:

    PagingAndSortingRepository<User, Long> repository = // … get access to a bean
    Page<User> users = repository.findAll(new PageRequest(1, 20));
    

    PagedListHolder ,而不是。例子:

    List<Movie> movieList = new ArrayList<>();
    movieList.add(new Movie());
    movieList.add(new Movie());
    PagedListHolder<Movie> holder = new PagedListHolder<>(movieList);