2

I am trying to write a JUnit test case with Powermock and Mockito. I am calling a method from below test case. This method inturn calls from method1 and that call method2. I have already added @RunWith and @PrepareForTest annotations.

//this my test class:

package com.company.Abc.helpdesk.service;

import static org.junit.Assert.*;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.company.vtest.login.dao.AbcDAO;
import com.company.vtest.login.entity.Login;

import static org.mockito.Mockito.*;

import static org.powermock.api.mockito.PowerMockito.when;

/**
 * @author Joginder
 * 
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Session.class })
public class AdminServicesTestNikhil {

    @Test
    public void testRegisterNewAdmin() {
        // Declare variables to be passed as params
        Login rna = new Login(8005, "admin");

        // Mock classes

        AbcDAO mockDAO = mock(AbcDAO.class);
        PowerMockito.mockStatic(Session.class);

        // Mock method calls

        when(AbcDAO.getSession()).thenReturn(any(Session.class));

        // Call test method
        AdminServices adminServices = new AdminServices();
        assertEquals(adminServices.registerNewAdmin(rna), 0);

        // Verify method calls

        // Verify results
        fail("Not yet implemented");
    }

}


//here is the private method: 

private static SessionFactory buildSessionFactory() {
        // build session factory;
        // Read from cfg
        SessionFactory sessionFactory;
        sessionFactory = new AnnotationConfiguration()
                .addAnnotatedClass(com.company.Abc.login.entity.Login.class)
                .addAnnotatedClass(
                        com.company.Abc.assessment.entity.CreatedAsmnt.class)
                .addAnnotatedClass(
                        com.company.Abc.assessment.entity.QuesPerAsmnt.class)
                .addAnnotatedClass(
                        com.company.Abc.assessment.entity.AnsOptions.class)
                .addAnnotatedClass(
                        com.company.Abc.assessment.entity.TestTaker.class)
                .addAnnotatedClass(
                        com.company.Abc.question.entity.QuestionBank.class)
                .addAnnotatedClass(
                        com.company.Abc.question.entity.QuestionDetails.class)
                .addAnnotatedClass(
                        com.company.Abc.question.entity.AnswerDetails.class)
                .addAnnotatedClass(
                        com.company.Abc.assessment.entity.PanelRetrieval.class)
                .addAnnotatedClass(
                        com.company.Abc.assessment.entity.ViewQA.class)
                .addAnnotatedClass(
                        com.company.Abc.question.entity.Master.class)
                .addAnnotatedClass(
                        com.company.Abc.question.entity.MasterT.class)
                .addAnnotatedClass(
                        com.company.vtest.question.entity.MasterForm.class)
                .addAnnotatedClass(ResultAnnotated.class)
                .addAnnotatedClass(Candidate.class)
                .addAnnotatedClass(AssessmentTaken.class)
                .addAnnotatedClass(Section.class)
                .addAnnotatedClass(SelTechInfo.class)
                .addAnnotatedClass(AsmntSchedule.class).configure()

                .buildSessionFactory();

        return sessionFactory;

    }

error trace:

java.lang.NullPointerException
    at com.company.Abc.login.dao.AbcDAO.getSession(AbcDAO.java:83)
    at com.company.Abc.helpdesk.service.AdminServicesTest.testRegisterNewAdmin(AdminServicesTest.java:54)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
6
  • 1
    Which is line 83 in AbcDAO.java? Commented Jul 27, 2015 at 7:45
  • Please also show the code where the error is thronw (com.company.Abc.login.dao.AbcDAO.getSession()) Commented Jul 27, 2015 at 7:46
  • is getSession of AbcDAO a static method ? why didn't you set expectation on mockDAO like when(mockDAO .getSession()).thenReturn(any(Session.class)); ? Commented Jul 27, 2015 at 7:54
  • What is when(AbcDAO.getSession()).thenReturn(any(Session.class)); supposed to do? Commented Jul 27, 2015 at 8:10
  • session = sessionFactory.openSession(); its the line 83 in AbcDAO.java Commented Jul 27, 2015 at 8:46

3 Answers 3

3

Instead of when(AbcDAO.getSession()).thenReturn(any(Session.class));, do Session mockedSession = mock(Session.class); when(AbcDAO.getSession()).thenReturn(mockedSession);

any() is just a pattern matcher. mock() will create a mock object using the given class.

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

Comments

1
Session mockSession = mock(Session.class);
// Mock a static method as below
PowerMockito.mockStatic(AbcDAO.class); 
when(AbcDAO.getSession()).thenReturn(mockSession);

If you any method calls on session object in adminServices.registerNewAdmin() set the required expectations on above mockSession object.

Comments

-1

public class AbcDAO {

private static SessionFactory sessionFactory=null; 
private static Session session;
public static Connection con = null;



private static SessionFactory buildSessionFactory() {
    // build session factory;
    // Read from cfg
    SessionFactory sessionFactory;
    sessionFactory = new AnnotationConfiguration()
            .addAnnotatedClass(com.company.Abc.login.entity.Login.class)
            .addAnnotatedClass(
                    com.company.Abc.assessment.entity.CreatedAsmnt.class)
            .addAnnotatedClass(
                    com.company.Abc.assessment.entity.QuesPerAsmnt.class)
            .addAnnotatedClass(
                    com.company.Abc.assessment.entity.AnsOptions.class)
            .addAnnotatedClass(
                    com.company.Abc.assessment.entity.TestTaker.class)
            .addAnnotatedClass(
                    com.company.Abc.question.entity.QuestionBank.class)
            .addAnnotatedClass(
                    com.company.Abc.question.entity.QuestionDetails.class)
            .addAnnotatedClass(
                    com.company.Abc.question.entity.AnswerDetails.class)
            .addAnnotatedClass(
                    com.company.Abc.assessment.entity.PanelRetrieval.class)
            .addAnnotatedClass(
                    com.company.Abc.assessment.entity.ViewQA.class)
            .addAnnotatedClass(
                    com.company.Abc.question.entity.Master.class)
            .addAnnotatedClass(
                    com.company.Abc.question.entity.MasterT.class)
            .addAnnotatedClass(
                    com.company.vtest.question.entity.MasterForm.class)
            .addAnnotatedClass(ResultAnnotated.class)
            .addAnnotatedClass(Candidate.class)
            .addAnnotatedClass(AssessmentTaken.class)
            .addAnnotatedClass(Section.class)
            .addAnnotatedClass(SelTechInfo.class)
            .addAnnotatedClass(AsmntSchedule.class).configure()

            .buildSessionFactory();

    return sessionFactory;

}

static SessionFactory sessionFactory;

public static Session getSession() throws HibernateException {

    // sessionFactory = AbcDAO.buildSessionFactory();

    try {
        if (sessionFactory != null)
            session = sessionFactory.getCurrentSession();
        else {
            sessionFactory = AbcDAO.buildSessionFactory();
            session = sessionFactory.openSession();
        }
    } catch (org.hibernate.HibernateException he) {

        session = sessionFactory.openSession();
    }
    return session;

}

public void save(Object object) {
    session.beginTransaction();
    session.save(object);
    session.close();
}

Comments

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.