在每个单元测试之前为同一测试类启动Spring配置

时间:2021-06-19 19:20:37

I try to integrate hsqldb (memory db) in my project, because from the beginning, all DAO are tested with a real database and its not very safe, because when database is not up, jenkins failed all test!

我尝试在我的项目中集成hsqldb(内存数据库),因为从一开始,所有DAO都使用真实数据库进行测试,并且它不是很安全,因为当数据库没有启动时,jenkins都无法通过所有测试!

but i see something strange when i launch a test class.

但是当我启动测试课时,我发现了一些奇怪的东西。

I have three test in my class and when i doing run as junit test with eclipse, i see spring configuration reloaded before each unit test. Does it normal? because i have use

我在班上有三个测试,当我用eclipse运行junit测试时,我看到在每个单元测试之前重新加载spring配置。这是正常的吗?因为我有用

 <prop key="hibernate.hbm2ddl.import_files">import.sql</prop>

and this file contain many create table, and when the second test is launch, i see error messages : "table toto already exist..."

并且此文件包含许多create table,当第二个测试启动时,我会看到错误消息:“table toto already exists ...”

does it possible to have spring configuration loaded only one for all test in the class?

是否可以在课堂上为所有测试加载一个弹簧配置?

here is my configuration :

这是我的配置:

    <bean name="dataSource"     class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc:hsqldb:mem:castor;syntax.ora=true" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>


     <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="mappingLocations" value="classpath*:fr/**/*.hbm.xml"/>
  <property name="hibernateProperties">
    <props>
     <prop key="hibernate.dialect">fr.edf.mpv2.castor.persistance.HsqlOracleDialect</prop>
     <prop key="hibernate.show_sql">false</prop>
     <prop key="hibernate.hbm2ddl.auto">create</prop>
     <prop key="hibernate.hbm2ddl.import_files">import.sql</prop>
     <prop key="hibernate.format_sql">false</prop>
     <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
     <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
    </props>
  </property>
  <property name="dataSource" ref="dataSource"/>
</bean>


      <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <constructor-arg ref="dataSource"/>
     <property name="rollbackOnCommitFailure" value="true"/>
</bean>

its a legacy project, and people who start the project with spring doesn't know how its work, so they doesn't use injection :( and they create a factory that load the spring configuration instead of use applicationContext.xml...

它是一个遗留项目,用spring开始项目的人不知道它是如何工作的,因此他们不使用注入:(并且他们创建了一个加载spring配置而不是使用applicationContext.xml的工厂......

so for help developper i have create a mock to override the factory in test context

所以对于帮助developper我创建了一个模拟来覆盖测试环境中的工厂

here is the declaration :

这是宣言:

   public class MailDAOTest extends BeanFactoryOverrider {
   ....



    @RunWith(PowerMockRunner.class)
    @PrepareForTest({ BeanFactory.class })
    @PowerMockIgnore("javax.management.*")
    public class BeanFactoryOverrider {

    /** Context spring de test. */
    private ApplicationContext applicationContext;

so everywhere in the code where we use the factory with is own springcontext, i override method to use my own spring context with my own configuration and not the real production configuration. this work fine, but its just to explain why i m using PowerkMockRunner ;)

所以在我们使用工厂的代码中的每个地方都有自己的springcontext,我覆盖了使用我自己的spring上下文和我自己的配置而不是真正的生产配置的方法。这项工作很好,但它只是解释为什么我使用PowerkMockRunner;)

i use spring 2.0 / hibernate 3.2.6 / hsql 2.3.2

我使用spring 2.0 / hibernate 3.2.6 / hsql 2.3.2

thanks a lot for your help!

非常感谢你的帮助!

1 个解决方案

#1


0  

If you specify context configuration location on your test class, and use spring test runner the context will be cached not reloaded each time.

如果在测试类上指定上下文配置位置,并使用spring test runner,则每次都将缓存上下文而不重新加载。

Something like :

就像是 :

@RunWith (SpringJUnit4ClassRunner.class)
@ContextConfiguration (locations = "classpath:/config/applicationContext-test.xml")
@WebAppConfiguration
public class MyTest {}

#1


0  

If you specify context configuration location on your test class, and use spring test runner the context will be cached not reloaded each time.

如果在测试类上指定上下文配置位置,并使用spring test runner,则每次都将缓存上下文而不重新加载。

Something like :

就像是 :

@RunWith (SpringJUnit4ClassRunner.class)
@ContextConfiguration (locations = "classpath:/config/applicationContext-test.xml")
@WebAppConfiguration
public class MyTest {}