gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:50:38 25 4
gpt4 key购买 nike

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

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

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

testAddGetVideo fails in test suite

在堆栈跟踪中有一个

org.springframework.web.HttpMediaTypeNotAcceptableException

testAddGetVideo():

@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
*/
@GET(VIDEO_SVC_PATH)
public Collection<Video> getVideoList();

/**
* This endpoint allows clients to add Video objects by sending POST requests
* that have an application/json body containing the Video object information.
*
* @return
*/
@POST(VIDEO_SVC_PATH)
public Video addVideo(@Body Video v);

日志:

---------------newVideo---------------
video 1 at http://localhost:8080/video/1/data
===================
---------------uploadVideo---------------
1 for video 1 data data
===================
---------------downloadVideo---------------
1 for video 1
===================
---------------newVideo---------------
video 2 at http://localhost:8080/video/2/data
===================

FAILS HERE TRANSMITTING THE VIDEOS

---------------getVideos---------------
2 videos
===================
---------------uploadVideo---------------

-9223372036854775808 for non existent video

发送无效 ID -9223372036854775808 是测试的一部分。

getVideos() 失败。 (日志输出的顺序有偏差)

2019-09-09 14:50:28.988  WARN 16804 --- [tp1776486598-21] org.eclipse.jetty.server.Response        : Committed before 406 null
2019-09-09 14:50:28.988 WARN 16804 --- [tp1776486598-21] .w.s.m.s.DefaultHandlerExceptionResolver : Handling of [org.springframework.web.HttpMediaTypeNotAcceptableException] resulted in Exception

java.lang.IllegalStateException: Committed
at org.eclipse.jetty.server.Response.resetBuffer(Response.java:1242)
at org.eclipse.jetty.server.Response.sendError(Response.java:567)
at org.eclipse.jetty.server.Response.sendError(Response.java:544)
at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMediaTypeNotAcceptable(DefaultHandlerExceptionResolver.java:246)
at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.doResolveException(DefaultHandlerExceptionResolver.java:119)
at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:138)
at org.springframework.web.servlet.handler.HandlerExceptionResolverComposite.resolveException(HandlerExceptionResolverComposite.java:75)

...堆栈跟踪继续...

但是当我立即运行单独的测试而不重新启动服务器时,它就通过了。

---------------newVideo---------------
video 3 at http://localhost:8080/video/3/data
===================
---------------getVideos---------------
3 videos
===================

单独测试通过

individual test passes

最佳答案

不知道为什么,但下面的更改修复了它。问题可能出在自动化测试上。

@Test
public void testGetNonExistantVideosData() throws Exception {

long nonExistantId = getInvalidVideoId();

try{
Response r = videoSvc.getData(nonExistantId);
assertEquals(404, r.getStatus());
}catch(RetrofitError e){
assertEquals(404, e.getResponse().getStatus());
}
}

我的方法签名如下。 我将方法更改为当传入无效的 id 作为参数时返回 null,并且仅当传入有效的 id 并且二进制视频 mpeg 数据成功写入 HttpServletResponse.Outputstream 时才返回 VideoStatus .

换句话说,执行 response.sendError() 还需要 return null;

   @RequestMapping(value = VIDEO_DATA_PATH, method = RequestMethod.POST)
public @ResponseBody VideoStatus uploadVideo(@PathVariable("id") long id, @RequestParam("data") MultipartFile data, HttpServletResponse response) {
VideoStatus status = new VideoStatus(VideoStatus.VideoState.READY);

关于java - 仅当套件运行时测试才会出现 HttpMediaTypeNotAcceptableException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57862175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com