I want to learn how to unit test API gateway endpoints, or just in general, before deploying the API.
For example, how could I test something like:
public class someController {
private SomeService someService;
@GET
@Path("...")
public String someMethod(){
return someService.someMethod();
}
}
someSerivce:
public class SomeService{
public String someMethod(){
//make http request to api gateway
return json string
}
}
How would I test "someService" class. I assume the "someController" class would just ensure "someMethod" is being called by making use of Mockito verify(someService).someMethod();
2
Answers
Create a mock of
SomeService
, inject it intoSomeController
(via constructor or setter).Set up the mock behaviour with when/thenReturns. Eg:
Call
SomeController.someMethod()
and check the result.If you are Spring then you can take this further by using
WebMvcTest
with an@Autowired MockMvc
To call the actual path and http method, headers, etc.
Assuming I am reading your question correctly that you want to do a test call one of your service methods and see the response, and if you are using IntelliJ, you can potentially use this plugin [1] with your application to invoke the methods on your service class and see the result.
[1] https://plugins.jetbrains.com/plugin/18529-unlogged
Disclaimer: I am a contributor of that plugin.