Skip to content

@Transactional annotation does not apply to inherited methods from abstract class during tests [SPR-11252] #15877

@spring-projects-issues

Description

@spring-projects-issues

Francisco Lozano opened SPR-11252 and commented

Scenario:
I have different implementations for the same DAO interface. Some of these DAOs are transactional (JDBC-based ones) and some others are not (memory, NoSQL).

In the transactional DAOs, I add:
{QUOTE}
@Transactional(propagation=Propagation.MANDATORY)
{QUOTE}
to ensure the transactional DAO code is always ran within the scope of a transaction.

I have some generic DAO integration tests, defined (common for all the DAO implementations) in an abstract parent class. Then, for each DAO implementation, I have an inherited class that returns the proper DAO instance to the tests.

The DAO instance to be tested comes either from direct instantiation (no Spring involved) or from a bean injected by Spring test framework.

As my transactional DAOs expect to be ran within the context of a transaction, I expect Spring test framework to handle transactions also in the tests defined in the abstract parent class, not only in the DAO-specific sub-class. I configure it to do so:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { AppConfiguration.class })
@Transactional
public class SpringJDBCPetDAOImplTest extends PetDAOTest {

Problem:

Spring test framework only honors @Transactional annotation for the tests defined directly in the concrete sub-class, and not in the abstract parent class. I have to override the test in the sub-class in order to make Spring test framework handle the transaction:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { AppConfiguration.class })
@Transactional
public class WorkaroundSpringJDBCPetDAOImplTest extends PetDAOTest {

	@Autowired
	@Qualifier("jdbc")
	PetDAO petDAO;

	@Override
	protected PetDAO getPetDAO() {
		return petDAO;
	}
	
	@Override
	public void simpleTest() {
		super.simpleTest();
	}

More info:
I've created an example project (https://github.com/flozano/spring-test-framework-transactions-problem), which shows how SpringJDBCPetDAOImplTest#simpleTest() fails because of the missing transaction, despite having the proper @Transactional and spring test fwk annotations in the test class.

PS: If you think it's hard to keep the same interface with different transactional semantics involved, I agree with you :) but it's the way it is.


Affects: 4.0 GA

Reference URL: https://github.com/flozano/spring-test-framework-transactions-problem

Issue Links:

1 votes, 2 watchers

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulestatus: duplicateA duplicate of another issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions