【问题标题】:Spring Boot - How to test an image is correctly servedSpring Boot - 如何测试图像是否正确提供
【发布时间】:2015-10-05 23:17:13
【问题描述】:

我有一个显示图像的网络应用程序。我想做一个测试来检查图像是否正确显示。

我的图片在root/src/test/resources/static/images/Head.png

我的代码是:

这会将图像返回给我。

ResponseEntity<byte[]> entity = new TestRestTemplate().getForEntity("http://localhost/images/Head.png", byte[].class);

这会检查接收到的图像的长度是否正确。

assertEquals("Wrong length\n", entity.getHeaders().getContentLength(), 442526);

现在,我尝试检查内容是否相同,但不起作用。我收到了FileNotFoundException

final File fichero = new File("/images/Head.png");
final FileInputStream ficheroStream = new FileInputStream(fichero);
final byte[] contenido = new byte[(int)fichero.length()];
ficheroStream.read(contenido);
assertEquals("Wrong content\n", entity.getBody(), contenido);

如何测试接收到的图像和我存储在服务器中的图像是否相同?谢谢

【问题讨论】:

  • FileNotFoundException 发生是因为您错误地指定了文件的路径。
  • 是的,我知道。但是我应该指定哪个路径才能使其被识别。或者我应该使用其他方式来获取我的文件内容?

标签: java image testing web-applications spring-boot


【解决方案1】:

我解决了。

final InputStream reader = getClass().getResourceAsStream("/static/images/Head.png");
final byte[] contenido = new byte[442526];
reader.read(contenido);

然后,比较:

assertTrue("Wrong content\n", Arrays.equals(entity.getBody(), contenido));
reader.close();

【讨论】:

    猜你喜欢
    • 2018-01-27
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    相关资源
    最近更新 更多