Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 633429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:11:05+00:00 2026-05-13T20:11:05+00:00

I wrote a test case that extends AbstractTransactionalJUnit4SpringContextTests . The single test case I’ve

  • 0

I wrote a test case that extends AbstractTransactionalJUnit4SpringContextTests. The single test case I’ve written creates an instance of class User and attempts to write it to the database using Hibernate. The test code then uses SimpleJdbcTemplate to execute a simple select count(*) from the user table to determine if the user was persisted to the database or not. The test always fails though. I was suspect because in the Spring controller I wrote, the ability to save an instance of User to the db is successful.

So I added the Rollback annotation to the unit test and sure enough, the data is written to the database since I can even see it in the appropriate table — the transaction isn’t rolled back when the test case is finished.

Here’s my test case:

@ContextConfiguration(locations = {
    "classpath:context-daos.xml", 
    "classpath:context-dataSource.xml", 
    "classpath:context-hibernate.xml"})
public class UserDaoTest extends AbstractTransactionalJUnit4SpringContextTests {

  @Autowired
  private UserDao userDao;

  @Test
  @Rollback(false)
  public void teseCreateUser() {
    try {
        UserModel user = randomUser();
        String username = user.getUserName();

        long id = userDao.create(user);

        String query = "select count(*) from public.usr  where usr_name = '%s'";
        long count = simpleJdbcTemplate.queryForLong(String.format(query, username));

        Assert.assertEquals("User with username should be in the db", 1, count);
    }
    catch (Exception e) {
        e.printStackTrace();
        Assert.assertNull("testCreateUser: " + e.getMessage());
    }
  }
}

I think I was remiss by not adding the configuration files.

context-hibernate.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd>

   <bean id="namingStrategy" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
  <property name="staticField">
     <value>org.hibernate.cfg.ImprovedNamingStrategy.INSTANCE</value>
  </property>
   </bean>

   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="destroy" scope="singleton">
  <property name="namingStrategy">
     <ref bean="namingStrategy"/>
  </property>

  <property name="dataSource" ref="dataSource"/>

  <property name="mappingResources">
     <list>
        <value>com/company/model/usr.hbm.xml</value>
     </list>
  </property>

  <property name="hibernateProperties">
     <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.use_sql_comments">true</prop> 
        <prop key="hibernate.query.substitutions">yes 'Y', no 'N'</prop>
        <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
        <prop key="hibernate.cache.use_query_cache">true</prop>
        <prop key="hibernate.cache.use_minimal_puts">false</prop>
        <prop key="hibernate.cache.use_second_level_cache">true</prop>
        <prop key="hibernate.current_session_context_class">thread</prop>
     </props>
  </property>
   </bean>

   <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"/>
  <property name="nestedTransactionAllowed" value="false" />
   </bean>   

   <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
  <property name="transactionManager">
     <ref local="transactionManager"/>
  </property>

  <property name="transactionAttributes">
     <props>
        <prop key="create">PROPAGATION_REQUIRED</prop>
        <prop key="delete">PROPAGATION_REQUIRED</prop>
        <prop key="update">PROPAGATION_REQUIRED</prop>
        <prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
     </props>
  </property>
   </bean>      
 </beans>

context-dataSource.xml

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="org.postgresql.Driver" />
        <property name="jdbcUrl"     value="jdbc\:postgresql\://localhost:5432/company_dev" />
        <property name="user"        value="postgres" />
        <property name="password"    value="postgres" />
     </bean>   
</beans>

context-daos.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="extendedFinderNamingStrategy" class="com.company.dao.finder.impl.ExtendedFinderNamingStrategy"/>
   <bean id="finderIntroductionAdvisor"    class="com.company.dao.finder.impl.FinderIntroductionAdvisor"/>

   <bean id="abstractDaoTarget" class="com.company.dao.impl.GenericDaoHibernateImpl" abstract="true" depends-on="sessionFactory">
      <property name="sessionFactory">
         <ref bean="sessionFactory"/>
      </property>

      <property name="namingStrategy">
         <ref bean="extendedFinderNamingStrategy"/>
      </property>
   </bean>

   <bean id="abstractDao" class="org.springframework.aop.framework.ProxyFactoryBean" abstract="true">
      <property name="interceptorNames">
         <list>
           <value>transactionInterceptor</value>
            <value>finderIntroductionAdvisor</value>
         </list>
       </property>
   </bean>

   <bean id="userDao" parent="abstractDao">
     <property name="proxyInterfaces">
       <value>com.company.dao.UserDao</value>
     </property>

  <property name="target">
     <bean parent="abstractDaoTarget">
        <constructor-arg>
           <value>com.company.model.UserModel</value>
        </constructor-arg>
     </bean>
  </property>
  </bean>


</beans>

Some of this I’ve inherited from someone else. I wouldn’t have used the proxying that is going on here because I’m not sure it’s needed but this is what I’m working with.

Any help much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:11:07+00:00Added an answer on May 13, 2026 at 8:11 pm

    IIRC, Hibernate (and other O/R mappers) delay the database INSERT and UPDATE statements until the transaction commit. The SELECT then does not see the data, as it is not yet written. Try explicitly requesting a session flush.

    See also the documentation for an explanation and example:
    http://docs.jboss.org/hibernate/core/3.3/reference/en/html/objectstate.html#objectstate-flushing

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How does one write a unit test that fails only if a function doesn't
I'm trying to write a Selenium test for a web page that uses an
Hey guys i wrote a quick test. I want delete to call deleteMe which
I just lost 50% of my answer on a test because I wrote the
I need to write test cases for my application. I've chosen NUnit. Please let
Do you write one test per function/method, with multiple checks in the test, or
I am wondering should I write unit test for everything. There are some classes
I see lots of TDD practitioners following this cycle: 1) Write your test as
I'm new to unit testing and nUnit (2.48). I'd like to write a test
I am trying to write a unit test for an action method which calls

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.