Spring Boot - 如何正确地测试图像

时间:2015-10-05 23:17:13

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

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

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

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

1 个答案:

答案 0 :(得分:0)

我解决了。

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