3

我正在尝试使用 protobuf 模型编写类的单元测试并在 Android Studio 中运行它们。
例如这是一个简单的转换器测试

class UpdateConfigConverterTest {

    private val testable = UpdateConfigConverter()

    @Test
    fun `should convert from proto model`() {
        val url = "https://test.com"
        val availableVersion = 1
        val requiredVersion = 0
        val result = testable.convert(
            ProtoUpdateConfig.newBuilder()
                .setAvailable(availableVersion)
                .setRequired(requiredVersion)
                .setUrl("https://test.com")
                .build()
        )
        assertEquals(availableVersion, result.updateAvailableVersion)
        assertEquals(requiredVersion, result.updateRequiredVersion)
        assertEquals(url, result.url)
    }
}

待测类:

internal typealias ProtoUpdateConfig = UpdateConfig
internal class UpdateConfigConverter {

    fun convert(source: ProtoUpdateConfig): UpdateConfig =
        UpdateConfig(
            updateAvailableVersion = source.available,
            updateRequiredVersion = source.required,
            url = source.url
        )
}

proto (实际上是内部消息,但对于 flat 是一样的):

syntax = "proto2";

package proto.api.response;

import "proto/api/KeyValue.proto";

message UpdateConfig {
    optional uint32 available = 1; 
    optional uint32 required = 2;
    optional string url = 3;
}

android studio 控制台输出:

java.lang.NoSuchMethodError: proto.api.response.ConfigAndroidOuterClass$ConfigAndroid$UpdateConfig.makeImmutable()V

at proto.api.response.ConfigAndroidOuterClass$ConfigAndroid$UpdateConfig.<clinit>(ConfigAndroidOuterClass.java:3130)
at com.anchorfree.eliteapi.converters.UpdateConfigConverterTest.should convert from proto model(UpdateConfigConverterTest.kt:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

尽管如此.gradlew/testDebugUnitTest成功完成。AS配置等可能有问题。

Android Studio 版本:3.6.0,在 3.5.2 中是一样的。
Protobuf 精简版:3.0.1。
Protobuf gradle 插件:0.8.8。

4

1 回答 1

5

根据这里的这篇文章,在将 Protobuf 与 Robolectric 3.1+ 版一起使用时可能会出现问题。

您可以尝试通过执行以下操作从 Robolectric 中排除 Protobuf 依赖项:

testImplementation ('org.robolectric:robolectric:3.x.x') {
    exclude group: 'com.google.protobuf'
}
于 2020-03-04T15:35:55.340 回答