开发者

Integration Test which simulates Exceptional / Rollback condition with AbstractTransactionalJUnit4SpringContextTests

I have a JUnit4 test which extends AbstractTransactionalJUnit4SpringContextTests. I have a business object that I've marked as a unit of work with @Transactional. The unit of work updates the database that a user has been notified & then attempts to send them a notification message. I have configured the method to rollback for a MailException (spring simple mail api) and mocked out a MailSender to throw a SendMailException when it's called to validate the rollback functionality.

In my Unit Test though, the transaction does not rollback, I'm trying to figure out if it's because it's taking part in the transaction that is being opened for my actual test method, because at that point the test method hasn't commit or rolled back. The method which represents a unit of work is configured to require a new transaction, and I've tried configuring two transaction managers in spring config and specifying that the test case use the second transaction manager. No matter, when I check the data inside the unit 开发者_运维技巧test method, it remains modified even though it should have been rolled back by the application code encountering the mail send exception.

any insight would be great.


I've used a baseclass with following annotations and extended my actual test-classes from it:

@ContextConfiguration(locations="classpath:applicationContext-test.xml")
@TransactionConfiguration(transactionManager="txManager", defaultRollback=true)
@Transactional
public abstract class IntegrationTestBase extends AbstractTransactionalJUnit4SpringContextTests
{
    //...Some setup stuff with @Before
}

The test-classes extending this class are marked with @Transactional -annotation, but I'm not sure if it's even necessary. Anyway, it seems to do the trick, as the log indicates that rollback occurs after each test, and changes made in one test-method aren't visible in another.

15:53:10,564 (6923) [INFO] org.springframework.test.context.transaction.TransactionalTestExecutionListener:280 - Rolled back transaction after test execution for test context [[TestContext@6dadfbe3 testClass = org.some.SomeTestClass, locations = array['classpath:applicationContext-test.xml'], testInstance = org.some.SomeTestClass@1f0936a1, testMethod = testSomething@SomeTestClass, testException = [null]]]

Also see @Transactional -annotations attributes rollbackFor, rollbackForClassName, noRollbackFor & noRollbackForClassName

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜