Java Spring Boot-如何测试映像是否正确提供
我有一个显示图像的web应用程序。我想做一个测试,检查图像是否正确显示 我在Java Spring Boot-如何测试映像是否正确提供,java,image,testing,web-applications,spring-boot,Java,Image,Testing,Web Applications,Spring Boot,我有一个显示图像的web应用程序。我想做一个测试,检查图像是否正确显示 我在root/src/test/resources/static/images/Head.png中找到了该图像 我的代码是: 这会将图像返回给我 ResponseEntity<byte[]> entity = new TestRestTemplate().getForEntity("http://localhost/images/Head.png", byte[].class); 现在,我尝试检查内容是否相同,
root/src/test/resources/static/images/Head.png
中找到了该图像
我的代码是:
这会将图像返回给我
ResponseEntity<byte[]> entity = new TestRestTemplate().getForEntity("http://localhost/images/Head.png", byte[].class);
现在,我尝试检查内容是否相同,但不起作用。我收到一个
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();
我解决了
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();
FileNotFoundException
发生的原因是您指定的文件路径不正确。是的,我知道。但是我应该指定哪条路径来识别它。或者我应该使用另一种方式获取文件内容吗?可能会出现重复的FileNotFoundException
,因为您指定的文件路径不正确。是的,我知道。但是我应该指定哪条路径来识别它。或者我应该使用另一种方式来获取我的文件的内容?可能是