我有简单的课程,但有匿名代码块。我需要用测试来覆盖这门课。
public class CleanerTask {
private final Logger log = LoggerFactory.getLogger(getClass());
DataWarehouseMessageDao dwMessageDao;
int cleanerDelay = 0;
TransactionTemplate template;
public CleanerTask(DataWarehouseMessageDao dwMessageDao, int cleanerDelay, TransactionTemplate template) {
this.dwMessageDao = dwMessageDao;
this.cleanerDelay = cleanerDelay;
this.template = template;
}
public void clean() {
log.info("Cleaner started");
final Date olderThan = new Date();
olderThan.setDate(olderThan.getDate() + cleanerDelay);
template.execute(new TransactionCallback<Date>() {
@Override
public Date doInTransaction(TransactionStatus transactionStatus) {
dwMessageDao.deleteAllByStatusAndDate(DataWarehouseMessageStatus.SAVED.getValue(), olderThan);
return olderThan;
}
});
}
}
并测试:
@RunWith(MockitoJUnitRunner.class)
public class CleanerTaskTest {
final static int CLEANER_DELAY = 5;
@Mock
DataWarehouseMessageDao dao;
@Mock
TransactionTemplate template;
CleanerTask cleanerTask;
@Before
public void setUp() throws Exception {
cleanerTask = new CleanerTask(dao, CLEANER_DELAY, template);
}
@Test
public void successfulScenario() {
try {
cleanerTask.clean();
verify(template, times(1)).execute(isA(TransactionCallback.class));
//that verify was not triggered
//verify(dao, times(1)).deleteAllByStatusAndDate(anyInt(), isA(Date.class));
} catch (Exception e) {
e.printStackTrace();
fail("No exceptions must occur");
}
}
}
注释行不起作用。日志:需要但未调用:dao.deleteAllByStatusAndDate( , isA(java.util.Date) ); -> 在 com.nxsystems.dw.publisher.handler.CleanerTaskTest.successfulScenario(CleanerTaskTest.java:52) 实际上,与此模拟的交互为零。
在 com.nxsystems.dw.publisher.handler.CleanerTaskTest.successfulScenario(CleanerTaskTest.java:52) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun .reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable. java:15) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 在 org.junit.internal 的 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) .runners.statements.RunBefores.evaluate(RunBefores.java:28) 在 org.junit.runners.BlockJUnit4ClassRunner。runChild(BlockJUnit4ClassRunner.java:76) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners。 ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit .runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)在 org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62) 在 org.junit.runner.JUnitCore.run(JUnitCore.java:157) 在 com.intellij.rt.execution.junit.JUnitStarter。主要(JUnitStarter.java:62)
此外,当这个 tets 启动时,调试器不会进入匿名块。那么如何让 Mockito 进入匿名区块呢?