14

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

@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);
    }};
}

First of all, please let me know if this test is poorly written/thought out.

Second, my question: the expectations section seems redundant to me, and I can't come up with an example where it wouldn't be.

Thunderforge
  • 19,637
  • 18
  • 83
  • 130
fred
  • 1,812
  • 3
  • 37
  • 57
  • I've changed the title to better reflect your question at the bottom of the page. If I misunderstood the question, feel free to revert my change. – Thunderforge Jun 16 '16 at 23:00
  • Old thread, I know, but my understanding is, loosely and tersely: An `Expectations` block handles things that _may_ happen; a `Verifications` block handles things that _must have_ happened. – Laird Nelson Jul 02 '16 at 16:59

1 Answers1

17

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); }};
}
Rogério
  • 16,171
  • 2
  • 50
  • 63
  • I still don't understand this. In your example you could achieve the same with an Expectations block, right? When do you need a Verifications block? – T3rm1 Dec 09 '15 at 08:39
  • 1
    You "need" a verification block when you want to write tests according to the ["Arrange Act Assert"](http://c2.com/cgi/wiki?ArrangeActAssert) or ["Given When Then"](http://martinfowler.com/bliki/GivenWhenThen.html) style. – Rogério Dec 09 '15 at 14:17
  • @Rogério I understand this answer, but why then can you set a verification in an expectation block? eg: `times`, `minTimes` etc. What scenario would one want to use these verifications in an expectations block? – PDStat Jun 08 '16 at 12:14
  • 1
    For the reason I described in the third paragraph, that is, to avoid writing duplicate code in the test. If the test needs to record a given expectation, and it also happens to want to check a fixed number of matching invocations, it makes more sense to have an API that allows a clean, short test without pointless duplication. – Rogério Jun 08 '16 at 16:03
  • 2
    Basically, if you want to avoid boiler plate code and simple `times`-like verification is enough for you, you can put it in expectations block to make a test easier to develop and read. This may be confusing as you are technically verifying in expectations block but, personally, I can accept that as I see a benefit in simplicity. – wst Apr 28 '17 at 01:53
  • How can you figure out from text `record expected results` that it means (in much simpler words) - `tell to the method what it should return`? – hipokito Mar 09 '21 at 16:16