如果我的测试中有validation,期望是多余的吗?

我对期望和validation的目的和差异感到困惑。 例如

@Tested FooServiceImpl fooService; @Injectable FooDao fooDao; @Test public void callsFooDaoDelete() throws Exception { new Expectations() {{ fooDao.delete(withEqual(1L)); times = 1; }}; fooService.delete(1L); new Verifications() {{ Long id; fooDao.delete(id = withCapture()); times = 1; Assert.assertEquals(1L, id); }}; } 

首先,如果这个测试编写得不好,请告诉我。

第二,我的问题:期望部分对我来说似乎是多余的,我无法想出一个不会出现的例子。

Expectations的目的是允许测试记录被测试代码所需的模拟方法和/或构造函数的预期结果

Verifications的目的是允许测试Verifications被模拟方法和/或构造函数的预期调用 ,如测试代码所做的那样。

因此,通常,测试不会同时记录validation相同的期望(其中“期望”指定对运行时测试代码期望发生的模拟方法/构造函数的一组调用)。

考虑到这一点,示例测试将如下所示:

 @Tested FooServiceImpl fooService; @Injectable FooDao fooDao; @Test public void callsFooDaoDelete() throws Exception { fooService.delete(1L); new Verifications() {{ fooDao.delete(1L); }}; }