如何在Rest API中测试更新功能



我找不到使用JUnit测试update方法的方法。我在嘲笑必要的回购和服务。但是当我调用findById(Mockito.any())时,它找不到任何东西,所以它抛出了一个异常。

这是我的测试课程:

@ExtendWith(MockitoExtension.class)
class ApplicantServiceTest {

@Mock
private ApplicantRepository applicantRepository;
@Mock
private CreditRatingService creditRatingService;
@Mock
private ApplicantMapper applicantMapper;
@InjectMocks
private ApplicantService applicantService;
@Test
void update() {
Applicant applicant = getSampleApplicants().get(1);
Applicant updatedApplicant = new Applicant(1L,2141245L,"ege","c0skun",2523,"21412412",null,null);
Mockito.when(applicantService.update(applicantMapper.toDTO(applicant),Mockito.any())).thenReturn(applicant);
Assert.assertEquals(applicant.getIdentificationNumber(),updatedApplicant.getIdentificationNumber());
Assert.assertEquals(applicant.getPhoneNumber(), updatedApplicant.getPhoneNumber());
Assert.assertEquals(applicant.getFirstName(), updatedApplicant.getFirstName());
assertEquals(applicant.getMonthlyIncome(), updatedApplicant.getMonthlyIncome());
Assert.assertEquals(applicant.getId(), updatedApplicant.getId());
}

这就是我尝试测试的方法:

public Applicant update(ApplicantDTO applicantDTO,Long id) {
Applicant byId = getById(id);
if (applicantDTO.getMonthlyIncome()!=byId.getMonthlyIncome()){
byId.setMonthlyIncome(applicantDTO.getMonthlyIncome());
}
if (!Objects.equals(applicantDTO.getFirstName(), byId.getFirstName())){
byId.setFirstName(applicantDTO.getFirstName());
}
if (!Objects.equals(applicantDTO.getLastName(), byId.getLastName())){
byId.setLastName(applicantDTO.getLastName());
}
if (!Objects.equals(applicantDTO.getPhoneNumber(), byId.getPhoneNumber())){
byId.setPhoneNumber(applicantDTO.getPhoneNumber());
}
if (!Objects.equals(applicantDTO.getIdentificationNumber(), byId.getIdentificationNumber())){
byId.setIdentificationNumber(applicantDTO.getIdentificationNumber());
}
return applicantRepository.save(byId);
}

这是输出:

17:43:19.520 [main] ERROR com.egecoskun.finalproject.services.ApplicantService - Applicant not found by id : null
Related Applicant not found with : [id : null]
EntityNotFoundException(details=Some Special Details)
at com.egecoskun.finalproject.services.ApplicantService.lambda$getById$0(ApplicantService.java:47)
at java.base/java.util.Optional.orElseThrow(Optional.java:403)
at com.egecoskun.finalproject.services.ApplicantService.getById(ApplicantService.java:45)
at com.egecoskun.finalproject.services.ApplicantService.update(ApplicantService.java:75)
at com.egecoskun.finalproject.services.ApplicantServiceTest.update(ApplicantServiceTest.java:136)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...

有人能解释一下如何对我的更新方法进行单元测试吗?

测试代码的问题在于,您混合了对模拟对象(例如applicantRepository(的方法进行模拟和对测试对象(applicantService(调用测试方法。我们使用when(...)来定义Mockito中的模拟行为,然后应该调用实际测试的代码(没有when()(。

您可能想要做的是:模拟按ID检索applicant对象,模拟保存修改后的对象,运行update方法对其进行测试并断言结果。

要模拟applicantRepository的行为,您需要告诉Mockito它的方法调用的预期结果是什么,例如(我对未附加的代码做了一些假设(:

when(applicantRepository.findById(anyLong())).thenReturn(applicantFromDb);
when(applicantRepository.save(notNull())).thenAnswer(returnsFirstArg());

(applicantFromDb是在测试方法中创建的对象(

之后,当您调用更新方法时,将使用上面模拟的行为:

Applicant result = applicantService.update(...);

现在,您可以断言测试方法返回的结果。

ApplicationService是您正在测试的类。您得到的异常来自在测试update()方法开始时调用的ApplicationService.getById()方法。您没有向我们展示该方法,但据推测,它调用存储库以通过id获取实例。您的存储库mock没有设置为在请求任何实体时返回任何内容,因此它返回null。这似乎触发了getById()方法中的orElseThrow子句。

因此,您看到的很可能是预期的行为——当尝试使用存储库中不存在的id进行更新时(存储库返回null(,服务会抛出异常。如果要测试实体所在的情况,则需要适当地模拟存储库。

最新更新