简体   繁体   中英

NullPointerException in while doing mockito unit test

I am new to mockito Junit testing. This one is my main class which I want to test: import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.springframework.stereotype.Component;

@Component
public class IlinqChecksumCalculator {

    private static Logger DATA_LOADER_CHECKSUM_CALCULATOR_LOGGER = Logger.getLogger(IlinqChecksumCalculator.class);

    public String calculateCheckSum(String rfsdata) throws IOException {

        System.out.println(rfsdata);
        String checkSumValue = null;
        if (StringUtils.isNotBlank(rfsdata)) {
            try {
                // Create MessageDigest object for MD5
                MessageDigest digest = MessageDigest.getInstance("MD5");

                // Update input string in message digest
                digest.update(rfsdata.getBytes(), 0, rfsdata.getBytes().length);

                // Converts message digest value in base 16 (hex)
                checkSumValue = new BigInteger(1, digest.digest()).toString(16);

            } catch (NoSuchAlgorithmException exception) {
                DATA_LOADER_CHECKSUM_CALCULATOR_LOGGER.error(
                        "Error in determineInputCheckSum() method during calculation of checksum for Input JSON String for ",
                        exception);
            }
        }
        System.out.println("Final checksum value is:" + checkSumValue);
        return checkSumValue;
    }

}

This one is my test class:

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import java.io.IOException;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class IlinqChecksumCalculatorTest {

    private IlinqChecksumCalculator ilinqCheckSum;

    @Before
    public void setUp() throws Throwable {

        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testCheckSum() throws IOException {

        when(ilinqCheckSum.calculateCheckSum("abcde")).thenReturn("defgh");

        assertEquals("defgh", ilinqCheckSum.calculateCheckSum("abcde"));

    }
}

I am getting a null pointer exception.

Just to answer your question: to handle ilinqCheckSum as mock, you shouuld annotate it with @Mock . But here you should not use mockito! You want to test IlinqChecksumCalculator and not a mock! You should create a real instance of it and inject the dependendencies as mock if necessary.

By mocking calculateCheckSum method you are not covering any code in your unit test. I think you should not use Mock here. Try below test method.

 public void testCheckSum() throws IOException {

       String result =  ilinqCheckSum.calculateCheckSum("abcde")
        assertNotNull(result );

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM