I have a gradle project with proto3 generator:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("com.google.protobuf:protobuf-gradle-plugin:0.8.14")
}
}
...
dependencies {
...
implementation("com.google.protobuf:protobuf-java:$protoBufCoreVersion")
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:$protoBufCoreVersion"
}
}
Also, I have config for adding generated source code to classpath:
tasks.register<Jar>("sourcesJar") {
from(sourceSets.main.get().allSource)
archiveClassifier.set("sources")
}
publishing {
publications {
create<MavenPublication>("mavenJava") {
from(components["java"])
artifact(tasks["sourcesJar"])
}
}
}
Finally, when all source code generated from protobuf models, I can see it in a classpath and import it in unit tests. But when I'm trying to run build or manually run unit test, next error happened:
Trying to invoke builder manually in unit tests:
@Test
fun test() {
Device.newBuilder() // compile error
}
Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
class my.project.model.request.Device, unresolved supertypes: com.google.protobuf.GeneratedMessageV3
class my.project.model.request.DeviceOrBuilder, unresolved supertypes: com.google.protobuf.MessageOrBuilder
Adding -Xextended-compiler-checks argument might provide additional information.
Where Device - just a simple model:
message Device {
string id = 1;
Type type = 2;
optional string manufacturer = 3;
optional string model = 4;
optional string software = 5;
}
Which generated into next java class:
public final class Device extends
com.google.protobuf.GeneratedMessageV3 implements
// @@protoc_insertion_point(message_implements:source.request.Device)
DeviceOrBuilder {
private static final long serialVersionUID = 0L;
...
@java.lang.Override
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder() {
return DEFAULT_INSTANCE.toBuilder();
}
How can I use generated models in a test?