@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;
}
}
我正在测试服务层。
我想知道哪一个应该进一步核实。
下面的代码只是验证是否调用了usermapper的insertuser,或者是否正确地包含了参数,应该进行什么额外的验证?
@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);
}
}
1条答案
按热度按时间lokaqttq1#
您应该测试返回的userid是否具有预期的值。告诉usermapper mock返回一个特定的值,并Assertuserservice也返回了该值。