我有一个 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 试过,效果一样。任何想法如何解决它?