Service class's @Transactional not invoked in test context

Viewed 106

I am using Spring's @Transactional in my DAO's service class. The application has beans configured in XML(s) and via annotations like @Service.

My test case configuration looks like below:

@RunWith(SpringJunit4ClassRunner.class)
@ContextHierarchy({
    @ContextConfiguration(locations = {"classpath:spring/spring.xml"}),
    @ContextConfiguration(classes = {Service.class})
})
@ComponentScan(...)
public class TestRunner {
  @Autowired
  private Service service;

  @Test
  public void testSave() {
      service.save(...);
  }
}

I have below configured in XML:

<tx:annotation-driven transaction-manager="txmManager"/>
<!-- bean for hibernate5 transaction manager using sessionfactory -->

The service class looks as below:

@Service
public class Service {

   @Autowired
   private DAO dao;

   @Transactional
   public Entity save(Entity entity) {
       dao.save(entity);
   }
}

Now it all works perfectly during source context run but when I run the test cases, the transaction is never invoked. Note: there is no NPE on calling Service.class's method.

I tweaked around and noticed that if I create a bean for Service.class in my (test) spring xml file, the test cases work as expected, i.e., the transaction manager is called and the in-memory database is updated.

I expected SpringJunit4ClassRunner to create a MergedContext and the transaction-manager configuration to be automatically kicked in upon Service#save call.

Any thoughts on what am I missing here?

Hibernate - 5.2.6.Final, Spring - 4.2.0.RELEASE, Spring-test - 4.1.6.RELEASE

0 Answers
Related