Reputation: 13038
I need to create an integration test against a REST API. My service is using Resttemplate as HTTP client. The client code is generated from swagger file.
Running the test yields an error java.lang.AssertionError: No further requests expected: HTTP GET
It seems that the test is running against a mock server. How to let the test run against the real server?
This is my current test setup (want to cut out a minimal test frame to get a fast test - booting the complete context is far too slow):
@RunWith(SpringRunner.class)
@Import(value = { TpzConfig.class, TpzServiceRestImpl.class, ManufacturingPlantPhPmMapperImpl.class,
ProductHierarchyMapperImpl.class, PlantMapperImpl.class })
@ActiveProfiles(profiles = { "tpz" })
@RestClientTest
public class TpzServiceRestImplTest {
@Autowired
private TpzService to;
@MockBean
private ProductionPlantService ppService;
@MockBean
private ProductHierarchyService phService;
@Test
public void test() {
List<ProductManufacturer> pmByProductHierarchy = to.pmByProductHierarchy("001100909100100388");
}
}
I need @RestClientTest
to have a bean of RestTemplateBuilder.
Is there a way to configure @RestClientTest
to use the real server (similar to @DataJpaTest
where i can configure not to use h2)?
Upvotes: 5
Views: 1759
Reputation: 372
@RestTemplateTest
give you pre-configured RestTemplateBuilder
and MockRestServiceServer
.
1.You could @Autowired
MockRestServiceServer
and mock expected HTTP calls.
2.Remove the auto configuration :
@RestClientTest(excludeAutoConfiguration = MockRestServiceServerAutoConfiguration.class)
But that make the test kind of slow.. There is maybe a way to optimize it.
3.In another hand, you could remove @RestClientTest
and in a test configuration file, create a bean of RestTemplateBuilder
. Something like this :
@TestConfiguration
public class TestConfig {
@Bean
public RestTemplateBuilder getRestTemplateBuilder() {
return new RestTemplateBuilder();
}
}
After this, add this configuration file in your imports :
@Import(value = { TpzConfig.class, TpzServiceRestImpl.class,
ManufacturingPlantPhPmMapperImpl.class, ProductHierarchyMapperImpl.class,
PlantMapperImpl.class, TestConfig.class })
And you should be good for your test.
Upvotes: 4