### Question:
Kalium implements Encryptor { @Override public KeyPair generateNewKeys() { final byte[] publicKey = new byte[CRYPTO_BOX_CURVE25519XSALSA20POLY1305_PUBLICKEYBYTES]; final byte[] privateKey = new byte[CRYPTO_BOX_CURVE25519XSALSA20POLY1305_SECRETKEYBYTES]; LOGGER.info("Generating new keypair..."); final int sodiumResult = this.sodium.crypto_box_curve25519xsalsa20poly1305_keypair(publicKey, privateKey); if (sodiumResult == -1) { LOGGER.error("Unable to generate a new keypair!"); throw new EncryptorException("Kalium could not generate a new public/private keypair"); } final PublicKey pubKey = PublicKey.from(publicKey); final PrivateKey privKey = PrivateKey.from(privateKey); LOGGER.info("Generated new keypair with public key {}", pubKey); LOGGER.debug("Generated public key {} and private key {}", pubKey, privKey); return new KeyPair(pubKey, privKey); } Kalium(final NaCl.Sodium sodium); @Override SharedKey computeSharedKey(final PublicKey publicKey, final PrivateKey privateKey); @Override byte[] seal(
            final byte[] message, final Nonce nonce, final PublicKey publicKey, final PrivateKey privateKey); @Override byte[] open(
            final byte[] cipherText, final Nonce nonce, final PublicKey publicKey, final PrivateKey privateKey); @Override byte[] sealAfterPrecomputation(final byte[] message, final Nonce nonce, final SharedKey sharedKey); @Override byte[] openAfterPrecomputation(final byte[] encryptedPayload, final Nonce nonce, final SharedKey sharedKey); @Override Nonce randomNonce(); @Override KeyPair generateNewKeys(); @Override SharedKey createSingleKey();  }### Answer:
@Test public void generatingNewKeysThrowsExceptionOnFailure() { doReturn(-1) .when(this.sodium) .crypto_box_curve25519xsalsa20poly1305_keypair(any(byte[].class), any(byte[].class)); final Throwable kaclEx = catchThrowable(() -> this.kalium.generateNewKeys()); assertThat(kaclEx) .isInstanceOf(EncryptorException.class) .hasMessage("Kalium could not generate a new public/private keypair"); verify(this.sodium).crypto_box_curve25519xsalsa20poly1305_keypair(any(byte[].class), any(byte[].class)); }
@Test public void generateNewKeysSodiumSuccess() { when(sodium.crypto_box_curve25519xsalsa20poly1305_keypair(any(byte[].class), any(byte[].class))).thenReturn(1); final KeyPair result = kalium.generateNewKeys(); assertThat(result).isNotNull(); assertThat(result.getPrivateKey()).isNotNull(); assertThat(result.getPublicKey()).isNotNull(); verify(sodium).crypto_box_curve25519xsalsa20poly1305_keypair(any(byte[].class), any(byte[].class)); }