<p>I'm writing tests for a business method that invokes some DAO classes to perform operations over a database.</p>

<p>This method, firstly retrieves a JDBC connection from a DataSource object, The same connection is passed to all DAO instances, so I can use it to control the transaction. So, if everything works properly, I must invoke commit() over the connection object.</p>

<p>I would like to test if the commit() is invoked, so I've thought to create an expectation (I'm using JMock) that checks that. But since the Connection class isn't a direct neighbour from my Business class, I don't know how to do this.</p>

<p>Someone knows how to overcome this? There is some JMock facility for this, or some alternative design that allows to overcome this?</p>

<p>Thanks</p>

You need to mock
```
DataSource
```
and
```
Connection
```
so that your mock
```
DataSource
```
returns your mock
```
Connection
```
. And yes, this kind of thing ends up becoming a real pain...