Announcement Announcement Module
Collapse
No announcement yet.
Unit testing Programmatic Transaction code Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unit testing Programmatic Transaction code

    Hi,

    I'm authoring a web application that requires runtime access to different database - as in, the user actually types in the database connection info (host url, username, password) via a web form, and then some logic gets run against that database. I understand this architecture is wonky, but trust me it's appropriate for this application.

    I've got the abstraction and basic code framework down just fine (business logic bean calls dao interface, dao interface in turn has an iBatis implementation), and everything is easily unit testable. I've got a nice dynamic mock of the dao interface (using jmock).

    However, I want to add transactional support. Declarative transactions in spring essentially assume that you have one or more static data sources around at app launch time (so that the data sources can be configured in the application context xml file). This obviously is not going to work for me, because I'm creating data sources/connections on the fly at runtime.

    So I decide to use programmatic transactions, specifically TransactionTemplate and transaction callbacks. I can (and did) code all of this up without any difficulty. The problem is figuring out how to test it. I don't want to test that business logic layer against a live database, I want to use my mock DAO. But the mock DAO doesn't have a real data source, and therefore I can't create a transaction manager/template. But the business logic bean expects one to be there because the business logic layer is the spring-suggested layer in which to include programmatic transaction code.

    I'm kind of spinning in circles here.. any advice?

    Thanks!
    -Peter

  • #2
    Re: Unit testing Programmatic Transaction code

    Originally posted by petey
    So I decide to use programmatic transactions, specifically TransactionTemplate and transaction callbacks. I can (and did) code all of this up without any difficulty. The problem is figuring out how to test it. I don't want to test that business logic layer against a live database, I want to use my mock DAO. But the mock DAO doesn't have a real data source, and therefore I can't create a transaction manager/template. But the business logic bean expects one to be there because the business logic layer is the spring-suggested layer in which to include programmatic transaction code.

    I'm kind of spinning in circles here.. any advice?
    Answering my own question here... Spring 1.2.3 (just released) helped me out a bit. What I ended up doing, and what seems to be working, is creating a stub/mock PlatformTransactionManager implementation, and using the new-to-1.2.3 SimpleTransactionStatus object to return from the getTransaction() method.

    Additionally, my mock is keeping track of when commit and/or rollback was called, so I can add unit tests that have my mock DAO class throw some runtime exceptions to ensure that the transactions are actually being preserved.

    However, I do wish there was a little more detail on working with spring programmatic transactions in the spring literature

    Comment

    Working...
    X