I currently have a project using TestNG to execute tests against my Spring project. Within my project I have a set of Feign interfaces that handle external calls on my Eureka configuration. I am having difficulty understanding how to mock/intercept these calls on a test-by-test basis during execution.
Here is an example of one of my Feign interfaces:
@FeignClient ("http://my-service")
public interface MyServiceFeign {
@RequestMapping (value = "/endpoint/{key}", method = RequestMethod.GET)
SomePojo getByKey(@PathVariable ("key") String key);
}
I have a service that relies on the client:
@Service
public class MyService {
@Autowired
private MyServiceFeign theFeign;
public SomePojo doStuff() {
return theFeign.getByKey("SomeKey");
}
}
My tests are launched simply via:
@SpringBootTest (
classes = Service.class,
webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT
)
@TestExecutionListeners (
inheritListeners = false,
listeners = {
DependencyInjectionTestExecutionListener.class,
TransactionalTestExecutionListener.class
}
)
@DirtiesContext
@ContextConfiguration (initializers = CustomYamlLoader.class)
@ActiveProfiles ("test")
publi class MyModuleTest extends AbstractTestNGSpringContextTests {
// ....
}
What I want to do in my test is execute something like this:
@Test
public void doSomeTest() {
SomePojo fakeReturn = new SomePojo();
fakeReturn.setSomeStuff("some stuff");
/*
!!! do something with the injected feign for this test !!!
setupFeignReturn(feignIntercept, fakeReturn);
*/
SomePojo somePojo = injectedService.doStuff();
Assert.assertNotNull(somePojo, "How did I get NULL in a fake test?");
}
So, here's my dilemma: I am missing a key understanding to be able to do this, I think. That or I am completely missing the concept of how this should be handled. I don't think that using a fallback implementation makes sense here, but I could be wrong.
Help!