package com.example.testsample; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) public class SampleAppServiceUnitTest { @InjectMocks private SampleAppService sampleAppService; @Mock private Sample1DomainService sample1DomainService; @Mock private Sample2DomainService sample2DomainService; private static Fixture[] fixtures() { return new Fixture[]{ new Fixture( new Sample1Entity(1L, "Taro", 2L), new Sample2Entity(2L, "Apple"), new SampleResponse(1L, "Taro", "Apple")) }; } @Test public void test() { // Arrange when(sample1DomainService.findById(any())) .thenReturn(new Sample1Entity(1L, "Taro", 2L)); when(sample2DomainService.findById(any())) .thenReturn(new Sample2Entity(2L, "Apple")); // Act final var result = sampleAppService.hoge(); // Assert assertThat(result.id()).isEqualTo(1L); assertThat(result.sample1Name()).isEqualTo("Taro"); assertThat(result.sample2Name()).isEqualTo("Apple"); verify(sample1DomainService, times(1)) .findById(anyLong()); verify(sample2DomainService, times(1)) .findById(anyLong()); } @MethodSource("fixtures") @ParameterizedTest public void parameterizedTest(final Fixture fixture) { assertThat(SampleAppService.convert(fixture.sample1, fixture.sample2)) .isEqualTo(fixture.response); } private record Fixture( Sample1Entity sample1, Sample2Entity sample2, SampleResponse response ) { } }