Mockito Junit5 NullPointerException - mocking public interface

297 Views Asked by At

Java Class

public class SendNotification {

    public  NotificationProperty config;
    public  PostDbClient postDbClient;
    public  ObjectMapper objectMapper;
    public  AmazonSNS snsClient;
    public  NotificationParser notificationParser;
}

Test Class

@ExtendWith(MockitoExtension.class)
public class SendNotificationTest {

    @Mock
    AmazonSNS amazonSNS;

    @Mock
    public  NotificationProperty config;

    @Mock
    public  PostDbClient postDbClient;

    @Mock
    public  ObjectMapper objectMapper;

    @Mock
    public  NotificationParser notificationParser;


    @InjectMocks
    SendNotification sendNotification;

    @BeforeEach
    public void createMocks() {
       // MockitoAnnotations.openMocks(this);

    }

    @Test
    public void shouldSendNotification() {

        System.out.println("Step1");
        System.out.println(config);
        System.out.println("Step2");
        System.out.println(postDbClient);
        System.out.println("Step3");
        System.out.println(objectMapper);
        System.out.println("Step4");
        System.out.println(notificationParser);
        System.out.println("Step5");
        System.out.println(amazonSNS);
        System.out.println("Step6");
        Mockito.when(amazonSNS.publish(any())).thenReturn(new PublishResult());
        // doReturn(new PublishResult()).when(amazonSNS).publish(new PublishRequest());
       
    }

Exception

java.lang.NullPointerException
    at java.base/java.lang.reflect.Method.getParameterTypes(Method.java:311)
    at org.mockito.internal.creation.DelegatingMethod.<init>(DelegatingMethod.java:20)
    at org.mockito.internal.invocation.DefaultInvocationFactory.createMockitoMethod(DefaultInvocationFactory.java:81)
    at org.mockito.internal.invocation.DefaultInvocationFactory.createInvocation(DefaultInvocationFactory.java:60)
    at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:83)
    at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:56)
    at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor$DispatcherDefaultingToRealMethod.interceptSuperCallable(MockMethodInterceptor.java:145)
    at com.expedia.ers.singleview.glasscase.client.GlassCaseClientFactory$MockitoMock$KUPO0Qd7.toString(Unknown Source)
    at java.base/java.lang.String.valueOf(String.java:2951)
    at java.base/java.io.PrintStream.print(PrintStream.java:759)
    at org.gradle.internal.io.LinePerThreadBufferingOutputStream.println(LinePerThreadBufferingOutputStream.java:230)


Output

Step1
config
Step2
postDbClient
Step3
objectMapper
Step4
notificationParser
Step5

Dependencies

        springBootVersion = '2.7.7'

        testImplementation group: 'io.springfox', name: 'springfox-staticdocs', version: '2.6.1'
        testImplementation 'org.junit.jupiter:JUnit-jupiter:5.6.2'
        testImplementation 'org.mockito:mockito-inline:5.0.0'

test {
    useJUnitPlatform()
    dependsOn cleanTest
    testLogging.showStandardStreams = true
    maxParallelForks = 1
}

I am not able to mock any public interface in my whole spring boot project. Test Case in passing in intelij, failing while running gradle clean buid

0

There are 0 best solutions below