Is Expectations redundant if I have Verifications in my test?

前端 未结 1 1369
生来不讨喜
生来不讨喜 2021-02-19 19:44

I\'m confused as to the purpose of and difference between expectations and verifications. E.g.

@Tested FooServiceImpl fooService;
@Injectable FooDao fooDao;

@Te         


        
1条回答
  •  青春惊慌失措
    2021-02-19 20:04

    The purpose of Expectations is to allow a test to record expected results for mocked methods and/or constructors, as needed by the code under test.

    The purpose of Verifications is to allow a test to verify expected invocations to mocked methods and/or constructors, as made by the code under test.

    So, normally, a test wouldn't both record and verify the same expectation (where an "expectation" specifies a set of invocations to mocked methods/constructors that are expected to occur when the code under test is exercised).

    With that in mind, the example test would look like this:

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

    0 讨论(0)
提交回复
热议问题