SoftwareEngineering/Java/UnitTest
package org.codereign.mockito.tutorial.tutorial01;
import static org.mockito.Mockito.*;
import java.util.List;
import org.junit.Test;
public class Tutorial01Test {
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void test() {
// ---------------------------------------------------------
// Arrange
// ....モック化
// ---------------------------------------------------------
List mockedList = mock(List.class);
// ---------------------------------------------------------
// Act
// ---------------------------------------------------------
mockedList.add("one");
mockedList.clear();
// ---------------------------------------------------------
// Assert
// ---------------------------------------------------------
verify(mockedList).add("one");
verify(mockedList).clear();
}
}
package org.codereign.mockito.tutorial.tutorial02;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.LinkedList;
import org.junit.Test;
public class Tutorial02Test {
@SuppressWarnings("rawtypes")
@Test
public void test1() {
// ---------------------------------------------------------
// Arrange
// ....モック化
// ---------------------------------------------------------
LinkedList mockedList = mock(LinkedList.class);
when(mockedList.get(0)).thenReturn("first");
when(mockedList.get(1)).thenThrow(new RuntimeException());
// ---------------------------------------------------------
// Act
// ---------------------------------------------------------
Object actual = mockedList.get(0);
// ---------------------------------------------------------
// Assert
// ---------------------------------------------------------
assertThat(actual, is((Object) "first"));
verify(mockedList).get(0);
}
@SuppressWarnings("rawtypes")
@Test
public void test2() {
// ---------------------------------------------------------
// Arrange
// ....モック化
// ---------------------------------------------------------
LinkedList mockedList = mock(LinkedList.class);
when(mockedList.get(0)).thenReturn("first");
when(mockedList.get(1)).thenThrow(new RuntimeException());
// ---------------------------------------------------------
// Act & Assert
// ---------------------------------------------------------
try {
mockedList.get(1);
fail("RuntimeException が発生する想定のため到達不可能なはず");
} catch (Exception e) {
assertThat(e, is(instanceOf(RuntimeException.class)));
}
verify(mockedList).get(1);
}
@SuppressWarnings("rawtypes")
@Test
public void test3() {
// ---------------------------------------------------------
// Arrange
// ....モック化
// ---------------------------------------------------------
LinkedList mockedList = mock(LinkedList.class);
when(mockedList.get(0)).thenReturn("first");
when(mockedList.get(1)).thenThrow(new RuntimeException());
// ---------------------------------------------------------
// Act
// ---------------------------------------------------------
Object actual = mockedList.get(999);
// ---------------------------------------------------------
// Assert
// ---------------------------------------------------------
assertThat(actual, is(nullValue()));
verify(mockedList).get(999);
}
}
package org.codereign.mockito.tutorial.tutorial04;
import static org.mockito.Mockito.*;
import java.util.List;
import org.junit.Test;
/**
* 呼び出し回数を検証します。
*/
public class Tutorial04Test {
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void test() {
// ---------------------------------------------------------
// Arrange
// ---------------------------------------------------------
List mockedList = mock(List.class);
// ---------------------------------------------------------
// Act
// ---------------------------------------------------------
mockedList.add("once");
mockedList.add("twice");
mockedList.add("twice");
mockedList.add("three times");
mockedList.add("three times");
mockedList.add("three times");
// ---------------------------------------------------------
// Assert
// ....呼び出し回数が想定していた回数と一致しているか検証する
// ---------------------------------------------------------
verify(mockedList, times(0)).add("never happened");
verify(mockedList, times(1)).add("once");
verify(mockedList, times(2)).add("twice");
verify(mockedList, times(3)).add("three times");
// ---------------------------------------------------------
// ....呼び出し回数が想定していた回数以下であるか検証する
// ---------------------------------------------------------
verify(mockedList, atMost(0)).add("never happened");
verify(mockedList, atMost(1)).add("never happened");
verify(mockedList, atMost(1)).add("once");
verify(mockedList, atMost(2)).add("never happened");
verify(mockedList, atMost(2)).add("once");
verify(mockedList, atMost(2)).add("twice");
verify(mockedList, atMost(3)).add("never happened");
verify(mockedList, atMost(3)).add("once");
verify(mockedList, atMost(3)).add("twice");
verify(mockedList, atMost(3)).add("three times");
// ---------------------------------------------------------
// ....呼び出し回数が想定していた回数以上であるか検証する
// ---------------------------------------------------------
verify(mockedList, atLeast(0)).add("never happened");
verify(mockedList, atLeast(0)).add("once");
verify(mockedList, atLeast(0)).add("twice");
verify(mockedList, atLeast(0)).add("three times");
verify(mockedList, atLeast(1)).add("once");
verify(mockedList, atLeast(1)).add("twice");
verify(mockedList, atLeast(1)).add("three times");
verify(mockedList, atLeast(2)).add("twice");
verify(mockedList, atLeast(2)).add("three times");
verify(mockedList, atLeast(3)).add("three times");
}
}
package org.codereign.mockito.tutorial.tutorial05;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.List;
import org.junit.Test;
/**
* 戻り値を返さないメソッドで例外を発生させる方法
*/
public class Tutorial05Test {
@SuppressWarnings({ "rawtypes" })
@Test
public void test() {
// ---------------------------------------------------------
// Arrange
// ....モック化
// ---------------------------------------------------------
List mockedList = mock(List.class);
doThrow(new RuntimeException()).when(mockedList).clear();
// ---------------------------------------------------------
// Act & Assert
// ---------------------------------------------------------
try {
mockedList.clear();
fail("RuntimeException が発生する想定のため到達不可能なはず");
} catch (Exception e) {
assertThat(e, is(instanceOf(RuntimeException.class)));
}
verify(mockedList).clear();
}
}
package org.codereign.mockito.tutorial.tutorial07;
import static org.mockito.Mockito.*;
import java.util.List;
import org.junit.Test;
/**
* モックが未使用であることを検証する方法
*/
public class Tutorial07Test {
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void test() {
// ---------------------------------------------------------
// Arrange
// ....モック化
// ---------------------------------------------------------
List mockOne = mock(List.class);
List mockTwo = mock(List.class);
List mockThree = mock(List.class);
// ---------------------------------------------------------
// Act
// ---------------------------------------------------------
mockOne.add("one");
// ---------------------------------------------------------
// Assert
// ---------------------------------------------------------
verify(mockOne).add("one");
verify(mockOne, never()).add("two");
verifyNoInteractions(mockTwo, mockThree);
}
}