JUnit for GAE:是否有用于后端服务的LocalTestConfig

时间:2013-04-22 07:54:50

标签: google-app-engine

我找不到执行名为“LocalBackendServiceTestConfig”的方法......请从此页面查看 - https://developers.google.com/appengine/docs/java/tools/localunittesting/javadoc/com/google/appengine/tools/development/testing/package-tree。它似乎没有......

所以,如果我有像我这样的JUnit中运行的代码片段:

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);

它将抛出此异常:

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)

如何解决这样的问题?

1 个答案:

答案 0 :(得分:1)

不幸的是,Google不提供此功能。

我通过创建一个模拟测试来解决这个问题。

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;
    }

}
}

但是,在运行配置了后端的开发服务器时,任务队列不起作用。你应该看看this issue(2011年起)。