Skip to content

Instantly share code, notes, and snippets.

@donchan922
Last active May 29, 2019 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donchan922/d59f8fb9926453a881c6378b32321daa to your computer and use it in GitHub Desktop.
Save donchan922/d59f8fb9926453a881c6378b32321daa to your computer and use it in GitHub Desktop.
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