Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring启动测试错误:java.lang.IllegalArgumentException:页面不能为null_Spring_Unit Testing_Spring Boot_Junit_Mockito - Fatal编程技术网

Spring启动测试错误:java.lang.IllegalArgumentException:页面不能为null

Spring启动测试错误:java.lang.IllegalArgumentException:页面不能为null,spring,unit-testing,spring-boot,junit,mockito,Spring,Unit Testing,Spring Boot,Junit,Mockito,我正在尝试测试以下控制器: @RepositoryRestController @RequestMapping("movies") public class MovieController { private MovieService movieService; private PagedResourcesAssembler<Movie> pagedAssembler; private MovieResourceAssembler movieResource

我正在尝试测试以下控制器:

@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));
    }
 }
@RepositoryRestController
@请求映射(“电影”)
公共级电影控制器{
私人电影服务;
专用页数据源汇编程序;
私人电影采购商电影采购商;
@自动连线
公共无效设置MovieService(MovieService MovieService){
this.movieService=movieService;
}
@自动连线
公共无效设置页面组装器(页面数据源组装器页面组装器){
this.pagedAssembler=pagedAssembler;
}
@自动连线
公共无效设置MovierSourcesAssembler(MovierSourcesAssembler MovierSourcesAssembler){
this.moviersourceAssembler=moviersourceAssembler;
}
//返回带有分页的所有电影
@GetMapping
公共响应getAllMovies(可分页){
Page moviePage=this.movieService.getAllMovies(可分页);
//删除一些不必要的字段
//this.movieService.removeUnderreableFieldsFromListing(moviePage);
返回ResponseEntity.ok(this.pagedAssembler.toResource(moviePage,this.moviersourceAssembler));
}
}
下面是测试:

@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());
    }
}
@RunWith(SpringRunner.class)
@春靴测试
@AutoConfigureMockMvc
公共级电影控制器测试{
@自动连线
私有MockMvc-MockMvc;
@蚕豆
私人电影服务;
@试验
public void getAllMovies\u PageableGiven\u ShouldReturnMoviesPage()引发异常{
List movieList=new ArrayList();
添加(新电影());
添加(新电影());
Page moviePage=新页面导入(电影列表);
给定(this.movieService.getAllMovies(PageRequest.of(0,2)))
.willReturn(电影版);
this.mockMvc.perform(get(“http://localhost:8080/api/movies?page=1"))
.andExpect(status().isOk());
}
}
我得到了以下错误:

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


通过阅读这两个Stackoverflow线程(和)以及,您似乎应该使用带有存储库的页面。例如:

PagingAndSortingRepository<User, Long> repository = // … get access to a bean
Page<User> users = repository.findAll(new PageRequest(1, 20));
List<Movie> movieList = new ArrayList<>();
movieList.add(new Movie());
movieList.add(new Movie());
PagedListHolder<Movie> holder = new PagedListHolder<>(movieList);

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

 @Autowired
 private MockMvc mockMvc;
我刚刚使用mockMvc创建了一个测试方法。执行方法发送页面请求:

我的控制器代码:

@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;

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

控制器:

测试类方法:


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

您可以发布完整的StatfkTrace吗?您可以提供所有Java类n Pom文件吗
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")))