Lempkin
Lempkin

Reputation: 1598

unit test : comparing file generation assertion returns false

I try to write my unit test to test my xlsx file generation. I store a valid file in test resources, then try to generate the same file and compare them.

InputStream xlsxContent = xlsxContentWriter.generateXlsx(getSamples(10), RawEventDto.class);
InputStream file = new ByteArrayInputStream(Files.readAllBytes(Paths.get(ClassLoader.getSystemResource("test.xlsx").toURI())));
Assert.assertEquals(IOUtils.toByteArray(file), IOUtils.toByteArray(xlsxContent));

Problem : assertion returns false.

To be sure that the generated file is the same than the file in resources, I saved the generated file itself in resources, so it's obviously the same :

InputStream xlsxContent = xlsxContentWriter.generateXlsx(getSamples(10), RawEventDto.class);           
File targetFile = new File("/home/user/test.xlsx");
OutputStream outStream = new FileOutputStream(targetFile);
byte[] buffer = new byte[1024];
int bytesRead;
while((bytesRead = xlsxContent.read(buffer)) !=-1){
      outStream.write(buffer, 0, bytesRead);
}
xlsxContent.close();
outStream.flush();
outStream.close();

Any idea why this returns false? Any trick to know about file ?

Edit :

Here's my code now :

           InputStream file = new ByteArrayInputStream(Files.readAllBytes(Paths.get(ClassLoader.getSystemResource("test.xlsx").toURI())));
        byte[] byteArray1 = IOUtils.toByteArray(file);
        byte[] byteArray2 = IOUtils.toByteArray(xlsxContent);


        Assert.assertEquals(IOUtils.toByteArray(file).length, IOUtils.toByteArray(xlsxContent).length);
        Assert.assertTrue(Arrays.equals(byteArray1,byteArray2));

But I still getting assertion = false, actually I'm getting this error :

java.lang.AssertionError: null at org.junit.Assert.fail(Assert.java:86) at org.junit.Assert.assertTrue(Assert.java:41) at org.junit.Assert.assertTrue(Assert.java:52) at com.renault.hsmt.util.test.XlsxContentWriterTest.testXlsxWrite(XlsxContentWriterTest.java:61)

Actually, even this code returns false (I'm comparing generated file to itself, without taking it from resources folder) :

InputStream xlsxContent = xlsxContentWriter.generateXlsx(getSamples(10), RawEventDto.class);
            byte[] byteArray1 = IOUtils.toByteArray(xlsxContent);
            byte[] byteArray2 = IOUtils.toByteArray(xlsxContent);


            Assert.assertEquals(IOUtils.toByteArray(xlsxContent).length, IOUtils.toByteArray(xlsxContent).length);
            Assert.assertTrue(Arrays.equals(byteArray1,byteArray2));

Upvotes: 1

Views: 703

Answers (2)

Dcortez
Dcortez

Reputation: 4238

If you are using JUnit 5 you can just call

Assertions.assertArrayEquals(array1,array2);

JUnit5 Assertions documentation

Upvotes: 1

RichardK
RichardK

Reputation: 3471

You will not compare 2 byte arrays like this, use Arrays.equals instead, example:

public static void main(String[] args) {
    byte[] a = {0,1};
    byte[] b = {0,1};
    System.out.println(a.equals(b)); //false
    System.out.println(Arrays.equals(a, b)); //true
}

in your case it will be:

  Assert.assertTrue(Arrays.equals(IOUtils.toByteArray(file), IOUtils.toByteArray(xlsxContent)));

Upvotes: 2

Related Questions