单元测试proto3生成了带有验证的对象



我正在为Android应用程序使用proto3,并且我在对象相等方面遇到问题,这使得测试非常困难,尤其是对于验证方法

下面是一个表示问题的单元测试:

@Test
public void test_equal () {
PlayerCards playerCards1 = new PlayerCards();
playerCards1.playerId = 1;
playerCards1.cards = new int[]{2};
PlayerCards playerCards2 = new PlayerCards();
playerCards2.playerId = 1;
playerCards2.cards = new int[]{2};

assertThat(playerCards1.toString(), is(playerCards2.toString())); // pass
assertThat(PlayerCards.toByteArray(playerCards1), 
is(PlayerCards.toByteArray(playerCards2))); // pass
assertThat(playerCards1, is(playerCards2)); // <----- fail
}

很明显,方法equals工作不正常,检查生成的代码(附在底部)没有生成equals,生成了hashcode。我可以通过使用CCD_ 4方法来解决CCD_,例如verify(anyMock).anyMethod(playerCards)

我担心,如果在检查时不格外小心,这也可能影响我的运行时。

  • 有没有办法生成equalshashcode
  • 如果不是,我至少可以扩展、覆盖verify,以便在检查原型生成的对象时使用toString

代码片段:

我的proto文件是:

syntax = "proto3";
option java_multiple_files = true;
option optimize_for = LITE_RUNTIME; // existing or not has no effect.
option java_package = "com.package.my";
option java_outer_classname = "Proto";
option objc_class_prefix = "ABC";
package com.package.protos;
message PlayerCards {
int64 playerId = 1;
repeated int32 cards = 2;
}

我通过渐变构建生成文件,并使用以下属性

buildscript {
// ...
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
}
}
apply plugin: 'com.google.protobuf'
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.1.0'
}
generateProtoTasks {
all().each { task ->
task.builtins {
remove java
javanano {
// Options added to --javanano_out
option 'ignore_services=true'
option 'enum_style=java'
option 'generate_intdefs=true'
}
}
}
}
}

dependencies {
// ...
compile 'io.grpc:grpc-protobuf-nano:1.0.2'
}

生成的输出

// Generated by the protocol buffer compiler.  DO NOT EDIT!
package com.package.my.nano;
@SuppressWarnings("hiding")
public final class PlayerCards extends
com.google.protobuf.nano.MessageNano {
private static volatile PlayerCards[] _emptyArray;
public static PlayerCards[] emptyArray() {
// Lazily initializes the empty array
if (_emptyArray == null) {
synchronized (
com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) {
if (_emptyArray == null) {
_emptyArray = new PlayerCards[0];
}
}
}
return _emptyArray;
}
// optional int64 playerId = 1;
public long playerId;
// repeated int32 cards = 2;
public int[] cards;
public PlayerCards() {
clear();
}
public PlayerCards clear() {
playerId = 0L;
cards = com.google.protobuf.nano.WireFormatNano.EMPTY_INT_ARRAY;
cachedSize = -1;
return this;
}
@Override
public void writeTo(com.google.protobuf.nano.CodedOutputByteBufferNano output)
throws java.io.IOException {
if (this.playerId != 0L) {
output.writeInt64(1, this.playerId);
}
if (this.cards != null && this.cards.length > 0) {
for (int i = 0; i < this.cards.length; i++) {
output.writeInt32(2, this.cards[i]);
}
}
super.writeTo(output);
}
@Override
protected int computeSerializedSize() {
int size = super.computeSerializedSize();
if (this.playerId != 0L) {
size += com.google.protobuf.nano.CodedOutputByteBufferNano
.computeInt64Size(1, this.playerId);
}
if (this.cards != null && this.cards.length > 0) {
int dataSize = 0;
for (int i = 0; i < this.cards.length; i++) {
int element = this.cards[i];
dataSize += com.google.protobuf.nano.CodedOutputByteBufferNano
.computeInt32SizeNoTag(element);
}
size += dataSize;
size += 1 * this.cards.length;
}
return size;
}
@Override
public PlayerCards mergeFrom(
com.google.protobuf.nano.CodedInputByteBufferNano input)
throws java.io.IOException {
while (true) {
int tag = input.readTag();
switch (tag) {
case 0:
return this;
default: {
if (!com.google.protobuf.nano.WireFormatNano.parseUnknownField(input, tag)) {
return this;
}
break;
}
case 8: {
this.playerId = input.readInt64();
break;
}
case 16: {
int arrayLength = com.google.protobuf.nano.WireFormatNano
.getRepeatedFieldArrayLength(input, 16);
int i = this.cards == null ? 0 : this.cards.length;
int[] newArray = new int[i + arrayLength];
if (i != 0) {
java.lang.System.arraycopy(this.cards, 0, newArray, 0, i);
}
for (; i < newArray.length - 1; i++) {
newArray[i] = input.readInt32();
input.readTag();
}
// Last one without readTag.
newArray[i] = input.readInt32();
this.cards = newArray;
break;
}
case 18: {
int length = input.readRawVarint32();
int limit = input.pushLimit(length);
// First pass to compute array length.
int arrayLength = 0;
int startPos = input.getPosition();
while (input.getBytesUntilLimit() > 0) {
input.readInt32();
arrayLength++;
}
input.rewindToPosition(startPos);
int i = this.cards == null ? 0 : this.cards.length;
int[] newArray = new int[i + arrayLength];
if (i != 0) {
java.lang.System.arraycopy(this.cards, 0, newArray, 0, i);
}
for (; i < newArray.length; i++) {
newArray[i] = input.readInt32();
}
this.cards = newArray;
input.popLimit(limit);
break;
}
}
}
}
public static PlayerCards parseFrom(byte[] data)
throws com.google.protobuf.nano.InvalidProtocolBufferNanoException {
return com.google.protobuf.nano.MessageNano.mergeFrom(new PlayerCards(), data);
}
public static PlayerCards parseFrom(
com.google.protobuf.nano.CodedInputByteBufferNano input)
throws java.io.IOException {
return new PlayerCards().mergeFrom(input);
}
}

证明缺少文档。

在谷歌上搜索时,我注意到pom(!?)文件generate_equals=true中有以下属性。

将此添加到渐变生成选项后,方法equalshashcode生成!

即。

protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.1.0'
}
generateProtoTasks {
all().each { task ->
task.builtins {
remove java
javanano {
// Options added to --javanano_out
option 'ignore_services=true'
option 'enum_style=java'
option 'generate_intdefs=true'
option 'generate_equals=true' // <--- this one
}
}
}
}
}

最新更新