3

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)?

dermoritz
  • 12,519
  • 25
  • 97
  • 185

1 Answers1

2

@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.

davvilla
  • 520
  • 6
  • 12
GabLeg
  • 352
  • 4
  • 14
  • thx. i'll try it. "I don't understand why you want to go on a real server in an integration test": because it is an INTEGRATION TEST - these don't have to run on a developer machine. integration tests also run in a different build phase and normally on an CI server providing the necessary environment. – dermoritz Jan 30 '20 at 14:30
  • We just have different definition/naming then ;) My stack is : unit test, integration test (start server locally but mock external services) and e2e test (run on environment with real external services and selenium do the calls). – GabLeg Jan 30 '20 at 16:11
  • 1
    In my case I wanted to run Spring rest test with another mock server, not Spring's own – Kirill Apr 23 '20 at 12:55