How to test a module that gets data from external services?

Hi all.

There is a certain function that queries the "job" with the external resource. And request and parsing the response goes inside the function.
As you can/need to write a test to test the operation in case of receiving/not receiving certain data?

Thanks in advance.
July 2nd 19 at 13:51
2 answers
July 2nd 19 at 13:53
To make parsing the response in separate function/method/module to be able to test it independently.

As for the query, the easiest way to cover it up e2e tests. Well, or explicitly pass in your function the thing that is in your language/platfora makes requests (e.g. client JS's XMLHTTPRequest or fetch), and in tests it to replace.
Well, it wanted to avoid making reply parsing to a separate function :)
Thank you. - Russell_Larkin commented on July 2nd 19 at 13:56
: why I wanted to avoid? On the contrary independent pieces and are easier to understand and refactor, and so on... - genevieve_Upton commented on July 2nd 19 at 13:59
: Just this piece is guaranteed to not be used by itself relatively small (10-15 rows). Saw no reason to make a separate function - Russell_Larkin commented on July 2nd 19 at 14:02
: nothing, 10-15 lines is just the size of a decent function. To divide a program into pieces is necessary not only for reasons of reuse, and sharing responsibility, improving testability, well, just for simplicity. - genevieve_Upton commented on July 2nd 19 at 14:05
OK, convinced :) - Russell_Larkin commented on July 2nd 19 at 14:08
July 2nd 19 at 13:55
As usual - come up with test data, and then to work with them. Dependencies that are not related to the test logic directly to samokat (in Java I am using Mockito, for your case I think also there is something that simplifies life). In the example below, perhaps there is something useful for you.
import org.junit.Assert;

class Test {

 public static void main(String[] args) {

 String message = "Hello, World!\nCheck this message.";

 ExternalController external = job -> {
 return job.toUpperCase();

 InternalController tested = new InternalController(external);
 String results = tested.processRequest(message);
 Assert.assertEquals("HELLO, WORLD!\nCHECK THIS MESSAGE.\n", results);

class InternalController {
 private ExternalController external;

 InternalController(ExternalController external) {
 this.external = external;

 String processRequest(String message) {
 String[] jobs = message.split("\n");
 StringBuilder results = new StringBuilder();
 for (String job : jobs) {
 String result = external.doWork(job);
 return results.toString();

interface ExternalController {
 String doWork(String job);
Thank you. The main question was how to samokat incoming data, without placing their analysis in a separate function. You understand that, like, in any way, but thought maybe something I do not understand :) - Russell_Larkin commented on July 2nd 19 at 13:58
: It all depends on your flow. You can pull data from the XML. Or explicitly create in your code the appropriate object.
Honestly, I have not quite right, the IOC did. Ideally, it should always return a static data within a single case.
In addition, you can with unit tests is to switch to functional? Maybe then it will be easier to test the required flow? - genevieve_Upton commented on July 2nd 19 at 14:01

Find more questions by tags Modular testing