1

I've a web app which displays an image. I want to do a test that checks that the image is correctly showed.

I've the image in root/src/test/resources/static/images/Head.png.

My code is:

This returns me the image.

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

This checks that the length of the image received is the correct.

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

And now, I try to check that the content is the same, but doesn't work. I receive a 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);

How could I test that the image received and the image I store in my server are the same? Thanks

Santiago Gil
  • 1,292
  • 7
  • 21
  • 52
  • `FileNotFoundException` happens because you specified the path to your file incorrectly. – kryger Oct 06 '15 at 07:23
  • Yes, I know that. But which path should I specify to get it recognized. Or should I use another way to get the content of my file? – Santiago Gil Oct 06 '15 at 19:38
  • Possible duplicate of [How to read a text-file resource into Java unit test?](http://stackoverflow.com/questions/3891375/how-to-read-a-text-file-resource-into-java-unit-test) – kryger Oct 06 '15 at 19:43

1 Answers1

0

I solved it.

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

And then, the comparison:

assertTrue("Wrong content\n", Arrays.equals(entity.getBody(), contenido));
reader.close();
Santiago Gil
  • 1,292
  • 7
  • 21
  • 52