1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.internal.verification;
6
7import org.junit.Test;
8import org.mockito.exceptions.base.MockitoAssertionError;
9import org.mockito.internal.invocation.InvocationBuilder;
10import org.mockito.internal.invocation.InvocationMatcher;
11import org.mockito.invocation.MatchableInvocation;
12import org.mockito.internal.verification.api.VerificationData;
13import org.mockito.invocation.Invocation;
14
15import java.util.Arrays;
16import java.util.List;
17
18import static org.junit.Assert.*;
19
20public class OnlyTest {
21
22    Only only = new Only();
23
24    public class VerificationDataStub implements VerificationData {
25        private final Invocation invocation;
26        private final InvocationMatcher wanted;
27
28        public VerificationDataStub(InvocationMatcher wanted, Invocation invocation) {
29            this.invocation = invocation;
30            this.wanted = wanted;
31        }
32
33        public List<Invocation> getAllInvocations() {
34            return Arrays.asList(invocation);
35        }
36
37        @Override
38        public MatchableInvocation getTarget() {
39            return wanted;
40        }
41
42        public InvocationMatcher getWanted() {
43            return wanted;
44        }
45    }
46
47    @Test
48    public void shouldMarkAsVerified() {
49        //given
50        Invocation invocation = new InvocationBuilder().toInvocation();
51        assertFalse(invocation.isVerified());
52
53        //when
54        only.verify(new VerificationDataStub(new InvocationMatcher(invocation), invocation));
55
56        //then
57        assertTrue(invocation.isVerified());
58    }
59
60    @Test
61    public void shouldNotMarkAsVerifiedWhenAssertionFailed() {
62        //given
63        Invocation invocation = new InvocationBuilder().toInvocation();
64        assertFalse(invocation.isVerified());
65
66        //when
67        try {
68            only.verify(new VerificationDataStub(new InvocationBuilder().toInvocationMatcher(), invocation));
69            fail();
70        } catch (MockitoAssertionError e) {}
71
72        //then
73        assertFalse(invocation.isVerified());
74    }
75}
76