Ok, after much experimentation here is a solution to test springboot controller & filters via a mocked connection - no webservice, no ports and quick test.
Unfortunately I didn't work out how to do it via @WebFluxTest
and WebClient
, instead MockMvc
can be used to achieve desired result:
@ExtendWith(SpringExtension.class)
@Import({SomeDependencyService.class, SomeFilter.class})
@WebMvcTest(controllers = SomeController.class, excludeAutoConfiguration = SecurityAutoConfiguration.class)
@AutoConfigureMockMvc()
public class SomeControllerTest {
@MockBean
private SomeDependencyService someDependencyService;
@Autowired
private MockMvc mockMvc;
private SomeCustomizedClient subject;
@BeforeEach
public void setUp() {
subject = buildClient();
WebClient webClient = mockClientConnection();
subject.setWebClient(webClient);
}
private WebClient mockClientConnection() {
MockMvcHttpConnector mockMvcHttpConnector = new MockMvcHttpConnector(mockMvc);
WebClient webClient = WebClient.builder().clientConnector(mockMvcHttpConnector).build();
return webClient;
}
@Test
public void sample() {
when(SomeDependencyService.somePersistentOperation(any(), any())).thenReturn(new someDummyData());
SomeDeserializedObject actual = subject.someCallToControllerEndpoint("example param");
assertThat(actual.getData).isEquals("expected data");
}
}
Now it is possible to test your customized client (for example if you have internal java client that contains few important customization like security, etags, logging, de-serialization and uniform error handling) and associated controller (and filters if you @import
them along) at the cost of a unit test.
You do NOT have to bring up entire service to verify the client and controller is working correctly.
WebTestClient
which is designed for testing?WebClient.Builder
to create one and use the auto-config of spring boot. I still don't see why what you want to do cannot be accomplished withWebTestClient
which is just a wrapper around theWebClient
.