I have the following code which I would like to mock. I'm basically interested in mocking the DataSourceTransactionManager.
@Autowired
@Qualifier("nesTransactionManager")
DataSourceTransactionManager mDataSourceTransactionManager;
...............................
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
TransactionStatus status = mDataSourceTransactionManager.getTransaction(def);
try {
<-doing some DB operations here>
mDataSourceTransactionManager.commit(status);
} catch (Exception e) {
mDataSourceTransactionManager.rollback(status);
}
.............
So far I've tried this:
@Mock
private DataSourceTransactionManager mDataSourceTransactionManager;
@Before
public void runBeforeEachTest() {
when(mDataSourceTransactionManager.getTransaction(any(DefaultTransactionDefinition.class))).thenReturn(null);
doNothing().when(mDataSourceTransactionManager).commit(any(TransactionStatus.class));
doNothing().when(mDataSourceTransactionManager).rollback(any(TransactionStatus.class));
}
And this:
@Mock
private DataSourceTransactionManager mDataSourceTransactionManager;
@Before
public void runBeforeEachTest() {
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
when(mDataSourceTransactionManager.getTransaction(eq(def))).thenReturn(null);
doNothing().when(mDataSourceTransactionManager).commit(any(TransactionStatus.class));
doNothing().when(mDataSourceTransactionManager).rollback(any(TransactionStatus.class));
But I've always got this error:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
0 matchers expected, 1 recorded:
-> at com.nuance.entrd.mc.nes.jobs.JobPollerTest.runBeforeEachTest(JobPollerTest.java:43)
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
For more info see javadoc for Matchers class.
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:337)
Error which doesn't make any sense in this context. Any idea on how can I address this ?
Thanks