2

I am having project in spring-mvc. I wrote a test case in mockito, Below is the code:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext.xml"})
@Configurable
public class ABCControllerTest {

    @InjectMocks
    CustomerController instance;

    @Mock
    Service paymentService;

    @Mock
    OfferService offerService;

    @Mock
    CategoryRepository categoryRepository;

    @Mock
    Category category;

    @Mock
    RuleService ruleService ;

    private MockMvc mockMvc;


    public PaymentCustomerControllerTest() {
    }

   @Before
    public void setup() {

        // this must be called for the @Mock annotations above to be processed
        // and for the mock service to be injected into the controller under
        // test.
        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders.standaloneSetup(instance).build();
    }

    @Test
    public void testGetNewCashback() throws Exception {
        int a= 100; 
        int b=200;
      //  System.out.print(mockMvc);
       try{
           mockMvc.perform(
                get("repo/getTotalSum?Id=306187C35A5C98A1B01E3DCDE8BAC1DA&visitId=306187C35A5C98A1B01E3DCDE8BAC1DA&mobileStatus=0"))
                .andExpect(MockMvcResultMatchers.status().isOk());
       }catch(Exception ex){
           System.out.print(ex.getMessage());
       }
       a = 100;
    }

    @Test
    public void testGetNewCashback() throws Exception {

        ResultDTO result = instance.getTotalSum(Id, visitId, mobileStatus);
        assertEquals(result, expResult);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
   }

}

and this is my pom file:

<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>com.ssww.cust</groupId>
    <artifactId>cust</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>

    <name>Settlement</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <skipTests>true</skipTests>

        <spring.core.version>3.1.2.RELEASE</spring.core.version>
        <spring.security.core.version>3.1.0.RELEASE</spring.security.core.version>
        <spring.data.jpa.version>1.1.0.RELEASE</spring.data.jpa.version>

        <mysql.connector.version>5.1.21</mysql.connector.version>
        <hibernate.version>4.1.6.Final</hibernate.version>
        <c3p0.version>0.9.1.2</c3p0.version>

        <querydsl.version>2.7.0</querydsl.version>

        <log4j.version>1.2.17</log4j.version>

        <cglib.version>2.2.2</cglib.version>
        <aspectj.version>1.6.12</aspectj.version>
        <slf4j.version>1.6.1</slf4j.version>
        <javax.servlet-api.version>2.5</javax.servlet-api.version>
        <javax.jstl-taglibs.version>1.1.2</javax.jstl-taglibs.version>
        <jackson.version>1.9.3</jackson.version>

        <!-- Testing -->
        <mockito.version>1.8.5</mockito.version>
        <junit.version>4.8.2</junit.version>

        <!-- Plugins -->
        <maven.copy.plugin.version>0.2.3</maven.copy.plugin.version>
        <maven.compiler.plugin.version>2.3.2</maven.compiler.plugin.version>
        <maven.apt.plugin.version>1.0</maven.apt.plugin.version>
        <netbeans.hint.deploy.server>Tomcat</netbeans.hint.deploy.server>
    </properties>



    <dependencies>
        <!-- javamelody-core >
        <dependency>
                <groupId>net.bull.javamelody</groupId>
                <artifactId>javamelody-core</artifactId>
                <version>1.49.0</version>
        </dependency-->
        <!-- itext, option to add PDF export -->
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.7</version>
            <exclusions>
                <exclusion>
                    <artifactId>bcmail-jdk14</artifactId>
                    <groupId>bouncycastle</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>bcprov-jdk14</artifactId>
                    <groupId>bouncycastle</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>bctsp-jdk14</artifactId>
                    <groupId>bouncycastle</groupId>
                </exclusion>
            </exclusions>
        </dependency>

<!--        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.0.0.RELEASE</version>
        </dependency>-->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>
        <!--        <dependency>
            <groupId>com.ocpsoft.rewrite</groupId>
            <artifactId>rewrite-impl-servlet</artifactId>
            <version>1.0.0.Alpha3</version>
        </dependency>-->
        <!--        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.7</version>
        </dependency>-->

        <!-- Spring Core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.core.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.core.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.core.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.core.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.core.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring.core.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.core.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <!-- A seamless aspect-oriented extension to the Java programming language -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>

        <!-- Cglib is a powerful, high performance and quality Code Generation
        Library, It is used to extend JAVA classes and implements interfaces at runtime. -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>${cglib.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>

        <!-- The JavaServer Pages Standard Tag Library (JSTL) encapsulates, as
        simple tags, core functionality common to many JSP applications. -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${javax.jstl-taglibs.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>${javax.jstl-taglibs.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-hibernate4</artifactId>
            <version>2.1.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.4.0</version>
        </dependency>

        <!-- Data Mapper package is a high-performance data binding package built
        on Jackson JSON processor -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>${jackson.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <!-- Jackson is a high-performance JSON processor (parser, generator) -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>${jackson.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <!-- Logger -->


        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>



        <!-- The Simple Logging Facade for Java or (SLF4J) serves as a simple facade
        or abstraction for various logging frameworks, e.g. java.util.logging, log4j
        and logback, allowing the end user to plug in the desired logging framework
        at deployment time. -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <!-- Spring Security -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${spring.security.core.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring.security.core.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring.security.core.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>${spring.security.core.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <!-- Spring Data JPA -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>${spring.data.jpa.version}</version>
        </dependency>


        <!-- Database pooling -->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>${c3p0.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <!-- MySQL Connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.connector.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <!-- Hibernate and JPA -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <!-- QueryDSL -->
        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-apt</artifactId>
            <version>${querydsl.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-jpa</artifactId>
            <version>${querydsl.version}</version>
        </dependency>

        <!-- Javax Servlet. This needs to be included for runtime only! -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>${javax.servlet-api.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- Testing dependencies -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
            <type>jar</type>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.0.6.RELEASE</version>
            <type>jar</type>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-core</artifactId>
            <version>1.4</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20090211</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.8</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>

        <dependency>
            <groupId>org.modeshape</groupId>
            <artifactId>modeshape-sequencer-msoffice</artifactId>
            <version>3.1.2.Final</version>
            <type>jar</type>
        </dependency>
        <!--        <dependency>
            <groupId>org.xhtmlrenderer</groupId>
            <artifactId>core-renderer</artifactId>
            <version>R8</version>
            <type>jar</type>
        </dependency>-->
        <dependency>
            <groupId>org.xhtmlrenderer</groupId>
            <artifactId>core-renderer</artifactId>
            <version>R8</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.0.8</version>
        </dependency>
        <dependency>

            <groupId>net.sf.supercsv</groupId>
            <artifactId>super-csv</artifactId>
            <version>2.1.0</version>
            <type>jar</type>
        </dependency>


        <dependency>
            <groupId>com.mixpanel</groupId>
            <artifactId>mixpanel-java</artifactId>
            <version>1.4.2</version>
        </dependency>
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.50</version>
        </dependency>
<!--        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.1</version>
            <scope>test</scope>
        </dependency>-->

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
            <type>jar</type>
        </dependency>
    </dependencies>

    <build>
        <finalName>settlement</finalName>
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>7.1.2.v20100523</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.0</version>
        <configuration>
          <skipTests>false</skipTests>
        </configuration>
      </plugin>
            <plugin>
                <groupId>com.mysema.maven</groupId>
                <artifactId>maven-apt-plugin</artifactId>
                <version>1.0.3</version><!--$NO-MVN-MAN-VER$ -->
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>src/main/java</outputDirectory>
                            <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.5.7.201204190339</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat6-maven-plugin</artifactId>
                    <configuration>
                        <addContextWarDependencies>true</addContextWarDependencies>
                    </configuration>
                    <version>2.1</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <configuration>
                        <ajpPort>8391</ajpPort>
                        <port>8392</port>
                        <addContextWarDependencies>true</addContextWarDependencies>
                    </configuration>
                    <version>2.1</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <repositories>

        <!-- For main Spring releases -->
        <repository>
            <id>org.springframework.maven.release</id>
            <name>Spring Maven Release Repository</name>
            <url>http://maven.springframework.org/release</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>

        <!--
                <repository>
                    <id>jboss</id>
                    <name>JBoss repository</name>
                    <url>https://repository.jboss.org/nexus/content/repositories/releases</url>
                </repository>
        -->

    </repositories>

</project>

But on building the project , i am getting this exception:

java.lang.NullPointerException
    at ABCControllerTest.testGetNewCashback(ABCControllerTest.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.surefire.battery.AbstractBattery.executeTestMethod(AbstractBattery.java:124)
    at org.codehaus.surefire.battery.AbstractBattery.executeTestMethods(AbstractBattery.java:70)
    at org.codehaus.surefire.battery.AbstractBattery.execute(AbstractBattery.java:49)
    at org.codehaus.surefire.battery.JUnitBattery.execute(JUnitBattery.java:224)
    at org.codehaus.surefire.Surefire.executeBattery(Surefire.java:204)
    at org.codehaus.surefire.Surefire.run(Surefire.java:153)
    at org.codehaus.surefire.Surefire.run(Surefire.java:77)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at the value of mock is getting null, so getting null pointer exception. I googled out, changed spring version to 4.0.0Release, still same.

Any idea? Thanks.

After changing to standalone:i am getting java.lang.Exception: mvc is having null value.this is my code

    @RunWith(MockitoJUnitRunner.class)
@ContextConfiguration({"classpath:applicationContext.xml"})
@WebAppConfiguration
public class PaymentCustomerControllerTest {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;



   @Before
    public void setUp() {
            MockitoAnnotations.initMocks(this);
            CustomerController instance = new CustomerController();

            mockMvc = MockMvcBuilders.standaloneSetup(instance).build();
    }


    @Test
    public void testGetNewCashback() throws Exception {
       if(mockMvc==null){
            throw new Exception("mvc is having null value");
        }
           mockMvc.perform(
                get("info/getInfo?Id=306&visitId=30618&mobileStatus=0"))
                .andExpect(MockMvcResultMatchers.status().isOk());

    }
0

1 Answer 1

-1

If you want to use Mockito for your tests, you must use MockitoJUnitRunner not SpringJUnit4ClassRunner

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but i am getting java.lang.Exception: mvc is having null value.
You can not use spring related annotations/configuration when running the test case with MockitoJunitRunner. @ContextConfiguration, MockMvc and @WebAppConfiguration should not be used in your test case. See this post stackoverflow.com/questions/15068740/…
MockitoAnnotations.initMocks(this) is the equivalent if you want to use a different runner

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.