vendredi 1 juillet 2016

Error mocking Cipher in java

In order to do the test, I mock a Cipher object, but when I run the test, its fails because it's run the cipher code instead of the mock.

So, I write a very simple test and when I run it, fails always.

My test code is:

import javax.crypto.Cipher;

import org.junit.Test;
import org.mockito.Mockito;

public class SimpleTest {
    @Test
    public void simpleTest() throws Exception {
        Cipher cipher = Mockito.mock(Cipher.class);
        cipher.doFinal(null);
        assertTrue(true);
    }
}

And the exception who produces:

java.lang.IllegalStateException: Cipher not initialized
    at javax.crypto.Cipher.checkCipherState(Cipher.java:1750)
    at javax.crypto.Cipher.doFinal(Cipher.java:2157)
    at SimpleTest.simpleTest(SimpleTest.java:12)
    ...

Why is doFinal executed?

Aucun commentaire:

Enregistrer un commentaire