These are chat archives for spring-cloud/spring-cloud

14th
Feb 2017
Grzegorz Poznachowski
@poznachowski
Feb 14 2017 13:38

Hi, when working with Ribbon Feign I created a mechanism of mocking out an URI to call to and specifying Wiremock address there (Wiremock server is started before the tests).
I have annotation:

@Documented
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@MockBean(value = AbstractLoadBalancer.class, reset = MockReset.NONE)
public @interface WireMock {
}

which mocks out AbstractLoadBalancer and sets URI value in TestExecutionListener:

 @Override
    public void beforeTestClass(TestContext testContext) throws Exception {
        TestContextHelper contextHelper = new TestContextHelper(testContext);
        if (contextHelper.testClassAnnotationNotPresent(WireMock.class)) {
            return;
        }

        port = getFreeServerPort();
        server = new WireMockServer(port);

        AbstractLoadBalancer mockedBalancer = contextHelper.getBean(AbstractLoadBalancer.class);
        when(mockedBalancer.chooseServer(any())).thenReturn(new Server("localhost", port));
        when(mockedBalancer.getLoadBalancerStats()).thenReturn(new LoadBalancerStats("test-load-balancer"));
    }

Everything works fine (mocks are reset in afterTestMethod), but I'd like to do the same for FeignClient which is not using Ribbon (has url parameter set).
There is a FeignClientFactoryBean keeping the url param, but it is used in newly created instance of HardcodedTarget which is
then passed to Targeter. I don't know how can I proceed with that. Any help appreciated :)