我正在尝试用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)
答案 0 :(得分:2)
而不是when(AbcDAO.getSession()).thenReturn(any(Session.class));
Session mockedSession = mock(Session.class);
when(AbcDAO.getSession()).thenReturn(mockedSession);
any()
只是一个模式匹配器。 mock()
将使用给定的类创建一个模拟对象。
答案 1 :(得分:1)
Session mockSession = mock(Session.class);
// Mock a static method as below
PowerMockito.mockStatic(AbcDAO.class);
when(AbcDAO.getSession()).thenReturn(mockSession);
如果您在adminServices.registerNewAdmin()中调用会话对象的任何方法 在上面的mockSession对象上设置所需的期望。
答案 2 :(得分:-1)
公共课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;
}
静态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();
}