猿问

仅当套件运行时测试才会出现 HttpMediaTypeNotAcceptableException

我是 Spring 开发的新手,正在尝试学习。安装了 Netbeans 11.1 并克隆了这个git 存储库并尝试让这个 VideoController 通过。

我很惊讶为什么 testAddGetVideo() 测试在组中运行时失败,但在单独运行时却通过。测试套件在这里。纠结了好几天了。通读SO和其他帖子。

testAddGetVideo 在测试套件中失败,但个别测试通过(向下滚动查看下文)

在堆栈跟踪中有一个


org.springframework.web.HttpMediaTypeNotAcceptableException


测试添加获取视频():


@Test

public void testAddGetVideo() throws Exception {

    videoSvc.addVideo(video);

    Collection<Video> stored = videoSvc.getVideoList();

    assertTrue(stored.contains(video));

}

它调用的我的 VideoController 方法:


/**

 * getVideos

 *

 * @return Collection<Video>

 */

@RequestMapping(value = VIDEO_SVC_PATH, method = RequestMethod.GET)

public @ResponseBody Collection<Video> getVideos(HttpServletResponse response) {

    System.out.println("---------------getVideos---------------");

    System.out.println(videos.size() + " videos");

    System.out.println("===================");

    response.setContentType("application/json");

    response.setStatus(200);

    return videos.values();

}


/**

 * newVideo

 *

 * @param v

 * @return Video

 */

@RequestMapping(value = VIDEO_SVC_PATH, method = RequestMethod.POST)

public @ResponseBody Video newVideo(@RequestBody Video v) {

    System.out.println("---------------newVideo---------------");


    checkAndSetId(v);

    String url = getDataUrl(v.getId());

    v.setDataUrl(url);


    videos.put(v.getId(), v);

    System.out.println("video " + v.getId() + " at " + v.getDataUrl());

    System.out.println("===================");

    return v;

}

视频服务


public static final String VIDEO_SVC_PATH = "/video";


public static final String VIDEO_DATA_PATH = VIDEO_SVC_PATH + "/{id}/data";


/**

 * This endpoint in the API returns a list of the videos that have

 * been added to the server. The Video objects should be returned as

 * JSON. 

 * 

 * To manually test this endpoint, run your server and open this URL in a browser:

 * http://localhost:8080/video

 * 

 * @return

 */



长风秋雁
浏览 108回答 1
1回答

潇湘沐

不知道为什么,但下面的更改修复了它。问题可能出在自动化测试上。@Testpublic void testGetNonExistantVideosData() throws Exception {&nbsp; &nbsp; long nonExistantId = getInvalidVideoId();&nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; Response r = videoSvc.getData(nonExistantId);&nbsp; &nbsp; &nbsp; &nbsp; assertEquals(404, r.getStatus());&nbsp; &nbsp; }catch(RetrofitError e){&nbsp; &nbsp; &nbsp; &nbsp; assertEquals(404, e.getResponse().getStatus());&nbsp; &nbsp; }}我的方法签名如下。我将方法更改为当传入无效 id 作为参数时返回 null,并且仅当传入有效 id 并且二进制视频 mpeg 数据成功写入 HttpServletResponse.Outputstream 时才返回 VideoStatus。换句话说,做 aresponse.sendError()也需要 a return null;。&nbsp; &nbsp;@RequestMapping(value = VIDEO_DATA_PATH, method = RequestMethod.POST)&nbsp; &nbsp; public @ResponseBody VideoStatus uploadVideo(@PathVariable("id") long id, @RequestParam("data") MultipartFile data, HttpServletResponse response) {&nbsp; &nbsp; &nbsp; &nbsp; VideoStatus status = new VideoStatus(VideoStatus.VideoState.READY);
随时随地看视频慕课网APP

相关分类

Java
我要回答