用Powermock和Mockito

用Powermock和Mockito

我试图用Powermock和Mockito编写一个JUnit测试用例。我从下面的测试案例中调用一种方法。此方法依次调用method1和那个method2。我已经添加了@RunWith和@PrepareForTest批注。

//这是我的测试类:

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;

    }


错误跟踪:

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)

最佳答案

代替when(AbcDAO.getSession()).thenReturn(any(Session.class));,执行
Session mockedSession = mock(Session.class);when(AbcDAO.getSession()).thenReturn(mockedSession);

any()只是一个模式匹配器。 mock()将使用给定的类创建一个模拟对象。

关于java - 使用Powermock和Mockito的空指针异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31647522/

10-09 04:48