public class MainServiceTest {
private SubService subService;
private MainService mainService;
@BeforeEach
public void setUp() {
subService = Mockito.mock(SubService.class);
mainService = new MainService(subService);
}
@Test
public void testExecuteCallsPerformActionTwiceWithDifferentReturnValues() {
// 1回目の呼び出しは0を返し、2回目の呼び出しは1を返すように設定
when(subService.performAction()).thenReturn(0).thenReturn(1);
int[] results = mainService.execute();
assertEquals(0, results[0]);
assertEquals(1, results[1]);
verify(subService, times(2)).performAction();
}
}
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme