How do I mock a TransactionManager in a JUnit test, (outside of the container)?
We simply create an empty implementaion for the transaction manager, and ensure that this implementation is used in the spring-context used by the unit test
package sample;import org.springframework.stereotype.Service;import org.springframework.transaction.PlatformTransactionManager;import org.springframework.transaction.TransactionDefinition;import org.springframework.transaction.TransactionException;import org.springframework.transaction.TransactionStatus;public class MockedTransactionManager implements PlatformTransactionManager { @Override public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException { return null; } @Override public void commit(TransactionStatus status) throws TransactionException { } @Override public void rollback(TransactionStatus status) throws TransactionException { }}
.. and in the spring-xml file then looks like..
<bean id="transactionManager" class="sample.MockedTransactionManager"/>
You can use PseudoTransactionManager:
PlatformTransactionManager manager = new PseudoTransactionManager();