fnproject / fdk-java

Java API and runtime for fn.
Apache License 2.0
142 stars 53 forks source link

How should I test my external dependencies? #252

Open gdamoreira opened 3 years ago

gdamoreira commented 3 years ago

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";
    }
}