开发者

JTA: how to be test JMS and JDBC failures?

开发者 https://www.devze.com 2022-12-13 20:15 出处:网络
we\'re currently working on testing JTA failure behaviour, on a system that receives messages using JMS, persists them, and sends results using another class.

we're currently working on testing JTA failure behaviour, on a system that receives messages using JMS, persists them, and sends results using another class.

The whole thing is tied together using Spring. Current unit tests use HSQLDB, Apache ActiveMQ and Bitronix for transaction management. Success with this has been limited, mostly because HSQLDB does not implement XA transactions.

So here is the question: how to best simulate database failures in a transaction unit test? Is there any way to make a standard JDBC driver (for Oracle, say) fail in the middle of a test?开发者_如何学JAVA

n.b. pressing the power button is not a repeatable test :)


You need to decide what exactly do you want to test - for example if you want to test how Oracle would behave in XA transaction with Bitronix then mocking DAOs, as suggested by duffymo, is not going to help you. In such case you need to find a way to break connectivity in the middle of transaction and then see how Bitronix/Oracle would handle recovery - e.g. heuristic outcomes and so on.

Note that in quite a few cases there are ways to get the same functionality without actually using XA transactions. It could be simpler, faster and more testable. For example, in very common case when messages are consumed from MOM and DML executed in database there is a common pattern of how to get away without XA even so two resource managers are getting updated.


Write a mock object for the test whose implementation throws an exception in the middle of the transaction.

Since you're using Spring, it's an easy matter to write a new, test-only implementation of the DAO interface that behaves in a repeatable, predictable manner. Inject the 'wonky DAO' only for the test.

Of course you're using the XA driver to connect to the database. Two phase commit won't work otherwise.

0

精彩评论

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