java - How to write tests for API based on Google Cloud Endpoints? -


is there way test api written using google cloud endpoint using junit or other framework?

in documentation there example using curl commands , perhaps logic behind test api on client side only.

when tried find approaches how test api server-side, came across possibility of writing junit tests , invoking httpurlconnection's localhost, there problems approach. example, instance of app engine should running before testing, deploy locally maven , testing prior deploying, if have broken tests doesn't deploy dev server , not feel that right way rewrite maven steps.

edit 1: found similar python: how unit test google cloud endpoints

with objectify can this. example, let's declare our booksendpoint follows:

@api(     name = "books",     version = "v1",     namespace = @apinamespace(ownerdomain = "backend.example.com", ownername = "backend.example.com", packagepath = "") ) public class booksendpoint {      @apimethod(name = "savebook")     public void savebook(book book, user user) throws oauthrequestexception, ioexception {         if (user == null) {             throw new oauthrequestexception("user not authorized");         }          account account = accountservice.create().getaccount(user);          ofy().save()                 .entity(bookrecord.frombook(account, book))                 .now();     }  } 

to test it, need following dependencies:

testcompile 'com.google.appengine:appengine-api-labs:1.9.8' testcompile 'com.google.appengine:appengine-api-stubs:1.9.8' testcompile 'com.google.appengine:appengine-testing:1.9.8' testcompile 'junit:junit:4.12' 

now, test follows:

public class booksendpointtest {      private final localservicetesthelper testhelper = new localservicetesthelper(             new localdatastoreservicetestconfig()     );      private closeable objectifyservice;      @before     public void setup() throws exception {         testhelper.setup();         objectifyservice = objectifyservice.begin(); // required if want use objectify     }      @after     public void teardown() throws exception {         testhelper.teardown();         objectifyservice.close();     }      @test     public void testsavebook() throws exception {         // create endpoint , execute method         new booksendpoint().savebook(                 book.create("id", "name", "author"),                 new user("example@example.com", "authdomain")         );          // check written datastore         bookrecord bookrecord = ofy().load().type(bookrecord.class).first().now();          // assert correct (simplified)         assertequals("name", bookrecord.getname());     }  } 

note, i'm using bookrecord , book here - entity , pojo, nothing special.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -