| /* |
| * Copyright (c) 2007 Mockito contributors |
| * This program is made available under the terms of the MIT License. |
| */ |
| |
| package org.mockitousage.verification; |
| |
| import org.assertj.core.api.ThrowableAssert; |
| import org.junit.Test; |
| import org.mockito.Mock; |
| import org.mockito.exceptions.verification.NoInteractionsWanted; |
| import org.mockito.exceptions.verification.TooManyActualInvocations; |
| import org.mockito.exceptions.verification.WantedButNotInvoked; |
| import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent; |
| import org.mockitousage.IMethods; |
| import org.mockitoutil.TestBase; |
| |
| import java.util.List; |
| |
| import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| import static org.junit.Assert.fail; |
| import static org.mockito.Mockito.*; |
| |
| public class BasicVerificationTest extends TestBase { |
| |
| @Mock private List<String> mock; |
| @Mock private List<String> mockTwo; |
| |
| @Test |
| public void shouldVerify() throws Exception { |
| mock.clear(); |
| verify(mock).clear(); |
| |
| mock.add("test"); |
| verify(mock).add("test"); |
| |
| verifyNoMoreInteractions(mock); |
| } |
| |
| @Test(expected=WantedButNotInvoked.class) |
| public void shouldFailVerification() throws Exception { |
| verify(mock).clear(); |
| } |
| |
| @Test |
| public void shouldFailVerificationOnMethodArgument() throws Exception { |
| mock.clear(); |
| mock.add("foo"); |
| |
| verify(mock).clear(); |
| |
| assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { |
| @Override |
| public void call() { |
| verify(mock).add("bar"); |
| } |
| }).isInstanceOf(ArgumentsAreDifferent.class); |
| } |
| |
| @Test |
| public void shouldFailOnWrongMethod() throws Exception { |
| mock.clear(); |
| mock.clear(); |
| |
| mockTwo.add("add"); |
| |
| verify(mock, atLeastOnce()).clear(); |
| verify(mockTwo, atLeastOnce()).add("add"); |
| try { |
| verify(mockTwo, atLeastOnce()).add("foo"); |
| fail(); |
| } catch (WantedButNotInvoked e) {} |
| } |
| |
| @Test |
| public void shouldDetectRedundantInvocation() throws Exception { |
| mock.clear(); |
| mock.add("foo"); |
| mock.add("bar"); |
| |
| verify(mock).clear(); |
| verify(mock).add("foo"); |
| |
| try { |
| verifyNoMoreInteractions(mock); |
| fail(); |
| } catch (NoInteractionsWanted e) {} |
| } |
| |
| @Test |
| public void shouldDetectWhenInvokedMoreThanOnce() throws Exception { |
| mock.add("foo"); |
| mock.clear(); |
| mock.clear(); |
| |
| verify(mock).add("foo"); |
| |
| try { |
| verify(mock).clear(); |
| fail(); |
| } catch (TooManyActualInvocations e) {} |
| } |
| |
| @Test |
| public void shouldVerifyStubbedMethods() throws Exception { |
| when(mock.add("test")).thenReturn(Boolean.FALSE); |
| |
| mock.add("test"); |
| |
| verify(mock).add("test"); |
| } |
| |
| |
| @Test |
| public void shouldDetectWhenOverloadedMethodCalled() throws Exception { |
| IMethods mockThree = mock(IMethods.class); |
| |
| mockThree.varargs((Object[]) new Object[] {}); |
| try { |
| verify(mockThree).varargs((String[]) new String[] {}); |
| fail(); |
| } catch(WantedButNotInvoked e) {} |
| } |
| } |