I am new to Reactive programming and having trouble testing. I have a very simple scenario,
an Entity:
class SimpleEntity{
@Id
int id;
String name;
}
a related repository:
class SimpleEntityRepository extends JpaRepository<SimpleEntity, Integer>{
Slice<SimpleEntity> findByName(String name, Pageable pageable);
}
a related service:
class SimpleEntityService{
@Autowired
SimpleEntityRepository repository;
public Mono<Slice<SimpleEntity>> findByName(String name, Pageable pageable){
//All business logic excluded
return Mono.just(
repository.findByName(name, pageable);
);
}
}
a related controller:
class SimpleEntityController{
@Autowired
SimpleEntityService service;
@RequestMapping("/some-mapping")
public Mono<Slice<SimpleEntity>> findByName(@Requestparam String name, int pageNum){
return service.findByName(name, Pageable.of(pageNum, 100));
}
}
Now, in my integrations tests, I am trying hit the controller using WebTestClient but I am unable to understand how can I fetch and deserialize the response:
@Test
public void someIntegrationTest(){
WebTestClient.ResponseSpec responseSpec = webTestClient.get()
.uri(URI)
.accept(MediaType.APPLICATION_JSON)
.exchange();
responseSpec.returnResult(SliceImpl.class).getResponseBody.blockFirst();
}
The last line throws the following exception:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of org.springframework.data.domain.Pageable (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information at [Source: UNKNOWN; byte offset: #UNKNOWN] (through reference chain: org.springframework.data.domain.SliceImpl["pageable"])
What I essentially want is to be able to get the Slice and be able to perform assertions over it.
There are several questions here
ParameterizedTypeReference
.WebTestClient
API to validate response and it's not necessary to block. For examplevalue
orconsumeWith
allows to access the body and assert result.