如何模拟 Mono.flatMap() 中的方法?

How to mock method in Mono.flatMap()?

我为某些服务编写了单元测试,但我不明白为什么我模拟的某些方法不调用。我尝试测试 testDeleteDeviceWhenDeviceNotFound() 方法并且它通过了,但是当我尝试 testDeleteDeviceSuccess() 时我遇到了问题

@Service
@Slf4j
@AllArgsConstructor
public class DeviceService {

    private DeviceRepository deviceRepository;
    private SyncSender syncSender;

    public Mono<Void> deleteDevice(long deviceId) {
        return deviceRepository
                .findById(deviceId)
                .switchIfEmpty(Mono.error(new NotFoundException()))
                .flatMap(existingDevice -> deviceRepository
                        .delete(existingDevice)
                        .then(syncSender.sendDeviceDelete(existingDevice.getDeviceId()))
                );
    }

} 和我的测试

   @ExtendWith(MockitoExtension.class)
class DeviceServiceTest {
    @Mock
    private DeviceRepository deviceRepository;
    @Mock
    private SyncSender syncSender;
    @InjectMocks
    private DeviceService deviceService;

    @Test
@DisplayName("Test deleteDevice when NotFoundException")
void testDeleteDeviceWhenDeviceNotFound() {
    long deviceId = 100L;
    Mockito.when(deviceRepository.findById(deviceId)).thenReturn(Mono.empty());
    Mono<Void> mono = deviceService.deleteDevice(deviceId);
    StepVerifier
            .create(mono)
            .expectErrorMatches(throwable -> throwable instanceof NotFoundException)
            .verify();
}
    @Test
    @DisplayName("Test deleteDevice success")
    void testDeleteDeviceSuccess() {
        DeviceModel deviceModel = createDeviceModel();
        deviceModel.setDeviceId(100L);
        Mockito.when(deviceRepository.findById(deviceModel.getDeviceId())).thenReturn(Mono.just(deviceModel));
        Mockito.when(syncSender.sendDeviceDelete(anyLong())).thenReturn(Mono.empty());
        Mockito.when(deviceRepository.delete(any(DeviceModel.class))).thenReturn(Mono.empty());
        deviceService.deleteDevice(deviceModel.getDeviceId());

    }
}

junit 的异常,其中一个存根方法在测试执行期间从未在被测代码中实现

    org.mockito.exceptions.misusing.UnnecessaryStubbingException: 
Unnecessary stubbings detected.
Clean & maintainable test code requires zero unnecessary code.
Following stubbings are unnecessary (click to navigate to relevant line of code):
  1. -> at com.ecosoft.dms.device.vending.service.DeviceServiceTest.testDeleteDeviceSuccess(DeviceServiceTest.java:172)
  2. -> at com.ecosoft.dms.device.vending.service.DeviceServiceTest.testDeleteDeviceSuccess(DeviceServiceTest.java:173)
Please remove unnecessary stubbings or use 'lenient' strictness. More info: javadoc for UnnecessaryStubbingException class.

当我尝试调试模式时,我看到我 return 在这个 step.Why?

.flatMap(existingDevice -> deviceRepository

在你失败的测试中,你有:

deviceService.deleteDevice(deviceModel.getDeviceId());

这会创建一个单声道,但不会启动底层异步任务。 测试完成,Mockito 正确报告存根方法从未被调用。

您在其他测试中使用了 StepVerifier,在我看来您也需要在此处使用它来触发底层异步操作

StepVerifier.create(deviceService.deleteDevice(deviceModel.getDeviceId()))
            .verifyComplete();

检查更简单的示例:

@Test
void testMonoNotRun() {
    Mono.fromCallable(() -> {
        System.out.println("Callable run");
        return 1;
    });
}

@Test
void testMonoRunBySubscribe() {
    Mono.fromCallable(() -> {
        System.out.println("Callable run");
        return 1;
    }).subscribe();
}

@Test
void testMonoRunByStepVerify() {
    StepVerifier.create(Mono.fromCallable(() -> {
        System.out.println("Callable run");
        return 1;
    })).verifyComplete();
}