0

We recently upgraded our JDK from 11 to 17. Part of this upgrade included an upgrade from mockito from version 3.3.3 to mockito version 4.11.0.

One of our existing tests which used to work in the past broke because of this. In this test we spy on a ByteArrayOutputStream object to verify that it does not get closed. This test fails because of the error given below.

@Test
void encryptionDoesNotCloseOutputStream() throws Throwable {
    byte[] key = new byte[32];
    RANDOM.nextBytes(key);
    byte[] data = new byte[101];
    RANDOM.nextBytes(data);
    ByteArrayInputStream in = spy(new ByteArrayInputStream(data));
    ByteArrayOutputStream out = spy(new ByteArrayOutputStream());

    ...
    
    // Write the IV at the beginning of the file (first 12 Bytes)
    out.write(buffers.iv);
    
    ...

    verify(in, never()).close();
    verify(out, never()).close();
}

This results in the following error.

[ERROR] Errors: 
[ERROR]   CryptoUtilsTest.encryptionDoesNotCloseOutputStream:47 » NullPointer Cannot read the array length because "this.buf" is null

As discussed in a related issue, How to properly spy on an input stream, I tried to change our mockito dependency from mockito-core to mockito-inline but this did not change anything.

We did not yet verify whether or not this issue also occurs in a java-11 environment when upgrading mockito. It does happen when just updating java to java-17 without touching mockito at all.

  • 1
    Side-note: what's the point in verifying a byte array stream never gets closed since closing it does nothing? **Edit**: sauce [here](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/ByteArrayOutputStream.html#close()) and [here](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/ByteArrayInputStream.html#close()) – Mena Jan 10 '23 at 16:34
  • While true, the usage is not restricted to ByteArrayOutputStream and the close might have effect for the other ones, we simply wish to verify whether or not the close operation is invoked, independent of which output stream is passed. – Lars van roy Jan 11 '23 at 07:17

0 Answers0