Last active
August 11, 2018 03:07
-
-
Save ytnk531/a9a645fcc8b480e7cf0aff2165c76060 to your computer and use it in GitHub Desktop.
基本的なモック
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Assert; | |
import org.junit.Test; | |
import java.util.List; | |
import static org.mockito.ArgumentMatchers.anyInt; | |
import static org.mockito.Mockito.mock; | |
import static org.mockito.Mockito.when; | |
public class MockTest { | |
// Listをmockにする。実際のテストでは、このような標準的なクラスはモックにすべきではない。 | |
private List mockedList = mock(List.class); | |
@Test | |
public void test1() { | |
// getが呼ばれたとき引数がintであれば500を返すスタブを作る | |
when(mockedList.get(anyInt())) | |
.thenReturn(500); | |
// getを呼ぶ | |
Object actual = mockedList.get(120); | |
// 500が返る | |
Assert.assertEquals(500, actual); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment