当前位置: 首页 > 知识库问答 >
问题:

使用带mockito的powermock时出现空指针异常

吴安和
2023-03-14

我试图用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)

共有1个答案

董翰墨
2023-03-14

代替when(abcdao.getsession()).thenreturn(any(Session.class));,doSession mockedSession=mock(Session.class);when(abcdao.getsession()).thenreturn(mockedSession);

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

 类似资料:
  • > 单击 受保护得空onPrepareDialog(int id,Dialog Dialog) 受保护的对话框onCreateDialog(int id) 如果我对timepickerdialog代码做了什么错误,请告诉我。

  • 问题内容: MyClass.java: 编译MyClass.java文件的SimpleCompileTest.java的清单。 SimpleCompileTest.java: 我正在执行SimpleCompileTest类并获取NullPointerException。ToolProvider.getSystemJavaCompiler()返回null。有人可以告诉我代码有什么问题吗 问题答案:

  • 以下是在sun.reflect.nativeMethodAccessorImpl.Invoke0(本机方法)在sun.reflect.nativeMethodAccessorImpl.Invoke(未知源)在sun.reflect.NativeMethodAccessorImpl.Invoke(未知源)在java.lang.Reflect.Method.Invoke(未知源)在com.codena

  • 我试图存根这个方法:QueryUtils.to顺序(排序,根,构建器)和我正在做 但它进入queryUtils方法体,它会说Sort为null,并抛出一个NPE。但是,当它是存根时,为什么需要进入方法体?我以前没有遇到过这个问题,我认为它不应该关心该方法的内部逻辑是什么。

  • 我使用了页面对象模型设计模式和Cucumber。为此,我创建了两个名为和的页面,但在运行脚本时,即使我已经使用pagefactory初始化webelements,也会出现空指针异常,请查看以下代码: 文摘: 登录页面: 然后我有一个类,在这里我调用上面的登录类方法。浏览器打开良好,url被命中,但当它进入l方法时,它会在第一行本身抛出异常。 我还有一个TestRunner类,其中我粘贴了我的步骤定

  • 问题内容: 在查询Android日历中的事件之前,我需要检查权限。为此,Android studio警告我在查询之前需要先进行检查。自动生成的代码如下: 尝试运行它时,出现以下错误: 尝试 在空对象引用上调用虚拟方法’int android.content.Context.checkPermission(java.lang.String,int,int)’ 因此很明显,此时某些东西为空,我试图以不