I am trying to write a test case for the method decrypt here.
private static Codec codec;
static {
try {
codec = new Codec(encryptionType, encryptionKey, false, true, false);
} catch (CodecException e) {
throw new RuntimeException("Codec initialisation failed", e);
}
}
public static String decrypt(final String toDecrypt) throws CodecException {
String decrypted = codec.decryptFromBase64(toDecrypt);
if (decrypted.endsWith(":")) {
decrypted = decrypted.substring(0, decrypted.length() - 1);
}
return decrypted;
}
Test Case:
@Mock
private Codec codec;
@Test
public void test_decrypt_Success() throws CodecException {
when(codec.decryptFromBase64(TestConstants.toDecrypt)).thenReturn(TestConstants.decrypted);
assertEquals(DocumentUtils.decrypt(TestConstants.toDecrypt), TestConstants.decrypted);
}
Since this is a static method, I can't inject an instance of the class in the test suite and mock its codec. The above code throws an error from the codec library at assert as expected.
What is your approach to testing static methods like this? Or should I not be writing tests for this at all?