### Question:
Jnacl implements Encryptor { @Override public SharedKey computeSharedKey(final PublicKey publicKey, final PrivateKey privateKey) { final byte[] precomputed = new byte[crypto_secretbox_BEFORENMBYTES]; LOGGER.debug("Computing the shared key for public key {} and private key {}", publicKey, privateKey); final int jnaclResult = secretBox.cryptoBoxBeforenm(precomputed, publicKey.getKeyBytes(), privateKey.getKeyBytes()); if (jnaclResult == -1) { LOGGER.error("Could not compute the shared key for pub {} and priv {}", publicKey, privateKey); throw new EncryptorException("jnacl could not compute the shared key"); } final SharedKey sharedKey = SharedKey.from(precomputed); LOGGER.debug("Computed shared key {} for pub {} and priv {}", sharedKey, publicKey, privateKey); return sharedKey; } Jnacl(final SecureRandom secureRandom, final SecretBox secretBox); @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[] cipherText, final Nonce nonce, final SharedKey sharedKey); @Override Nonce randomNonce(); @Override KeyPair generateNewKeys(); @Override SharedKey createSingleKey();  }### Answer:
@Test public void computingSharedKeyThrowsExceptionOnFailure() { doReturn(-1) .when(this.secretBox) .cryptoBoxBeforenm(any(byte[].class), eq(publicKey.getKeyBytes()), eq(privateKey.getKeyBytes())); final Throwable kaclEx = catchThrowable(() -> this.jnacl.computeSharedKey(publicKey, privateKey)); assertThat(kaclEx).isInstanceOf(EncryptorException.class).hasMessage("jnacl could not compute the shared key"); verify(this.secretBox) .cryptoBoxBeforenm(any(byte[].class), eq(publicKey.getKeyBytes()), eq(privateKey.getKeyBytes())); }
@Test public void computeSharedKeySodiumReturnsSuccess() { doReturn(1).when(this.secretBox).cryptoBoxBeforenm(any(byte[].class), any(byte[].class), any(byte[].class)); final SharedKey result = this.jnacl.computeSharedKey(publicKey, privateKey); assertThat(result).isNotNull(); assertThat(result.getKeyBytes()).isEqualTo(new byte[crypto_secretbox_BEFORENMBYTES]); verify(this.secretBox).cryptoBoxBeforenm(any(byte[].class), any(byte[].class), any(byte[].class)); }