How to check if my transactional methods really support transactions?
I'm using Spring 3.0, and I have a set of methods like this:
@Transactional (value = "authTransactionManager")
public void remove(User user) {
...
开发者_运维百科}
I use 2 different transaction managers and specify necessary manager (authTransactionManager
in example above).
I was curious what would happen if I specify nonexistent manager. I expected compile exception or runtime exception, but everything worked fine. That's why I have doubts about any transactions were ever supported in my methods.
How can I test it? (maybe, spring is so smart that uses any available manager when nonexistent manager specified and all my doubts are unfounded?)
You could turn on debug-level logging, this is generally what I do when debugging these kind of transaction problems!
Spring is quite good about logging transactions.
Here are 2 options:
(1) Use integration tests. You could use tools like dbUnit to load data into the database, and then write tests against the DB, using transactions. (We do this all the time to test our XML Hibernate mappings. In fact, the tests execute as part of the automated build.)
(2) Use FIT tests. These are quite a different beast: functional tests for the system. FIT tests will require 'fixtures' to act as glue between your tests and your system, but they could help with this issue. (It might be overkill in this case.)
In both cases, you have the option of either (a) doing some quick-and-dirty testing as a sanity check or (b) designing a testing subsystem that goes into your automated build. (We use both integration tests and FIT tests for case (b)).
精彩评论