Mockito with Junit 5 not Mocking objects while running test cases

Viewed 68

I am working on my project and in my project I have Junit 5 and Mockito for Mocking and testing purpose, But when I am running my test cases, Mockito is not Mocking my objects. I tried to run in debug form too, but still it is not Mocking objects (as can be seen in image). I am sharing my class and POM file to make it better for everyone to understand. After looking at my POM file many of you might be thinking that why I have not added Jupitar dependency so the answer to that is actually spring dependency already contains Jupitar dependency with it so I didn't added it separately.

POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>,xyz</groupId>
    <artifactId>xys</artifactId>
    <version>3.9.34</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.2</version>
        <relativePath/>
    </parent>

    <name>tirebatteryservice</name>
    <description>tirebatteryservice</description>

    <properties>
        <tbc.version>1.0.0</tbc.version>
        <jms.version>1.1</jms.version>
        <java.version>1.8</java.version>
        <lombok.version>1.18.4</lombok.version>
        <commons-lang3.version>3.8.1</commons-lang3.version>
        <fastjson.version>2.0.12</fastjson.version>
        <spring.boot.version>2.7.2</spring.boot.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <org.apache.commons.collections.version>3.2.1</org.apache.commons.collections.version>
        <jacoco.maven.plugin.version>0.7.8</jacoco.maven.plugin.version>
        <testng.version>6.8</testng.version>
        <azure.log.version>2.2.1</azure.log.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <elasticsearch.version>6.6.1</elasticsearch.version>
        <ejml.version>0.23</ejml.version>
        <powermock.version>2.0.0-beta.5</powermock.version>
        <log4j2.version>2.16.0</log4j2.version>
        <tbc-common.version>1.8.0-SNAPSHOT</tbc-common.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.4</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
            <exclusions>
                <exclusion>
                    <groupId>io.github.classgraph</groupId>
                    <artifactId>classgraph</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>io.github.classgraph</groupId>
            <artifactId>classgraph</artifactId>
            <version>4.8.108</version>
        </dependency>

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>net.gpedro.integrations.slack</groupId>
            <artifactId>slack-webhook</artifactId>
            <version>1.4.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
            <version>${spring.boot.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <!-- Spring boot starter test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.jms</groupId>
            <artifactId>jms</artifactId>
            <version>${jms.version}</version>
        </dependency>


        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
        <!--For Open API V3 Swagger specification-->
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.4.4</version>
            <exclusions>
                <exclusion>
                    <groupId>io.github.classgraph</groupId>
                    <artifactId>classgraph</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons-lang3.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.directory.studio</groupId>
            <artifactId>org.apache.commons.collections</artifactId>
            <version>${org.apache.commons.collections.version}</version>
        </dependency>

        <!-- jacoco -->
        <dependency>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.maven.plugin.version}</version>
        </dependency>

        <!--enables unit tests to run-->
        <dependency>
            <groupId>org.jacoco</groupId>
            <artifactId>org.jacoco.agent</artifactId>
            <classifier>runtime</classifier>
            <scope>test</scope>
            <version>${jacoco.maven.plugin.version}</version>
        </dependency>

        <!-- Application insights -->
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>applicationinsights-spring-boot-starter</artifactId>
            <version>2.5.1</version>
        </dependency>

        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>applicationinsights-logging-log4j2</artifactId>
            <version>${azure.log.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <version>${spring.boot.version}</version>
            <scope>compile</scope>
        </dependency>
        <!-- search -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>${elasticsearch.version}</version>
        </dependency>
        <!-- stanford -->
        <dependency>
            <groupId>com.googlecode.efficient-java-matrix-library</groupId>
            <artifactId>ejml</artifactId>
            <version>0.23</version>
        </dependency>
        <!-- sonar -->
        <dependency>
            <groupId>org.codehaus.sonar</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>5.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-core</artifactId>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>

        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.13</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.13</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.196</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.sams.member.platform.util</groupId>
            <artifactId>ccpa-access</artifactId>
            <version>0.1.14</version>
        </dependency>

        <dependency>
            <groupId>com.github.kstyrc</groupId>
            <artifactId>embedded-redis</artifactId>
            <version>0.6</version>
        </dependency>

        <!-- Azure Key Vault Dependency  -->
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-keyvault</artifactId>
            <version>1.2.2</version>
        </dependency>

        <!--TBC COMMON-->
        <dependency>
            <groupId>com.sams.clubops.tbc</groupId>
            <artifactId>tbc-common</artifactId>
            <version>${tbc-common.version}</version>
        </dependency>

        <dependency>
            <groupId>net.sf.barcode4j</groupId>
            <artifactId>barcode4j</artifactId>
            <version>2.1</version>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>4.6.1</version>
            <scope>test</scope>
        </dependency>

        <!--Mockito dependency-->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-inline</artifactId>
            <version>3.8.0</version>
            <scope>test</scope>
        </dependency>

<!--        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>4.1.0</version>
            <scope>test</scope>
        </dependency>-->



    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
                <configuration>
                    <fork>true</fork>
                    <mainClass>com.sams.clubops.Application</mainClass>
                </configuration>
            </plugin>


            <plugin>
                <groupId>org.sonarsource.scanner.maven</groupId>
                <artifactId>sonar-maven-plugin</artifactId>
                <version>3.5.0.1254</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
                <configuration>
                    <testFailureIgnore>false</testFailureIgnore>
                </configuration>
            </plugin>


            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/resources/docker</directory>
                                    <includes>
                                        <include>Dockerfile</include>
                                        <include>startup.sh</include>
                                        <include>sshd_config</include>
                                        <include>ssh_setup.sh</include>
                                        <include>proxy-intranet-ref-app.conf</include>
                                        <include>nginx-rproxy-secure.conf</include>
                                    </includes>
                                </resource>
                                <resource>
                                    <directory>src/main/resources/printTemplates</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.7.201606060606</version>
                <configuration>
                    <includes>
                        <include>**/com/sams/clubops/**</include>
                    </includes>
                    <excludes>
                        <exclude>*</exclude>
                    </excludes>
                </configuration>
                <executions>
                    <execution>
                        <id>default-instrument</id>
                        <goals>
                            <goal>instrument</goal>
                        </goals>
                    </execution>

<!--                    Restores original classes as they were before offline instrumentation.-->
                    <execution>
                        <id>default-restore-instrumented-classes</id>
                        <goals>
                            <goal>restore-instrumented-classes</goal>
                        </goals>
                    </execution>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jacoco-check</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <rule>
                                    <element>BUNDLE</element>
                                    <limits>
                                        <limit>
                                            <counter>LINE</counter>
                                            <value>COVEREDRATIO</value>
                                            <minimum>0.50</minimum>
                                        </limit>
                                    </limits>
                                </rule>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.coveo</groupId>
                <artifactId>fmt-maven-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <displayLimit>10</displayLimit>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Test Class:

package com.sams.clubops.salesforce;

import static org.junit.jupiter.api.Assertions.*;

import com.alibaba.fastjson.JSONObject;
import com.sams.clubops.salesforce.client.SalesForceClient;
import com.sams.clubops.salesforce.config.SalesForceProperties;
import com.sams.clubops.salesforce.service.SFAuthTokenService;
import com.sams.clubops.salesforce.service.SFSendEmailService;
import com.sams.clubops.tbc.model.BaseOrder;
import com.sams.clubops.tbc.model.EmailType;
import java.io.IOException;
import junit.utils.SFTestsHelper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.*;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class SalesForceClientTest {

  @Spy @InjectMocks private SalesForceClient salesForceClient;

  @Mock private SFAuthTokenService sfAuthTokenService;

  @Mock private SFSendEmailService sfSendEmailService;

  @Mock private SalesForceProperties salesForceProperties;

  @BeforeEach
  public void init() {
    MockitoAnnotations.openMocks(this);
  }


  @Test
  public void sendTest() throws IOException {
    String baseOrderJson = SFTestsHelper.loadJsonString("jsons", "quote.json");

    BaseOrder baseOrder = JSONObject.parseObject(baseOrderJson, BaseOrder.class);

    Mockito.when(sfAuthTokenService.getSalesforceAuthenticationToken()).thenReturn("sample-token");
    Mockito.when(sfSendEmailService.sendSFEmail(Mockito.anyString(), Mockito.anyString()))
        .thenReturn("sample-true-response");
    Mockito.when(sfSendEmailService.isEmailSuccessful(Mockito.eq("sample-true-response")))
        .thenReturn(true);
    Mockito.when(salesForceProperties.getEnvKey()).thenReturn("TBC_OrderConf_Quote_DEV");

    assertTrue(salesForceClient.send(EmailType.ORDER, baseOrder));

    Mockito.when(sfSendEmailService.sendSFEmail(Mockito.anyString(), Mockito.anyString()))
        .thenReturn("sample-false-response");
    Mockito.when(sfSendEmailService.isEmailSuccessful(Mockito.eq("sample-false-response")))
        .thenReturn(false);

    assertFalse(salesForceClient.send(EmailType.ORDER, baseOrder));
  }
}

This picture I took after running my code in debug mode to explain that objects are not getting mocked This picture I took after running my code in debug mode to explain that objects are not getting mocked

1 Answers

Verify that mockito is correctly added to your classPath in order to be used .

Related