gpt4 book ai didi

Java Spring Controller 断点和 JUnit

转载 作者:行者123 更新时间:2023-11-30 05:19:11 28 4
gpt4 key购买 nike

我有一个带有 Junit 测试的 Spring REST 应用程序。我在 Eclipse 中运行该应用程序并开始测试。如果我在 Junit 类中设置断点,它会正常工作并停止执行,但如果我直接在被调用的 Controller 中设置断点,则不会发生任何情况。我做错了什么?

@Test
public void testAddVideoData() throws Exception {
Video received = videoSvc.addVideo(video);
VideoStatus status = videoSvc.setVideoData(received.getId(),
new TypedFile(received.getContentType(), testVideoData));
assertEquals(VideoState.READY, status.getState());

Response response = videoSvc.getData(received.getId());
assertEquals(200, response.getStatus());

InputStream videoData = response.getBody().in();
byte[] originalFile = IOUtils.toByteArray(new FileInputStream(testVideoData));
byte[] retrievedFile = IOUtils.toByteArray(videoData);
assertTrue(Arrays.equals(originalFile, retrievedFile));
}

这是我要设置断点的方法:

@RequestMapping(value = "/video/{id}/data" , method = RequestMethod.POST)
public @ResponseBody VideoStatus addVideoData(AtomicLong id, @RequestParam("data")
MultipartFile videoData) {

VideoStatus videoStatus= new VideoStatus(VideoState.PROCESSING);


try {
videoDataMgr = VideoFileManager.get();
Video video = videos.get(id);

if(!videoDataMgr.hasVideoData(video)) {
InputStream in = videoData.getInputStream();
videoDataMgr.saveVideoData(video, in);
}
videoStatus.setState(VideoState.READY);


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// SAVE IT TO DISK

return videoStatus;

}

最佳答案

由于应用程序和测试在 2 个不同的 JVM 中运行,因此您必须(使用 Eclipse):

  • 对于测试:“作为 Java 应用程序运行”
  • 对于应用程序:“作为 Java 应用程序进行调试”,在需要时设置断点。

关于Java Spring Controller 断点和 JUnit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59825968/

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