2012-08-22 56 views
2

我正在使用Spring和Hibernate的項目。我有一個包含SessionFactory的抽象DAO。我需要爲我的單元測試嘲笑SessionFactory或Session。但迄今爲止我還沒有取得成功。以下是代碼。任何人有任何想法?謝謝。如何通過PowerMockito在使用Spring和Hibernate的項目中模擬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 

+0

一個類缺失,你的類路徑中有mockito嗎? – gontard

回答

7

爲什麼不這樣做:代替

@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。

+0

問題是你不能使用Mockito來模擬finalFunction,比如Session.class,也不能用它來模擬像Session.class這樣的接口。儘管如此,我接受了您的建議並使用了您的代碼,結果與以前相同。不過謝謝。 :) –

+1

'SessionFactoryImpl'是'final',但'SessionFactory'是一個'interface'。你**可以**模擬與Mockito的接口。 – gontard

+0

嗨Gontard,是的,你是對的。現在我的問題解決了。謝謝 –

0

有趣的部分是在這裏:

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 din't發佈了更新版本的Powermock,用於處理Mockito 1.9.5-rc1中所做的更改。

Mockito中的這些重構更改是爲了幫助引入MockMaker API,它允許第三方提供自己的模擬工廠,字節碼引擎或創建模擬。 請參閱此處的文檔:http://docs.mockito.googlecode.com/hg/1.9.5-rc1/org/mockito/Mockito.html

因此,現在,如果您使用的是Powermock,則應該使用Mockito 1.9.0。

相關問題