lannyboy
lannyboy

Reputation: 627

JUnit for GAE: is there LocalTestConfig for backend service

I don't find a way to execute something called as "LocalBackendServiceTestConfig"... Check this out from this page - https://developers.google.com/appengine/docs/java/tools/localunittesting/javadoc/com/google/appengine/tools/development/testing/package-tree. It doesn't seem to have one...

So, if I have code snippet to run in my JUnit like this:

TaskOptions taskOptions = TaskOptions.Builder.withUrl(PageConstant.PROCESS_CSV_FILE)
                                .param("blobId", blobId)
                                .param("drive", csvUploadFileModel.getDriveStatus())
                                .param("sites", csvUploadFileModel.getSitesStatus())
                                .header("Host", BackendServiceFactory.getBackendService().getBackendAddress("backend-1"))
                                .method(Method.POST);

it will throw this exception:

Caused by: java.lang.NullPointerException
    at com.google.appengine.api.backends.BackendServiceImpl.getDevAppServerLocalAddress(BackendServiceImpl.java:70)
    at com.google.appengine.api.backends.BackendServiceImpl.getBackendAddress(BackendServiceImpl.java:43)

Any idea of how to solve such an issue?

Upvotes: 0

Views: 141

Answers (1)

Brian Chapman
Brian Chapman

Reputation: 1344

Unfortunately Google does not provide this functionality.

I got around this by creating a mock for testing.

public class BackendServiceFactory {

public static BackendService getBackendService() {
    return new MockBackendService();
}

public static class MockBackendService implements BackendService {

    /*
     * (non-Javadoc)
     * @see com.google.appengine.api.backends.BackendService#getBackendAddress(java.lang.String)
     */
    @Override
    public String getBackendAddress(String arg0) {
        return "localhost:8080";
    }

    /*
     * (non-Javadoc)
     * @see com. google.appengine.api.backends.BackendService#getBackendAddress(java.lang.String,
     * int)
     */
    @Override
    public String getBackendAddress(String arg0, int arg1) {
        // TODO Auto-generated method stub
        return null;
    }

    /*
     * (non-Javadoc)
     * @see com.google.appengine.api.backends.BackendService#getCurrentBackend()
     */
    @Override
    public String getCurrentBackend() {
        // TODO Auto-generated method stub
        return null;
    }

    /*
     * (non-Javadoc)
     * @see com.google.appengine.api.backends.BackendService#getCurrentInstance()
     */
    @Override
    public int getCurrentInstance() {
        // TODO Auto-generated method stub
        return 0;
    }

}
}

However when running the development server with backends configured, task queues don't work. You should have a look at this issue (from 2011).

Upvotes: 1

Related Questions