@Test
public void testEncryptFailed() throws Exception {

  KeysetHandle handle =
      KeysetManager.withEmptyKeyset()
          .rotate(MacKeyTemplates.HMAC_SHA256_128BITTAG)
          .getKeysetHandle();
  // Encrypt with dummy Aead.
  TestUtil.DummyAead faultyAead = new TestUtil.DummyAead();
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  KeysetWriter writer = BinaryKeysetWriter.withOutputStream(outputStream);
  try {
    handle.write(writer, faultyAead);
    fail("Expected GeneralSecurityException");
  } catch (GeneralSecurityException e) {
    assertExceptionContains(e, "dummy");
  }
}