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.