kontent-ai / java-packages

Delivery Java SDK for Kontent.ai including examples for Spring, Gradle, Kotlin and Java Android app
https://kontent.ai/learn/tutorials/develop-apps/overview/?tech=java
MIT License
14 stars 28 forks source link

Response Faking #138

Closed smithaaron closed 2 years ago

smithaaron commented 2 years ago

Expected result

There's a link in the wiki to 'Faking responses for unit testing`, however there is no corresponding page. Are there any guidelines or examples available for unit testing?

Resources

https://github.com/Kentico/kontent-java-packages/wiki

Simply007 commented 2 years ago

Hello @smithaaron,

I will try to dig and place the wiki page back.

Until then, you could use the tests source code to mock the responses using org.apache.http.localserver.LocalServerTestBase:

https://github.com/Kentico/kontent-java-packages/blob/master/kontent-delivery/src/test/java/kentico/kontent/delivery/DeliveryClientTest.java#L287-L326

this.serverBootstrap.registerHandler(
        String.format("/%s/%s", projectId, "items/on_roasts"),
        (request, response, context) -> {

            response.setEntity(
                    new InputStreamEntity(
                            this.getClass().getResourceAsStream("SampleContentItem.json")
                    )
            );
        });
Simply007 commented 2 years ago

Hello @smithaaron,

I went through the history of the wiki, but the page was never created.

I browsed the code and filled the Faking response page. I also suggested an alternative way of faking responses, which is implemented right in the client.

If you spot any problem or issue, feel free to re-open this issue.