Results 1 to 7 of 7

Thread: Only load a bean once throughout a test suite

  1. #1

    Default Only load a bean once throughout a test suite

    Is this possible? I can provide any source code you want, I just want to know if this is possible.

    I run my test suite using ant and junit.

    Thanks in advance!

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    If you use springs test support this is already the case unless you specify it otherwise.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Which is accomplished with the @RunWith(SpringJUnit4ClassRunner.class) annotation correct?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    I suggest a read of the test chapter, it is the SpringJUnit4ClassRunner in conjunction with the @ContextConfiguration annotation. As long as that is the same the configuration is already loaded.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    I have read the test chapter.

    I have both of those annotations, but on every test that derives from my baseclass, in which i put those annotations, I still see the following output:


    Code:
    [10 Jul 2012 14:44:36,212] DEBUG  DefaultListableBeanFactory | Creating instance of bean 'sessionFactory'
    [10 Jul 2012 14:44:36,217] DEBUG  DefaultListableBeanFactory | Eagerly caching bean 'sessionFactory' to allow for resolving potential circular references
    [10 Jul 2012 14:44:36,217] DEBUG  CachedIntrospectionResults | Getting BeanInfo for class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]
    [10 Jul 2012 14:44:36,227] DEBUG  CachedIntrospectionResults | Caching PropertyDescriptors for class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]
    [10 Jul 2012 14:44:36,228] DEBUG  CachedIntrospectionResults | Found bean property 'beanClassLoader' of type [java.lang.ClassLoader]
    [10 Jul 2012 14:44:36,228] DEBUG  CachedIntrospectionResults | Found bean property 'cacheProvider' of type [org.hibernate.cache.CacheProvider]
    [10 Jul 2012 14:44:36,228] DEBUG  CachedIntrospectionResults | Found bean property 'cacheRegionFactory' of type [java.lang.Object]
    [10 Jul 2012 14:44:36,228] DEBUG  CachedIntrospectionResults | Found bean property 'cacheableMappingLocations' of type [[Lorg.springframework.core.io.Resource;]
    [10 Jul 2012 14:44:36,228] DEBUG  CachedIntrospectionResults | Found bean property 'class' of type [java.lang.Class]
    [10 Jul 2012 14:44:36,228] DEBUG  CachedIntrospectionResults | Found bean property 'collectionCacheStrategies' of type [java.util.Properties]
    [10 Jul 2012 14:44:36,228] DEBUG  CachedIntrospectionResults | Found bean property 'configLocation' of type [org.springframework.core.io.Resource]
    [10 Jul 2012 14:44:36,228] DEBUG  CachedIntrospectionResults | Found bean property 'configLocations' of type [[Lorg.springframework.core.io.Resource;]
    [10 Jul 2012 14:44:36,228] DEBUG  CachedIntrospectionResults | Found bean property 'configuration' of type [org.hibernate.cfg.Configuration]
    [10 Jul 2012 14:44:36,228] DEBUG  CachedIntrospectionResults | Found bean property 'configurationClass' of type [java.lang.Class]
    [10 Jul 2012 14:44:36,228] DEBUG  CachedIntrospectionResults | Found bean property 'dataSource' of type [javax.sql.DataSource]
    [10 Jul 2012 14:44:36,228] DEBUG  CachedIntrospectionResults | Found bean property 'entityCacheStrategies' of type [java.util.Properties]
    [10 Jul 2012 14:44:36,228] DEBUG  CachedIntrospectionResults | Found bean property 'entityInterceptor' of type [org.hibernate.Interceptor]
    [10 Jul 2012 14:44:36,228] DEBUG  CachedIntrospectionResults | Found bean property 'eventListeners' of type [java.util.Map]
    [10 Jul 2012 14:44:36,228] DEBUG  CachedIntrospectionResults | Found bean property 'exposeTransactionAwareSessionFactory' of type [boolean]
    [10 Jul 2012 14:44:36,228] DEBUG  CachedIntrospectionResults | Found bean property 'filterDefinitions' of type [[Lorg.hibernate.engine.FilterDefinition;]
    [10 Jul 2012 14:44:36,228] DEBUG  CachedIntrospectionResults | Found bean property 'hibernateProperties' of type [java.util.Properties]
    [10 Jul 2012 14:44:36,228] DEBUG  CachedIntrospectionResults | Found bean property 'jdbcExceptionTranslator' of type [org.springframework.jdbc.support.SQLExceptionTranslator]
    [10 Jul 2012 14:44:36,229] DEBUG  CachedIntrospectionResults | Found bean property 'jtaTransactionManager' of type [javax.transaction.TransactionManager]
    [10 Jul 2012 14:44:36,229] DEBUG  CachedIntrospectionResults | Found bean property 'lobHandler' of type [org.springframework.jdbc.support.lob.LobHandler]
    [10 Jul 2012 14:44:36,229] DEBUG  CachedIntrospectionResults | Found bean property 'mappingDirectoryLocations' of type [[Lorg.springframework.core.io.Resource;]
    [10 Jul 2012 14:44:36,229] DEBUG  CachedIntrospectionResults | Found bean property 'mappingJarLocations' of type [[Lorg.springframework.core.io.Resource;]
    [10 Jul 2012 14:44:36,229] DEBUG  CachedIntrospectionResults | Found bean property 'mappingLocations' of type [[Lorg.springframework.core.io.Resource;]
    [10 Jul 2012 14:44:36,229] DEBUG  CachedIntrospectionResults | Found bean property 'mappingResources' of type [[Ljava.lang.String;]
    [10 Jul 2012 14:44:36,229] DEBUG  CachedIntrospectionResults | Found bean property 'namingStrategy' of type [org.hibernate.cfg.NamingStrategy]
    [10 Jul 2012 14:44:36,229] DEBUG  CachedIntrospectionResults | Found bean property 'object' of type [org.hibernate.SessionFactory]
    [10 Jul 2012 14:44:36,229] DEBUG  CachedIntrospectionResults | Found bean property 'objectType' of type [java.lang.Class]
    [10 Jul 2012 14:44:36,229] DEBUG  CachedIntrospectionResults | Found bean property 'schemaUpdate' of type [boolean]
    [10 Jul 2012 14:44:36,229] DEBUG  CachedIntrospectionResults | Found bean property 'singleton' of type [boolean]
    [10 Jul 2012 14:44:36,229] DEBUG  CachedIntrospectionResults | Found bean property 'typeDefinitions' of type [[Lorg.springframework.orm.hibernate3.TypeDefinitionBean;]
    [10 Jul 2012 14:44:36,229] DEBUG  CachedIntrospectionResults | Found bean property 'useTransactionAwareDataSource' of type [boolean]
    [10 Jul 2012 14:44:36,230] DEBUG  DefaultListableBeanFactory | Returning cached instance of singleton bean 'dataSource'
    [10 Jul 2012 14:44:36,230] DEBUG  TypeConverterDelegate     | Converting String to [interface org.springframework.core.io.Resource] using property editor [org.springframework.core.io.ResourceEditor@154850]
    [10 Jul 2012 14:44:36,234] DEBUG  DefaultListableBeanFactory | Invoking afterPropertiesSet() on bean with name 'sessionFactory'
    [10 Jul 2012 14:44:36,767] INFO   LocalSessionFactoryBean   | Building new Hibernate SessionFactory
    [10 Jul 2012 14:44:36,801] DEBUG  DriverManagerDataSource   | Creating new JDBC DriverManager Connection to [jdbc:hsqldb:mem:mydb]
    [10 Jul 2012 14:44:37,472] DEBUG  DriverManagerDataSource   | Creating new JDBC DriverManager Connection to [jdbc:hsqldb:mem:mydb]
    [10 Jul 2012 14:44:38,763] DEBUG  DefaultListableBeanFactory | Finished creating instance of bean 'sessionFactory'
    Which shows each time that sessionFactory gets created. I want to persist the sessionFactory the first time throughout the whole suite, and never load it more than once. Is this possible?

    And I have:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath:PersistenceHelper-context.xml"})
    public class Foo


    And all of the tests I use the sessionFactory extend from this class.

  6. #6

    Default

    Turns out each junit test was being run in a separate jvm. Problem fixed by the earlier suggestions.

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    I was just going to suggest that .
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •