[英]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()))
);
}
和 myTest
@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.
當我嘗試調試模式時,我看到我回到了這一步。為什么?
.flatMap(existingDevice -> deviceRepository
在你失敗的測試中,你有:
deviceService.deleteDevice(deviceModel.getDeviceId());
這將創建一個 mono,但從不啟動底層異步任務。 測試完成,並且 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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.