I'm trying to write unit tests of classes with using protobuf models and run them in Android Studio.
For example this is a simple converter test
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)
}
}
Class under test:
internal typealias ProtoUpdateConfig = UpdateConfig
internal class UpdateConfigConverter {
fun convert(source: ProtoUpdateConfig): UpdateConfig =
UpdateConfig(
updateAvailableVersion = source.available,
updateRequiredVersion = source.required,
url = source.url
)
}
proto (actually it is inner message, but for flat is the same):
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;
}
The android studio console outputs:
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)
Despite that .gradlew/testDebugUnitTest
completes successfully. Probably smth wrong with an AS configuration etc.
Android Studio version: 3.6.0, in 3.5.2 was the same.
Protobuf lite: 3.0.1.
Protobuf gradle plugin: 0.8.8.
According to this post here there may be issues while using Protobuf with Robolectric version 3.1+.
You can try to exclude the Protobuf dependency from Robolectric by doing the following:
testImplementation ('org.robolectric:robolectric:3.x.x') {
exclude group: 'com.google.protobuf'
}