2015-10-05 59 views
1

我有一个显示图像的Web应用程序。我想做一个测试,检查图像是否正确显示。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); 

我怎么能测试收到的图像和我在服务器中存储的图像是否一样?谢谢

+0

'因为你指定的路径到您的文件不正确FileNotFoundException'发生。 – kryger

+0

是的,我知道。但是,我应该指定哪个路径来识别它。或者我应该用另一种方式来获取我的文件的内容? –

+0

[如何将文本文件资源读入Java单元测试?](http://stackoverflow.com/questions/3891375/how-to-read-a-text-file-resource-into-java-单元测试) – kryger

回答

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