简体   繁体   中英

Unit testing a REST-Service

I have a REST-Service based on the spark-framework . Looks like this(simplyfied):

public void init() {

    get(new Route("spark/favorites") {
        @Override
        public Object handle(Request request, Response response) {
            ExternalService exS= new ExternalService();             

            ArrayList<String> favs= exS.getFavorites();

            Gson gson = getGson();
            return gson.toJson(favs);
        }
    });
 }

Now I want to write some tests for my service to see if my get/post/put/delete does what I want. Therefor I deploy it on an embedded Jetty during my tests.

The problem I a facing now is that my service depends on external REST-Services. I would like to mock all calls to those (to have a fast unit test). But I have no idea how to mock inside the running service.

Is that even possible? Should I switch to another REST-Framework? Suggestions?

that's an integration test, no matter if your app talks to webservice mocks or real 3rd party webservices. Unit test are when you test your classes in isolation.

If you want to mock out external webservices you would need to make links to 3rd party apps configurable and have a separate configuration just for integration testing. For webservice mocking you could use one of several available mocking frameworks: https://sourceforge.net/projects/soaprest-mocker

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM