配置FeignClient中的模拟bean

时间:2017-09-20 14:05:47

标签: java spring spring-cloud-netflix spring-cloud-feign

我有一个Spring Cloud FeignClient:

@FeignClient(name = "AccountSettingsClient", url = "${account.settings.service.url}", decode404 = true,
        configuration = AccountSettingsClientConfig.class, fallbackFactory = AccountSettingsClientFallbackFactory.class)
public interface AccountSettingsClient {
    @RequestMapping(method = RequestMethod.GET, value = "/settings/{uuid}",
            headers = "User-Agent=page/okhttp", consumes = MediaType.APPLICATION_JSON_VALUE)
    AccountSettings accountSettings(@PathVariable("uuid") String uuid);
}

AccountSettingsClientConfig是:

@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(SomeProperties.class)
@EnableFeignClients
public class AccountSettingsClientConfig {
    @Bean
    public RequestInterceptor oauth2FeignRequestInterceptor() {
        return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), resource());
    }
}

现在在集成测试中我需要模拟oauth2FeignRequestInterceptor bean并且它不起作用:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.NONE,
        properties = {"account.settings.service.url=http://localhost:6565/users/accountsettings/"},
        classes = { AccountSettingsClientConfig.class,
                    HttpMessageConvertersAutoConfiguration.class,
                    FeignAutoConfiguration.class, AccountSettingsClientIT.TestConfig.class })
@Slf4j
public class AccountSettingsClientIT {
    @Inject
    private AccountSettingsClient accountSettingsClient;

    @TestConfiguration
    static class TestConfig {
        @Primary
        @Bean
        public RequestInterceptor oauth2FeignRequestInterceptor() {
            return mock(RequestInterceptor.class);
        }
    }
}

我也试过@MockBean,它的效果是一样的。 任何想法如何解决它?

0 个答案:

没有答案