我正在对handler(routerFunction)进行单元测试,但未执行。
处理程序:
@Log4j2
@Component
@RequiredArgsConstructor
public class ReactiveAccommodationHandler implements
AccommodationHandler {
private final ReactiveAccommodationService service;
private final PageResolver pageResolver;
private final WebMapper mapper;
private final ValidatorManager validator;
@Override
@PreAuthorize("hasRole('ACQUISITION_MANAGER')")
public Mono<ServerResponse> updateInternal(final ServerRequest serverRequest) {
final Mono<PutAccommodationRequest> mono = serverRequest
.bodyToMono(AccommodationResource.class)
.map(resource -> mapper.dtoInPut(resource, serverRequest.pathVariable("accommodationId")))
.doOnNext(request -> validator.validate(request));
return noContent()
.build(service.updateInternal(mono));
}}
测试:
@RunWith(MockitoJUnitRunner.class)
public class AccommodationHandlerTest {
@Mock
private ReactiveAccommodationService service;
@Mock
private PageResolver pageResolver;
@Mock
private WebMapper mapper;
@Mock
private ValidatorManager validator;
@InjectMocks
private ReactiveAccommodationHandler handler;
@Test
public void updateInternal() {
//given
final Long accommodationId = RandomUtils.nextLong();
final AccommodationResource accommodation = AccommodationResource
.builder()
.accommodationId(accommodationId)
.build();
final Mono<AccommodationResource> accommodationMono = Mono.just(accommodation);
final String accommodationIdAsString = accommodationId.toString();
final PutAccommodationRequest request = PutAccommodationRequest
.builder()
.accommodationId(accommodationId)
.build();
final ServerRequest serverRequest = MockServerRequest
.builder()
.pathVariable("accommodationId", accommodationIdAsString)
.body(accommodationMono);
//when
when(mapper.dtoInPut(eq(accommodation), eq(accommodationIdAsString))).thenReturn(request);
doNothing().when(validator).validate(request);
when(service.updateInternal(any(Mono.class))).thenReturn(Mono.empty());
final Mono<ServerResponse> monoOut = handler.updateInternal(serverRequest);
//then
StepVerifier
.create(monoOut)
.expectNextMatches(response -> HttpStatus.NO_CONTENT.equals(response.statusCode()))
.expectComplete()
.verify();
verify(mapper).dtoInPut(eq(accommodation), eq(accommodationIdAsString));
}}
源看起来不错,但是,当我运行它时,验证会抱怨它:
Wanted but not invoked:
mapper.dtoInPut(
AccommodationResource(accommodationId=6745422589255220224, name=null,
type=null, billingData=null),
"6745422589255220224"
);
任何帮助都是有益的。
常规的住宿:
@Getter
@Builder
@ToString
@Wither
@JsonInclude(JsonInclude.Include.NON_NULL)
public class AccommodationResource {
@JsonProperty("id")
private final Long accommodationId;
private final String name;
private final Type type;
@JsonCreator
public AccommodationResource(
@JsonProperty("id") final Long accommodationId,
@JsonProperty("name") final String name,
@JsonProperty("type") final Type type
{
this.accommodationId = accommodationId;
this.name = name;
this.type = type;
}
public enum Type {
APARTMENT,
APARTHOTEL,
BED_AND_BREAKFAST,
COUNTRY_HOUSE,
GUEST_HOUSE,
FARM_STAY,
LODGE,
HOLIDAY_HOME,
VILLA,
HOTEL,
HOSTEL,
MOTEL,
RESORT
}
}
答案 0 :(得分:0)
我的猜测是您的问题在这里:
测试:
final ServerRequest serverRequest = MockServerRequest
.builder()
.pathVariable("accommodationId", accommodationIdAsString)
.body(accommodationMono);
final Mono<ServerResponse> monoOut = handler.updateInternal(serverRequest);
来源:
final Mono<PutAccommodationRequest> mono = serverRequest
.bodyToMono(AccommodationResource.class)
.map(resource -> mapper.dtoInPut(resource, serverRequest.pathVariable("accommodationId")))
.doOnNext(request -> validator.validate(request));
return noContent()
.build(service.updateInternal(mono));
这里有些奇怪。
service.updateInternal
在源代码中使用Mono<PutAccommodationRequest>
,但在您的测试中,它使用ServerRequest serverRequest
。
我觉得有点奇怪...