单元测试 proto3 生成的对象与验证

Unit Test proto3 generated objects with verify

我正在将 proto3 用于 Android 应用程序,但我遇到了 对象相等性 的问题,这使得测试非常困难,尤其是用于验证方法

这是代表问题的单元测试:

@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没有正常工作,检查生成的代码(附在底部)没有等号,生成哈希码。 我可以使用 .toString 方法解决 assertThat 但我找不到任何其他验证方法, 例如。 verify(anyMock).anyMethod(playerCards)

我担心如果我在检查时不是特别小心,这也可能会影响我的运行时间。


代码片段:

我的 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;
}

我通过 gradle build 生成文件并使用以下属性

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 中的以下内容 属性。

将此添加到 gradle 生成选项后,方法 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
                }
            }
        }
    }
}