我有以下代码,我想嘲笑。我基本上对模拟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);
}
.............
到目前为止,我已经尝试过这个:
@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));
}
而且:
@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));
但我总是遇到这个错误:
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)
在此上下文中没有任何意义的错误。关于如何解决这个问题的任何想法?
由于
答案 0 :(得分:1)
我解决问题的方法是使用PowerMokito:
@RunWith(PowerMockRunner.class)
@PrepareForTest({DataSourceTransactionManager.class})
public class JobPollerTest {
.....................
private DataSourceTransactionManager mDataSourceTransactionManager;
......................
mDataSourceTransactionManager = PowerMockito.mock(DataSourceTransactionManager.class);
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
Mockito.when(mDataSourceTransactionManager.getTransaction(def)).thenReturn(null);
答案 1 :(得分:0)
第一步是在测试类名称上添加以下行。
@PrepareForTest(DataSourceTransactionManager.class)
public class yourTestName{
第二步是按如下方式添加模拟:
@Mock
DataSourceTransactionManager dstManager;
第三步:
@Mock
TransactionStatus tranStatus;
@test
方法的第四步请初始化如下:
TransactionDefinition transDef=new DefaultTransactionDefinition();
Mockito.when(dstManager.getTransaction(transDef)).thenReturn(tranStatus);
答案 2 :(得分:0)
您可以采取多种措施来解决问题, PowerMock 是其中之一: