From a66b72b25e899790439921fd469ce2a63d4d8528 Mon Sep 17 00:00:00 2001 From: eugenp Date: Mon, 11 Nov 2013 16:59:04 +0200 Subject: [PATCH] initial testing work for mockito configuration of behavior --- mockito/pom.xml | 18 ++++-- .../mockito/MockitoConfigExamplesTest.java | 60 +++++++++++++++++++ 2 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 mockito/src/test/java/org/baeldung/mockito/MockitoConfigExamplesTest.java diff --git a/mockito/pom.xml b/mockito/pom.xml index e5e2c604ab..39ae7cc94a 100644 --- a/mockito/pom.xml +++ b/mockito/pom.xml @@ -17,6 +17,12 @@ 15.0 + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + @@ -73,12 +79,12 @@ - 3.2.4.RELEASE + 3.2.5.RELEASE 3.1.4.RELEASE - 4.2.4.Final - 5.1.26 + 4.2.7.SP1 + 5.1.27 1.7.5 @@ -96,14 +102,14 @@ 4.11 1.9.5 - 4.2.4 - 4.2.5 + 4.2.5 + 4.2.6 1.8.1 1.8.9 - 1.4.3 + 1.4.5 2.16 diff --git a/mockito/src/test/java/org/baeldung/mockito/MockitoConfigExamplesTest.java b/mockito/src/test/java/org/baeldung/mockito/MockitoConfigExamplesTest.java new file mode 100644 index 0000000000..7861741a79 --- /dev/null +++ b/mockito/src/test/java/org/baeldung/mockito/MockitoConfigExamplesTest.java @@ -0,0 +1,60 @@ +package org.baeldung.mockito; + +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.when; + +import org.junit.Test; +import org.mockito.Mockito; + +@SuppressWarnings("unchecked") +public class MockitoConfigExamplesTest { + + // tests + + @Test + public final void whenMockBehaviorIsConfigured_thenBehaviorIsVerified() { + final MyList listMock = Mockito.mock(MyList.class); + when(listMock.add(anyString())).thenReturn(false); + + final boolean added = listMock.add(randomAlphabetic(6)); + assertThat(added, is(false)); + } + + @Test(expected = IllegalStateException.class) + public final void givenMethodIsConfiguredToThrowException_whenCallingMethod_thenExceptionIsThrown() { + final MyList listMock = Mockito.mock(MyList.class); + when(listMock.add(anyString())).thenThrow(IllegalStateException.class); + + listMock.add(randomAlphabetic(6)); + } + + @Test(expected = NullPointerException.class) + public final void whenMethodHasNoReturnType_whenConfiguringBehaviorOfMethod_thenPossible() { + final MyList listMock = Mockito.mock(MyList.class); + doThrow(NullPointerException.class).when(listMock).clear(); + + listMock.clear(); + } + + @Test + public final void givenBehaviorIsConfiguredToThrowExceptionOnSecondCall_whenCallingOnlyOnce_thenNoExceptionIsThrown() { + final MyList listMock = Mockito.mock(MyList.class); + when(listMock.add(anyString())).thenReturn(false).thenThrow(IllegalStateException.class); + + listMock.add(randomAlphabetic(6)); + } + + @Test(expected = IllegalStateException.class) + public final void givenBehaviorIsConfiguredToThrowExceptionOnSecondCall_whenCallingTwice_thenExceptionIsThrown() { + final MyList listMock = Mockito.mock(MyList.class); + when(listMock.add(anyString())).thenReturn(false).thenThrow(IllegalStateException.class); + + listMock.add(randomAlphabetic(6)); + listMock.add(randomAlphabetic(6)); + } + +}