我正在按照 Google 的教程 [ https://developers.google.com/protocol-buffers/docs/javatutorial?hl=en ] 编写一个 Proto 类。
我的原型文件是
package protoc_Shashi;
option java_package="com.shashi.protoc.generated";
option java_outer_classname="AddressBookProtos";
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber number = 4;
}
message AddressBook {
repeated Person person = 1;
}
我正在编写一个 junit 测试用例来创建Person
教程中指定的新对象。
对象已创建但调用它的toString()
方法给我一个错误,它应该被子类覆盖。同样的Stacetrace如下
java.lang.UnsupportedOperationException: This is supposed to be overridden by subclasses.
at com.google.protobuf.GeneratedMessage.getUnknownFields(GeneratedMessage.java:192)
at com.google.protobuf.TextFormat$Printer.print(TextFormat.java:301)
at com.google.protobuf.TextFormat$Printer.printFieldValue(TextFormat.java:434)
at com.google.protobuf.TextFormat$Printer.printSingleField(TextFormat.java:353)
at com.google.protobuf.TextFormat$Printer.printField(TextFormat.java:309)
at com.google.protobuf.TextFormat$Printer.print(TextFormat.java:299)
at com.google.protobuf.TextFormat$Printer.access$400(TextFormat.java:273)
at com.google.protobuf.TextFormat.print(TextFormat.java:76)
at com.google.protobuf.TextFormat.printToString(TextFormat.java:143)
at com.google.protobuf.AbstractMessage.toString(AbstractMessage.java:79)
at com.shashi.protoc.generated.AddressBookProtosTest.createPerson(AddressBookProtosTest.java:29)
我的junit测试类如下
package com.shashi.protoc.generated;
import org.junit.Test;
/**
* @author Shashi Bhushan
* Created on 29/12/15.
* For Google-Protoc
*/
public class AddressBookProtosTest {
@Test
public void createPerson(){
AddressBookProtos.Person person =
AddressBookProtos.Person.newBuilder()
.setId(100)
.setName("Shashi")
.setEmail("sb@sb.com")
.addNumber(
AddressBookProtos.Person.PhoneNumber.newBuilder()
.setNumber("555")
.setType(AddressBookProtos.Person.PhoneType.WORK)
)
.build();
System.out.println("Is Initialized : " +
person.isInitialized());
System.out.println("To String : " +
person.toString()); // Error on this line
}
}
我在这里错过了一些微不足道的事情吗?