我正在使用Spring 3.1.1.RELEASE,JUnit 4.8.1和HSQL 2.7.7内存数据库。我有一个测试班级注释为
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ "classpath:test-trainingSessionServiceContext.xml" }) @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) public class TrainingSessionServiceTest {
问题是,当我运行“ mvn clean test”时,似乎所有的测试类都在上述类失败之后运行,因为内存数据库已被破坏并且没有重新创建。我收到类似的错误
org.hibernate.exception.SQLGrammarException: user lacks privilege or object not found: CB_ORGANIZATION" type="javax.persistence.PersistenceException">javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: user lacks privilege or object not found: CB_ORGANIZATION at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1360) at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:817) at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:771) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240) at $Proxy46.find(Unknown Source) at org.mainco.subco.organization.repo.OrganizationDaoImpl.findById(OrganizationDaoImpl.java:77) at org.mainco.subco.pd.repo.LinkDaoTest.createDummyLink(LinkDaoTest.java:686) at org.mainco.subco.pd.repo.LinkDaoTest.testSaveLink(LinkDaoTest.java:67) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
这是我设置给出异常的测试类(在上述类之后运行)的方式……
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ "classpath:test-context.xml" }) public class LinkDaoTest extends AbstractTransactionalJUnit4SpringContextTests {
在运行每个测试类之前,有什么方法可以将应用程序上下文恢复到原始状态?我不想让“ TrainingSessionServiceTest”类扩展AbstractTransactionalJUnit4SpringContextTests。这是我的应用程序上下文的相关部分:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.hsqldb.jdbcDriver"/> <property name="url" value="jdbc:hsqldb:mem:pd" /> <property name="username" value="sa" /> <property name="password" value="" /> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> </property> <property name="persistenceXmlLocation" value="classpath:META-INF/test-persistence.xml"/> <property name="persistenceUnitName" value="testingDatabase"/> <property name="dataSource" ref="dataSource"/> </bean> <bean id="sharedEntityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <tx:annotation-driven /> <jdbc:embedded-database id="embedded" type="HSQL"/> <jdbc:initialize-database data-source="dataSource"> <jdbc:script location="classpath:db-test-data.sql"/> </jdbc:initialize-database>
使用@DirtiesContext强制重置。例如,我有:
@ContextConfiguration(classes={BlahTestConfig.class}) @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) public class SomeTest { @Autowired XXXX xx; @Autowired YYYY yy; @Before public void setUp() { MockitoAnnotations.initMocks(this); when(YYYY.newYY()).thenReturn(zz); } @Test public void testSomeTest() { XX.changeSomething("StringTest"); XX.doSomething(); check_for_effects(); } @Test public void testSomeOtherTest() { XX.changeSomething("SomeotherString"); XX.doSomething(); check_for_effects(); }
从Spring文档
DirtiesContext
表示在测试执行期间对基础Spring ApplicationContext进行了如下污染(修改),并且无论测试是否通过,都应将其关闭:
在当前测试类之后,当在类模式设置为AFTER_CLASS的类上声明时,这是默认的类模式。
在当前测试类中的每个测试方法之后,在类模式设置为AFTER_EACH_TEST_METHOD的类上声明时。
当前测试之后,当在方法上声明时。
如果测试已修改上下文(例如,通过替换bean定义),请使用此注释。随后的测试提供了新的上下文。[注] @DirtiesContext在JUnit 3.8中的局限性
在JUnit 3.8环境中,@DirtiesContext仅在方法上受支持,因此在类级别不受支持。
你可以将@DirtiesContext用作同一类中的类级和方法级注释。在这种情况下,在任何带注释的方法之后以及整个类之后,ApplicationContext都被标记为dirties。如果将ClassMode设置为AFTER_EACH_TEST_METHOD,则在类中的每个测试方法之后将上下文标记为dirties。
@DirtiesContext public class ContextDirtyingTests { // some tests that result in the Spring container being dirtied } @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) public class ContextDirtyingTests { // some tests that result in the Spring container being dirtied } @DirtiesContext @Test public void testProcessWhichDirtiesAppCtx() { // some logic that results in the Spring container being dirtied }
当应用程序上下文标记为脏时,将其从测试框架的缓存中删除并关闭;因此,可以为需要上下文具有相同资源位置集的任何后续测试重建基础Spring容器。