JPARepository 保存方法 returns 为空?

JPARepository save methods returns null?

我想为服务编写测试,但看起来 JPARepository.save 方法 returns 为空。 这是我来自 ServiceTest

的代码
@ExtendWith(MockitoExtension.class)

class PatientServiceTest {
@Mock
private PatientRepository patientRepository;
@Spy
private PatientMapper mapper = new PatientMapper();
@InjectMocks
private PatientService serviceUnderTests;

@Test
void whenCreatePatientByService_thenPatientRepositoryIsNotEmpty() {
    PatientModel patientModel = new PatientModel(134, "Pawel", "Nowak", 
    "pawel@test.pl", "123456789");
    serviceUnderTests.create(patientModel);

    assertNotNull(patientRepository.findAll());
}

服务代码

public PatientModel create(PatientModel patientModel) {
    PatientEntity patientEntity = mapper.from(patientModel);

    PatientEntity savedPatient = repository.save(patientEntity);
    return mapper.from(savedPatient);
}

这是调试模式的屏幕截图:

Repository 只是接口扩展JPARepository,而mapper 只有两个方法,将实体映射到模型和模型到实体。

IntelliJ 说:

java.lang.NullPointerException: Cannot invoke "com.nowakpawel.healthcenter.repository.entity.PatientEntity.getId()" because "entity" is null

    at com.nowakpawel.healthcenter.service.mapper.PatientMapper.from(PatientMapper.java:24)
    at com.nowakpawel.healthcenter.service.PatientService.create(PatientService.java:29)
    at com.nowakpawel.healthcenter.service.PatientServiceTest.whenCreatePatientByService_thenPatientRepositoryIsNotEmpty(PatientServiceTest.java:44)

为什么当我想在 Service 中将实体映射到模型时,Java 将 null 放入 from 方法,而 PatientEntity 不为空?

PatientModel 作为参数传递给该方法,并将其转换为 PatientEntity patientEntity 对象。由于它是一个单元测试并且 PatientRepository 已被模拟,因此当某些东西被模拟时,您需要 return 模拟的值。
所以值应该是 returned for the statement

PatientEntity savedPatient = repository.save(patientEntity);

你可以这样写测试用例

void whenCreatePatientByService_thenPatientRepositoryIsNotEmpty() {
    PatientModel patientModel = new PatientModel(134, "Pawel", "Nowak", 
    "pawel@test.pl", "123456789");
    PatientEntity patientEntity = new PatientEntity();
    patientEntity.setXxxx("Nowak"); // Other values as per getter and setters

    doReturn(patientEntity).when(patientRepository).save(Matchers.any());
    
    PatientModel response = serviceUnderTests.create(patientModel);        
    
    assertNotNull(response);
    asserEquals("Nowak", response.getXxxxx()); //Getter method name here    
}

谢谢你们回答我的问题。我错过了 Mockito 的那一部分;) 但我还是有麻烦。这是我的代码:

@Test
    void whenCreatePatientByService_thenPatientRepositoryIsNotEmpty() {
        PatientModel patientModel = new PatientModel(14, "Pawel", "Nowak", "pawel@test.pl",
                "123456789");
    
    PatientEntity entity = mapper.from(patientModel);
    when(patientRepository.save(entity)).thenReturn(entity);
    serviceUnderTests.create(patientModel);
    assertNotNull(patientRepository.findAll());
}

出现错误:

org.mockito.exceptions.misusing.PotentialStubbingProblem: 
Strict stubbing argument mismatch. Please check:
 - this invocation of 'save' method:
    patientRepository.save(
    PatientEntity [id=14, firstName='Pawel', lastName='Nowak', emailAddress='pawel@test.pl', phoneNumber='123456789']
);
-> at com.nowakpawel.healthcenter.service.PatientService.create(PatientService.java:28)
 - has following stubbing(s) with different arguments:
    1. patientRepository.save(
    PatientEntity [id=14, firstName='Pawel', lastName='Nowak', emailAddress='pawel@test.pl', phoneNumber='123456789']
);
      -> at com.nowakpawel.healthcenter.service.PatientServiceTest.whenCreatePatientByService_thenPatientRepositoryIsNotEmpty(PatientServiceTest.java:48)
Typically, stubbing argument mismatch indicates user mistake when writing tests.
Mockito fails early so that you can debug potential problem easily.
However, there are legit scenarios when this exception generates false negative signal:
  - stubbing the same method multiple times using 'given().will()' or 'when().then()' API
    Please use 'will().given()' or 'doReturn().when()' API for stubbing.
  - stubbed method is intentionally invoked with different arguments by code under test
    Please use default or 'silent' JUnit Rule (equivalent of Strictness.LENIENT).
For more information see javadoc for PotentialStubbingProblem class.

据我了解-问题是

stubbed method is intentionally invoked with different arguments by code under test

我说得对吗?为什么这是一个错误,而我将实体传递给 repository 保存方法?

这是我的整个项目,以备不时之需 -> https://github.com/nowakpawel/health-center

好的,我找到了解决方案。刚刚将 equalshashCode 方法添加到 patientEntity.

已解决,请关闭此帖。