Spring 引导 - 如何测试图像是否正确提供

Spring Boot - How to test an image is correctly served

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

我有 root/src/test/resources/static/images/Head.png 中的图像。

我的代码是:

这张returns我的图片。

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

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

我解决了

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();