如何在使用Spring和Hibernate的项目中模拟SessionFactory或PowerMockito的Sessi



我正在处理一个使用Spring和Hibernate的项目。我有一个包含SessionFactory的抽象DAO。我需要为我的单元测试模拟SessionFactory或Session。但到目前为止,我还没有成功。下面是代码。有人知道吗?非常感谢。

这是我的AbstractDAO

public abstract class AbstractDAO {
    protected JdbcTemplate jdbcTemplate;
    protected SessionFactory sessionFactory;
    protected NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    ...
    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
    }}

这是我的ConcreteDAO

public class LoginDAO extends AbstractDAO implements InitializingBean {
    ...
    public User getLoggedinUserByUserid(Long userid){
            log.info("in getLoggedinUserByUserid"); 
            User result = null;
            Session session = sessionFactory.openSession();
            try {
            session.beginTransaction();
            result = (User) session.get(User.class, userid);
            session.getTransaction().rollback();
            session.close();
            } catch (Exception e) {
            log.error(e,e);
            session.getTransaction().rollback();
            session.close();
            }
    return result;
     }
...}

这是我的测试班

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "LoginDAOTest-context.xml" })
@PrepareForTest({SessionFactory.class, Session.class, Transaction.class})
public class LoginDAOTest2 extends BaseDAO {
    private static final Logger log = Logger.getLogger(LoginDAOTest2.class);
    @Rule
public PowerMockRule rule = new PowerMockRule();
private LoginDAO loginDAO = new LoginDAO();
private SessionFactory mockedSessionFactory;
private Session mockedSession;
private Transaction mockedTransaction;
@Autowired
        public void setSessionFactory(SessionFactory sessionFactoryCore) {
                mockedSessionFactory =  PowerMockito.mock( sessionFactoryCore.getClass());
                mockedSession = PowerMockito.mock(Session.class);
                PowerMockito.doReturn(mockedSession).when(mockedSessionFactory).openSession();
                PowerMockito.doReturn(mockedTransaction).when(mockedSession).beginTransaction();
  loginDAO.setSessionFactory(this.mockedSessionFactory);
        }
@Test
     public void shouldRollbackInGetLoggedinUserByUseridWhenSessionThrowsException() {
              // Given
            PowerMockito.doThrow(new Exception()).when(mockedSession).get(User.class, 12L);
            // When
            loginDAO.getLoggedinUserByUserid(12L);
            // Then
            verify(mockedTransaction, times(1)).rollback();
    }
}

这是junit的结果。


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.aeon.ps.dao.LoginDAOTest2': Injection of autowired dependencies failed; nested exception is java.lang.NoClassDefFoundError: org/mockito/internal/MockitoInvocationHandler
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:374)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    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)
Caused by: java.lang.NoClassDefFoundError: org/mockito/internal/MockitoInvocationHandler
    at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:138)
    at com.aeon.ps.dao.LoginDAOTest2.setSessionFactory(LoginDAOTest2.java:61)
    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.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:586)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
    ... 26 more
Caused by: java.lang.ClassNotFoundException: org.mockito.internal.MockitoInvocationHandler
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 35 more

为什么不这样做:

@Before
public void setUp() {
    mockedSessionFactory =  Mockito.mock(SessionFactory.class);
    mockedSession = Mockito.mock(Session.class);
    mockedTransaction = Mockito.mock(Transaction.class);
    Mockito.when(mockedSessionFactory.openSession()).thenReturn(mockedSession);
    Mockito.when(mockedSession.beginTransaction()).thenReturn(mockedTransaction);
    loginDAO.setSessionFactory(this.mockedSessionFactory);
}

而不是:

@Autowired
public void setSessionFactory(SessionFactory sessionFactoryCore) {
    mockedSessionFactory =  PowerMockito.mock( sessionFactoryCore.getClass());
    mockedSession = PowerMockito.mock(Session.class);
    PowerMockito.doReturn(mockedSession).when(mockedSessionFactory).openSession();
    PowerMockito.doReturn(mockedTransaction).when(mockedSession).beginTransaction();
    loginDAO.setSessionFactory(this.mockedSessionFactory);
}

此外,它允许您不使用powermock。

有趣的部分在这里:

Caused by: java.lang.NoClassDefFoundError: org/mockito/internal/MockitoInvocationHandler
    at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:138)
    at com.aeon.ps.dao.LoginDAOTest2.setSessionFactory(LoginDAOTest2.java:61)
    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.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:586)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
    ... 26 more

这意味着powermock使用的内部类在您正在使用的mockito发行版中不可用。

根据我收集到的信息,您正在使用Mockito 1.9.5-rc1,以及其他一些最近发布的Powermock。到目前为止,Johan还没有发布Powermock的新版本来处理Mockito 1.9.5-rc1中的更改。

Mockito中的这些重构更改是为了帮助MockMaker API的引入,该API允许第三方提供自己的mock工厂、字节码引擎或其他创建mock的方法。请参阅此处的文档:http://docs.mockito.googlecode.com/hg/1.9.5-rc1/org/mockito/Mockito.html

所以现在,如果你使用Powermock,你应该使用Mockito 1.9.0。

相关内容

  • 没有找到相关文章

最新更新