1

I have a Spring Boot application that exposes a rest API which internally acts as a http client for an external rest API.

My Junit test class is annotated with @SpringBootTest, so the application starts and expose their endpoints.

I can use HoverflyRule in Simulation mode to proxy requests to my application rest API, but it doesn't work for the restTemplate used by the application to get to the external endpoints. And that's just I want to test, how the application behave with simulated responses from the external API.

This scenario is not covered by hoverfly-java?, I'm totally wrong and confused?

PRF
  • 821
  • 1
  • 9
  • 16

1 Answers1

0

Hoverfly runs a mocked server for you - it's not aware of the existence of RestTemplate as such. It's your responsibility to tell the rest template to request the correct Hoverfly endpoint.

One possible solution is to have an endpoint as a configuration entry inside your application.properties for instance:

external-resource.endpoint=http://fancy-stuff.com

and inject it into your class:

@Value("${external-resource.endpoint}")
private String endpoint;

restTemplate.get(endpoint, ...);

Then you can have application-test.properties config specifically for your tests where you can have a correct endpoint that points to Hoverfly mocked endpoint:

#Hoverfly mocked endpoint
external-resource.endpoint=http://localhost:9000

In this case, the only thing you have to do is to put @ActiveProfiles("test") on your test class which will activate "test" profile and will take endpoint value pointing to Hoverfly.

Hope it makes sense!

Danylo Zatorsky
  • 5,856
  • 2
  • 25
  • 49
  • Thanks!. It makes sense but I want to make use of the random port selection. I cannot use fixed port numbers in my CI pipeline. – PRF Nov 28 '17 at 13:07