Last active
May 29, 2019 12:25
-
-
Save donchan922/d59f8fb9926453a881c6378b32321daa 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
class DemoServiceTest { | |
// テスト対象のクラス内で呼び出すクラス(依存クラス)をモック化する | |
@Mock | |
private DemoRepository demoRepository; | |
// テスト対象のクラスにモックをインジェクションする | |
@InjectMocks | |
private DemoService demoService; | |
@BeforeEach | |
void setup() { | |
// 各テストの実行前にモックオブジェクトを初期化する | |
MockitoAnnotations.initMocks(this); | |
} | |
@Test | |
void findById_idが001の場合() { | |
// 設定 | |
User user = new User("001", "Alice", 23); | |
when(demoRepository.findById("001")).thenReturn(user); | |
// 実行 | |
User result = demoService.findById("001"); | |
// 検証 | |
assertAll( | |
() -> verify(demoRepository, times(1)).findById("001"), | |
() -> assertEquals("001", result.getId()), | |
() -> assertEquals("Alice", result.getName()), | |
() -> assertEquals(23, result.getAge()) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment