1

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

3
  • Maybe the Mockito version Gradle is using is different from the one used by IntelliJ. Can you check if the versions are the same and reload the Gradle dependencies (e.g. by using ./gradlew build --refresh-dependencies) Commented Jan 19, 2023 at 21:08
  • Please show the full Gradle file so that others can replicate the problem. Commented Jan 20, 2023 at 6:52
  • I solved this problem, don't know the exact reason why it surfaced. instead of 'Mock' I used Spy and pass all the dependencies via reflections. The confusion was that when I was using 'Mock' the null pointer was in the instance creation itself (Maybe because of mocking public interface), when Changed to Spy the null pointer was in all variables and used inside. So Later found this hack to solve my problem. Actually, this issue surfaced when I was upgrading my spring version Commented Jan 21, 2023 at 6:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.