单元测试中 LowLevelHttpTransport 的问题
我目前正在尝试对一个使用 HTTPtransport 并且属于 Android 应用程序一部分的类运行一些单元测试。 google-api 的文档说这应该没问题,因为 Apache 传输内置于 Android 中。
但是,当运行我的测试时,我收到以下错误:
java.lang.IllegalStateException: 无法加载 NetHttpTrasnport com.google.api.client.http.HttpTransport.useLowLevelHttpTransport(HttpTransport.java:98) 在 com.google.api.client.http.HttpTransport。(HttpTransport.java:156) 在 com.google.api.client.googleapis.GoogleTransport.create(GoogleTransport.java:58) 在 uk.co.redfruit.android.whogotwhat.googlebase.GoogleBaseSearch.search(GoogleBaseSearch.java:41) 在 uk.co.redfruit.android.whogotwhat.test.GoogleBaseSearchTest.testSearchForBarcode(GoogleBaseSearchTest.java:22) 在 java.lang.reflect.Method.invokeNative(本机 方法)在 android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169) 在 android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154) 在 android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430) 在 android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
这是一个错误吗?或者我只是错过了什么?
I'm currently trying to run some unit tests on a class that uses HTTPtransport and is part of an Android application. The docs for google-api says that this should be fine since the Apache transport is built into Android.
However, when running my tests I get the following error:
java.lang.IllegalStateException:
unable to load NetHttpTrasnport at
com.google.api.client.http.HttpTransport.useLowLevelHttpTransport(HttpTransport.java:98)
at
com.google.api.client.http.HttpTransport.(HttpTransport.java:156)
at
com.google.api.client.googleapis.GoogleTransport.create(GoogleTransport.java:58)
at
uk.co.redfruit.android.whogotwhat.googlebase.GoogleBaseSearch.search(GoogleBaseSearch.java:41)
at
uk.co.redfruit.android.whogotwhat.test.GoogleBaseSearchTest.testSearchForBarcode(GoogleBaseSearchTest.java:22)
at
java.lang.reflect.Method.invokeNative(Native
Method) at
android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at
android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at
android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430)
at
android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
Is this a bug? Or am I just missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的 - 我找到了问题所在。由于您尝试确定所处环境的方式(使用 class.forName() 并捕获异常),所有类路径错误都会导致失败 - 这可能会产生误导。
它正确确定了我的环境(因此这与单元测试无关),但随后无法加载其他一些类。
通过添加 apache 客户端、重新打包和转义 jar,我能够解决该问题。我建议这也许可以改进。
OK - I figured out the problem. Due to the way you're trying to determine which environment you're in (using class.forName() and catching Exception) all classpath errors will cause this to fail - which can be misleading.
It was correctly determining my environment (so this was nothing to do with Unit Tests), but then failed to load some other classes.
By adding the apache client, repackaged and escape jars I was able to fix the problem. I would suggest that this could perhaps be improved upon.
您是否有可能使用该库的 1.1 版本?我刚刚修复了 bug 使 Apache 传输成为版本 1.2 中的默认传输。因此,如果您使用的是 1.1 版,请尝试切换到 1.2 版,如果这不能解决错误,请告诉我。
免责声明:我是 google-api-java-client 图书馆。
Is it possible that you are using version 1.1 of the library? I just fixed a bug to make the Apache transport the default in version 1.2. So if you're using version 1.1, please try switching to 1.2 and let me if that doesn't resolve the error.
Disclaimer: I'm an owner of the google-api-java-client library.