How to write tests for API based on Google Cloud Endpoints?
-
26-12-2019 - |
Question
Is there some way to test API written using Google Cloud Endpoint using JUnit or other framework?
In documentation there is an example using curl commands and perhaps the logic behind this is to test API on client side only.
When I tried to find some approaches how to test API from server-side, I came across the possibility of writing JUnit tests and invoking HttpURLConnection's to localhost, but there are problems with this approach. For example, the instance of app engine should be already running before testing, but I deploy locally with maven and testing is prior deploying, so if I have broken tests it doesn't deploy dev server and I do not feel that that is the right way to rewrite maven steps.
Edit 1: Found something similar for Python: How to unit test Google Cloud Endpoints
Solution
With Objectify you can do it like this. For example, let's declare our BooksEndpoint
as 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 is not authorized");
}
Account account = AccountService.create().getAccount(user);
ofy().save()
.entity(BookRecord.fromBook(account, book))
.now();
}
}
To test it, you would 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 will look as 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 you want to use Objectify
}
@After
public void tearDown() throws Exception {
testHelper.tearDown();
objectifyService.close();
}
@Test
public void testSaveBook() throws Exception {
// Create Endpoint and execute your method
new BooksEndpoint().saveBook(
Book.create("id", "name", "author"),
new User("example@example.com", "authDomain")
);
// Check what was written into datastore
BookRecord bookRecord = ofy().load().type(BookRecord.class).first().now();
// Assert that it is correct (simplified)
assertEquals("name", bookRecord.getName());
}
}
Note, I'm using BookRecord
and Book
here - those are my Entity and POJO, nothing special.
OTHER TIPS
First, thanks for the link to the Python answer. Regarding Java, this Udacity course is based around a Google Cloud Endpoints project in Java, and it has many code examples on how to test endpoints. The source code is here. I have been trying to reproduce the project in Python, and I unfortunately cannot provide any details based on personal experience with testing endpoints written in Java, but I hope the links help!