top of page
Concrete

API Testing using Functional Interfaces

  • Writer: Georgios Kogketsof
    Georgios Kogketsof
  • Dec 17, 2020
  • 2 min read

One of the most frequent question I get in software testing in general is what is the ideal test coverage for a requirement. The most common answer in this question is given by the 2nd rule of testing principles stating: "Exhaustive testing is impossible". Well although this is pretty accurate for manual and exploration testing it is not as accurate for automation testing. In automation testing we have the ability to continuously add test steps to enrich the test case to cover as many cases to reach complete coverage. To elaborate more I will use a real life scenario testing an API integration with various ESPs.


Assume that we want to test a service that connects with various ESPs and periodically we more ESPs are added to this service. To test a service like this we will use Anax testing framework utilising the DataProviders and DataSuppliers functionality.


A DataProvider is a Spring bean that provides input for test steps, usually retrieved from a data source (could be a database, a text file or other source of information), Data-Driven testing.

public interface DataProvider<T> {
    <Τ> List<T> provideTestData();
}

A DataSupplier is a Spring bean that supplies input for test steps, usually retrieved from a data source (could be a database, a text file or other source of information), Data-Driven testing.

public interface DataSupplier<T>  {
    public Stream<Supplier<T>> supplyResults();
}

For our test purposes we will use a DataProvider creating a connection with the ESP and capturing the response in JSON format in order to validate it:

@AnaxTestStep(description = "Verify 200(ok) response", dataprovider ="espConnectionDataProvider")
public void testStep_1(EspDTO espConnection) throws Exception {
    connectionResponse = espExperimentTestData.createConnection(espConnection);
    connections.put(espConnection.getEspName().get(),connectionResponse);
    JSONAssert.assertEquals(expectedResponses.getEspSuccessfulConnection(espConnection,PortalApiUser.builder().build()).toString()
            ,connectionResponse.toString(), JSONCompareMode.LENIENT);
}

In order the above to work we need to create a service called "EspConnectionDataProvider" and this is because spring boot will try to find a bean called as the dataprovider name with the first letter capitalised.

@Service
public class EspConnectionDataProviderCashed<EspDTO> implements DataProvider {
 (List<EspDTO>)Arrays.asList(adobeEspSupplier.get(),brontoEspSupplier.get()
}

The above service will provide a list of EspDTO for test step_1 to be executed as many times as the list size for every EspDTO provided. In order to complete the test we need to implement the supplier as a service:

@Service
public class AdobeEspSupplier implements Supplier<EspDTO> {
    @Override
    public EspDTO get() {
        return EspDTO.builder()
                .espName(EspConnections.ADOBE)
                .name("Adobe Connection"+ GetUniqueIdProvider.getUniqueId())
                .build();
    }
}

Comments


bottom of page