/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage.verification;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.exceptions.verification.*;
import org.mockitoutil.TestBase;
import java.util.LinkedList;
import static junit.framework.TestCase.fail;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
@SuppressWarnings("unchecked")
public class ExactNumberOfTimesVerificationTest extends TestBase {
private LinkedList<String> mock;
@Before
public void setup() {
mock = mock(LinkedList.class);
}
@Test
public void shouldDetectTooLittleActualInvocations() throws Exception {
mock.clear();
mock.clear();
verify(mock, times(2)).clear();
try {
verify(mock, times(100)).clear();
fail();
} catch (TooLittleActualInvocations e) {
assertThat(e)
.hasMessageContaining("Wanted 100 times")
.hasMessageContaining("was 2");
}
}
@Test
public void shouldDetectTooManyActualInvocations() throws Exception {
mock.clear();
mock.clear();
verify(mock, times(2)).clear();
try {
verify(mock, times(1)).clear();
fail();
} catch (TooManyActualInvocations e) {
assertThat(e)
.hasMessageContaining("Wanted 1 time")
.hasMessageContaining("was 2 times");
}
}
@Test
public void shouldDetectActualInvocationsCountIsMoreThanZero() throws Exception {
verify(mock, times(0)).clear();
try {
verify(mock, times(15)).clear();
fail();
} catch (WantedButNotInvoked e) {}
}
@Test
public void shouldDetectActuallyCalledOnce() throws Exception {
mock.clear();
try {
verify(mock, times(0)).clear();
fail();
} catch (NeverWantedButInvoked e) {
assertThat(e).hasMessageContaining("Never wanted here");
}
}
@Test
public void shouldPassWhenMethodsActuallyNotCalled() throws Exception {
verify(mock, times(0)).clear();
verify(mock, times(0)).add("yes, I wasn't called");
}
@Test
public void shouldNotCountInStubbedInvocations() throws Exception {
when(mock.add("test")).thenReturn(false);
when(mock.add("test")).thenReturn(true);
mock.add("test");
mock.add("test");
verify(mock, times(2)).add("test");
}
@Test
public void shouldAllowVerifyingInteractionNeverHappened() throws Exception {
mock.add("one");
verify(mock, never()).add("two");
verify(mock, never()).clear();
try {
verify(mock, never()).add("one");
fail();
} catch (NeverWantedButInvoked e) {}
}
@Test
public void shouldAllowVerifyingInteractionNeverHappenedInOrder() throws Exception {
mock.add("one");
mock.add("two");
InOrder inOrder = inOrder(mock);
inOrder.verify(mock, never()).add("xxx");
inOrder.verify(mock).add("one");
inOrder.verify(mock, never()).add("one");
try {
inOrder.verify(mock, never()).add("two");
fail();
} catch (VerificationInOrderFailure e) {}
}
}