0

I am writing JUnit test cases, In starting only I have written with @RunWith(MockitoJUnitRunner.class), and it was working fine.

And later I changed with @ExtendWith(MockitoExtension.class), and it started giving the null pointer, I don't understand why it's happening. whats wrong with here.

Base code: ----------------

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
public class IdpsDecryptorImpl implements IdpsDecryptor {

    @Value("${idps.key}")
    private String idpsKey;

    @Autowired
    private IdpsClient idpsClient;

    private Key retrieveKey() {
        return idpsClient.newKeyHandleLatest(idpsKey);
    }

    public String decrypt(byte[] encryptedBytes) {
        byte[] decryptedBytes;
        Key keyHandle = retrieveKey();
        try {
            decryptedBytes = keyHandle.deriveTwiceAndDecryptLocal(encryptedBytes);
        } catch (IdpsException | IdpsCommunicationException ex) {
            throw new StorageException(ErrorCode.IDPS_DECRYPT_ERROR, ex.getMessage());
        }
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }
}

   
------------------
Test case:


import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;


/**
 * @author : sprasad13
 * @created : 11/07/22, Monday
 **/

@ExtendWith(MockitoExtension.class)
//@RunWith(MockitoJUnitRunner.class)
public class IdpsDecryptorImplTest extends TestCase {

    @InjectMocks
    private IdpsDecryptorImpl idpsDecryptorimpl;
    //@Mock
    Key key;

    @Mock
    IdpsClient idpsClient;

    private String idpsKey = "testKey";

    @Test
    public void testDecrypt() throws IdpsCommunicationException, IdpsException {
        ReflectionTestUtils.setField(idpsDecryptorimpl,"key",key);
        byte[] encryptedBytes = new byte[2];
        encryptedBytes[0] = 20;
        encryptedBytes[1] = 30;

        byte[] decryptedBytes = new byte[2];
        decryptedBytes[0] = 97;
        decryptedBytes[1] = 98;

        Mockito.when(idpsClient.newKeyHandleLatest(idpsKey)).thenReturn(key);
        Mockito.when(key.deriveTwiceAndDecryptLocal(encryptedBytes)).thenReturn(decryptedBytes);

        Assert.assertNotNull(idpsDecryptorimpl.decrypt(encryptedBytes));

    }

    @Test(expected = StorageException.class)
    public void testDecryptException() throws IdpsCommunicationException, IdpsException {
        byte[] encryptedBytes = new byte[1];
        encryptedBytes[0] = 20;

        byte[] decryptedBytes = new byte[1];
        decryptedBytes[0] = 97;

        Mockito.when(idpsClient.newKeyHandleLatest(idpsKey)).thenReturn(key);
        Mockito.when(key.deriveTwiceAndDecryptLocal(encryptedBytes)).thenThrow(new IdpsException());
        String result = idpsDecryptorimpl.decrypt(encryptedBytes);
        Assert.assertNull(result);
    }
}
  • What field is null? That's where your problem is. I'm guessing `key` since you don't initialize it and have commented out `@Mock` – Christopher Schneider Jul 11 '22 at 15:37
  • Key and IdpsClient both are interfaces, also tried with @Mock or without Mock both cases its making null on this "idpsClient.newKeyHandleLatest(idpsKey))" Mockito.when(idpsClient.newKeyHandleLatest(idpsKey)).thenReturn(key); Mockito.when(idpsClient.newKeyHandleLatest(Mockito.any())).thenReturn(key); – Satish Prasad Jul 12 '22 at 03:47

0 Answers0