Set up a RestTemplateBuilder
in unit tests without psring application context.
MockServerRestTemplateCustomizer mockServerCustomizer = new MockServerRestTemplateCustomizer();
MyService myService = new MyServiceService(new RestTemplateBuilder(mockServerCustomizer));
MockRestServiceServer server = mockServerCustomizer.getServer();
See also javadoc of MockServerRestTemplateCustomizer
Use
org.springframework.test.web.client.match.MockRestRequestMatchers
org.springframework.test.web.client.response.MockRestResponseCreators
this.server.expect(MockRestRequestMatchers.requestTo("..."))
.andRespond(MockRestResponseCreators.withSuccess(..., MediaType.APPLICATION_JSON));
@WebMvcTest( secure = false)
or exclude autoconfiguration:
@WebMvcTestexcludeAutoConfiguration = { MockMvcSecurityAutoConfiguration.class })