Is it possible to override a bean created through the @FeignClient
annotation by just creating a @Configuration
bean that contains a mocked version of it for testing?
I've already tried it but it seems the @FeignClient
bean is created last (or so I think) as in my test I always get injected the real version instead of the mocked one. In the same config file I have another bean created without any annotations (apart from @Component
) mocked the same way by just using the name of the real one and it works perfectly.
I've tried using @MockBean
to mock it and it works but the project has some quirks on it that make the creation of another Spring context break the tests.
Thanks.
EDIT. Actually, I just debugged the test and realised that, if I use the same name as the Feign client, the debugger doesn't even stop in the @Configuration
bean to create the mocked version. Changing the name to something else it works but it just creates another bean of the same type with the new name. Is there any configuration I'm missing here?
EDIT 2. This is an example code. Executing this I have that BarService
is the mocked version but FooService
is the real one.
@FeignClient(name = "fooService")
public interface FooService {
}
@Component
public class BarService {
}
@Configuration
public class ConfigClass {
@Bean
public FooService fooService() {
return Mockito.mock(FooService.class);
}
@Bean
public BarService barService() {
return Mockito.mock(BarService.class);
}
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@SpringBootTest
public class TestClass {
@Autowired
private FooService fooService;
@Autowired
private BarService barService;
@Test
public void test() {
System.out.println(fooService.getClass());
}
}