lundi 4 janvier 2021

I have a question while writing the test code. feat JUnit

@RequiredArgsConstructor
@Service
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    @Transactional
    @Override
    public Long insertUser(UserSaveRequestDto userSaveRequestDto) {
        Long user_id = userMapper.insertUser(userSaveRequestDto);

        return user_id;
    }

}

I'm trying to test the service layer.

I wonder which one should be further verified.

The code below simply verified whether the userMapper's insertUser was called or if the parameters were properly contained, what additional verification should be made?

@ExtendWith(MockitoExtension.class)
public class UserServiceTest {

    @Mock
    UserMapper userMapper;

    @InjectMocks
    UserServiceImpl userService;

    UserSaveRequestDto userSaveRequestDto;

    UserResponseDto userResponseDto;

    @BeforeEach
    void setUp() {
        userSaveRequestDto = UserSaveRequestDto.builder()
            .userName("test")
            .userPhoneNumber("01026137832")
            .build();
    
        userResponseDto = UserResponseDto.builder()
            .userId(1L)
            .userName("test")
            .userPhoneNumber("01026137832")
            .build();
    }

    // Mockito 이용한 테스트 코드
    @DisplayName("Mock을 사용한 insertUser 테스트")
    @Test
    public void insertUser() {
        // given

        // when
        Long userId = userService.insertUser(userSaveRequestDto);

        // then
        ArgumentCaptor<UserSaveRequestDto> captor = ArgumentCaptor.forClass(UserSaveRequestDto.class);

        then(userMapper).should().insertUser(captor.capture());
        assertThat(captor.getValue()).isEqualTo(userSaveRequestDto);
    }

}

Aucun commentaire:

Enregistrer un commentaire