简体   繁体   English

单元测试中的LowLevelHttpTransport问题

[英]Problem with LowLevelHttpTransport in unit tests

I'm currently trying to run some unit tests on a class that uses HTTPtransport and is part of an Android application. 我目前正在尝试在使用HTTPtransport并且是Android应用程序一部分的类上运行一些单元测试。 The docs for google-api says that this should be fine since the Apache transport is built into Android. google-api的文档说,这很好,因为Apache传输内置于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) java.lang.IllegalStateException:无法在com.google.api.client.http.HttpTransport。(HttpTransport.java:156)的com.google.api.client.http.HttpTransport.useLowLevelHttpTransport(HttpTransport.java:98)处加载NetHttpTrasnport ),网址为uk.co.uk.co.redfruit.android.whogotwhat.googlebase.GoogleBaseSearch.search(GoogleBaseSearch.java:41)上的com.google.api.client.googleapis.GoogleTransport.create(GoogleTransport.java:58)。 android的android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)上的java.lang.reflect.Method.invokeNative(Native Method)上的redfruit.android.whogotwhat.test.GoogleBaseSearchTest.testSearchForBarcode(GoogleBaseSearchTest.java:22)。在android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430)处进行test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)在android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:1447)处进行测试

Is this a bug? 这是错误吗? Or am I just missing something? 还是我只是想念一些东西?

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. 由于您尝试确定所处环境的方式(使用class.forName()并捕获Exception),所有类路径错误都会导致此操作失败-可能会引起误解。

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. 通过添加apache客户端,重新打包和转义jar,我能够解决问题。 I would suggest that this could perhaps be improved upon. 我建议也许可以对此进行改进。

Is it possible that you are using version 1.1 of the library? 您是否可能正在使用该库的1.1版? I just fixed a bug to make the Apache transport the default in version 1.2. 我刚刚修复了一个错误 ,使Apache传输在版本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. 因此,如果您使用的是1.1版,请尝试切换到1.2,如果不能解决该错误,请让我使用。

Disclaimer: I'm an owner of the google-api-java-client library. 免责声明:我是google-api-java-client库的所有者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM