fdk-java icon indicating copy to clipboard operation
fdk-java copied to clipboard

How should I test my external dependencies?

Open gdamoreira opened this issue 4 years ago • 0 comments

Hi team,

I'm using FnProject now and I'm using external dependencies to access external resources and I'm trying to mock to test the function without dependencies in it. How should I mock my external resource?

public class FunctionTest {

    @Rule
    public final FnTestingRule testing = FnTestingRule.createDefault();

    @Test
    public void shouldReturnGreeting() {
        testing.givenEvent().withBody("{\"data\":123}").enqueue();
        testing.thenRun(Function.class, "handleRequest");

        FnResult result = testing.getOnlyResult();
        assertEquals("OK", result.getBodyAsString());
    }

}

public class Function {
    public ExternalResource getExternalResource() {
        return new ExternalResource(); // how to mock this?
    }
    public String handleRequest(String inputData) {
        var externalResource = getExternalResource();
        externalResource.doSomething();
        return "OK";
    }
}

gdamoreira avatar Feb 19 '21 16:02 gdamoreira