initial testing work for mockito configuration of behavior
This commit is contained in:
parent
ae6082ccec
commit
a66b72b25e
|
@ -17,6 +17,12 @@
|
|||
<version>15.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- web -->
|
||||
|
||||
<!-- test scoped -->
|
||||
|
@ -73,12 +79,12 @@
|
|||
|
||||
<properties>
|
||||
<!-- Spring -->
|
||||
<org.springframework.version>3.2.4.RELEASE</org.springframework.version>
|
||||
<org.springframework.version>3.2.5.RELEASE</org.springframework.version>
|
||||
<org.springframework.security.version>3.1.4.RELEASE</org.springframework.security.version>
|
||||
|
||||
<!-- persistence -->
|
||||
<hibernate.version>4.2.4.Final</hibernate.version>
|
||||
<mysql-connector-java.version>5.1.26</mysql-connector-java.version>
|
||||
<hibernate.version>4.2.7.SP1</hibernate.version>
|
||||
<mysql-connector-java.version>5.1.27</mysql-connector-java.version>
|
||||
|
||||
<!-- logging -->
|
||||
<org.slf4j.version>1.7.5</org.slf4j.version>
|
||||
|
@ -96,14 +102,14 @@
|
|||
<junit.version>4.11</junit.version>
|
||||
<mockito.version>1.9.5</mockito.version>
|
||||
|
||||
<httpcore.version>4.2.4</httpcore.version>
|
||||
<httpclient.version>4.2.5</httpclient.version>
|
||||
<httpcore.version>4.2.5</httpcore.version>
|
||||
<httpclient.version>4.2.6</httpclient.version>
|
||||
|
||||
<rest-assured.version>1.8.1</rest-assured.version>
|
||||
<groovy.version>1.8.9</groovy.version>
|
||||
|
||||
<!-- Maven plugins -->
|
||||
<cargo-maven2-plugin.version>1.4.3</cargo-maven2-plugin.version>
|
||||
<cargo-maven2-plugin.version>1.4.5</cargo-maven2-plugin.version>
|
||||
<maven-surefire-plugin.version>2.16</maven-surefire-plugin.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue