为 Android 测试 Google 物联网核心客户端
Testing Google IoT Core Client for Android
具有以下单元测试的 Android Things 项目失败:
import com.google.android.things.iotcore.IotCoreClient;
import org.junit.Test;
public class ExampleUnitTest {
@Test
public void clientTest() {
new IotCoreClient.Builder();
}
}
build.gradle 依赖关系:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
compileOnly 'com.google.android.things:androidthings:1.0'
implementation 'com.google.android.things:cloud-iot-core:1.0.0'
testImplementation 'com.google.android.things:cloud-iot-core:1.0.0'
}
错误输出:
java.lang.VerifyError: Expecting a stackmap frame at branch target 36
Exception Details:
Location:
com/google/android/things/iotcore/IotCoreClient$Builder.build()Lcom/google/android/things/iotcore/IotCoreClient; @22: ifnonnull
Reason:
Expected stackmap frame at this location.
Bytecode:
0000000: 2ab4 0013 1203 b800 1e2a b400 1412 06b8
0000010: 001e 2ab4 0015 c700 0e2a bb00 0959 b700
0000020: 1ab5 0015 2ab4 0017 c600 112a b400 16c7
0000030: 000a 2ab8 001f b500 162a b400 19c6 0011
0000040: 2ab4 0018 c700 0a2a b800 1fb5 0018 bb00
0000050: 1059 2ab4 0013 b600 1b2a b400 13b6 001c
0000060: bb00 1259 b700 23b7 0022 4ca7 000d 4cbb
0000070: 000e 592b b700 20bf bb00 0b59 2ab4 0013
0000080: 2ab4 0014 2b2a b400 152a b400 182a b400
0000090: 192a b400 162a b400 1703 b700 1db0
Exception Handler Table:
bci [78, 107] => handler: 110
我可以在 Activity 中很好地实例化生成器,但也想为它编写测试。有没有办法测试我的 IoT Core 连接?
这可能只是一个依赖性问题,但我没有想法。
该库在内部依赖于 Paho MQTT 客户端,它不会作为 public API 的一部分传递给调用者(即 classes 不可见图书馆)。您看到的错误的发生是因为 IotCoreClient.Builder
尝试实例化测试无法看到的内部 MQTT 客户端 class。您可以找到 Cloud IoT Core 客户端 on GitHub.
的来源
除了这个特定错误之外,我建议不要构建这样的测试。它会产生您通常希望在测试中避免的两个问题:
正如所写,这看起来像是一个验证 Cloud IoT 客户端是否正常工作的测试(换句话说,Google 的库代码是否正常工作,而不是您的代码是否正常工作在职的)。单元测试应侧重于验证代码的行为并剔除任何依赖项的实现细节。
一直连接到 Cloud IoT 的单元测试不是密封的,因此很难提供可重复的结果。从不良的互联网连接到意外的设备数据等故障模式都可能导致测试错误地失败。理想情况下,您将为测试提供一个模拟或存根接口,您可以在其中为被测实际代码提供确定性结果。
具有以下单元测试的 Android Things 项目失败:
import com.google.android.things.iotcore.IotCoreClient;
import org.junit.Test;
public class ExampleUnitTest {
@Test
public void clientTest() {
new IotCoreClient.Builder();
}
}
build.gradle 依赖关系:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
compileOnly 'com.google.android.things:androidthings:1.0'
implementation 'com.google.android.things:cloud-iot-core:1.0.0'
testImplementation 'com.google.android.things:cloud-iot-core:1.0.0'
}
错误输出:
java.lang.VerifyError: Expecting a stackmap frame at branch target 36
Exception Details:
Location:
com/google/android/things/iotcore/IotCoreClient$Builder.build()Lcom/google/android/things/iotcore/IotCoreClient; @22: ifnonnull
Reason:
Expected stackmap frame at this location.
Bytecode:
0000000: 2ab4 0013 1203 b800 1e2a b400 1412 06b8
0000010: 001e 2ab4 0015 c700 0e2a bb00 0959 b700
0000020: 1ab5 0015 2ab4 0017 c600 112a b400 16c7
0000030: 000a 2ab8 001f b500 162a b400 19c6 0011
0000040: 2ab4 0018 c700 0a2a b800 1fb5 0018 bb00
0000050: 1059 2ab4 0013 b600 1b2a b400 13b6 001c
0000060: bb00 1259 b700 23b7 0022 4ca7 000d 4cbb
0000070: 000e 592b b700 20bf bb00 0b59 2ab4 0013
0000080: 2ab4 0014 2b2a b400 152a b400 182a b400
0000090: 192a b400 162a b400 1703 b700 1db0
Exception Handler Table:
bci [78, 107] => handler: 110
我可以在 Activity 中很好地实例化生成器,但也想为它编写测试。有没有办法测试我的 IoT Core 连接?
这可能只是一个依赖性问题,但我没有想法。
该库在内部依赖于 Paho MQTT 客户端,它不会作为 public API 的一部分传递给调用者(即 classes 不可见图书馆)。您看到的错误的发生是因为 IotCoreClient.Builder
尝试实例化测试无法看到的内部 MQTT 客户端 class。您可以找到 Cloud IoT Core 客户端 on GitHub.
除了这个特定错误之外,我建议不要构建这样的测试。它会产生您通常希望在测试中避免的两个问题:
正如所写,这看起来像是一个验证 Cloud IoT 客户端是否正常工作的测试(换句话说,Google 的库代码是否正常工作,而不是您的代码是否正常工作在职的)。单元测试应侧重于验证代码的行为并剔除任何依赖项的实现细节。
一直连接到 Cloud IoT 的单元测试不是密封的,因此很难提供可重复的结果。从不良的互联网连接到意外的设备数据等故障模式都可能导致测试错误地失败。理想情况下,您将为测试提供一个模拟或存根接口,您可以在其中为被测实际代码提供确定性结果。