docs/bugpattern/StaticProtoFuzzer.md
ProtoFuzzer is a mutable class, even when seeded by a compile-time constant. We're trying to avoid the following pitfalls which can arise when assigning a ProtoFuzzer to a static field:
@Before
method. private ProtoFuzzer protoFuzzer;
...
@Before
public void setUp() {
...
// Customize as appropriate
protoFuzzer = ProtoFuzzer.newBuilder().setSeed(...).build();
...
}
private static final ProtoFuzzer protoFuzzer =
ProtoFuzzer.newBuilder()
.setSeed(...)
.build();
...
// Re-ordering myFirstCustomProto and mySecondCustomProto can change their values!
private static final MyCustomProto myFirstCustomProto =
protoFuzzer.makeMessageOfType(
MyCustomProto.getDefaultInstance()
);
private static final MyCustomProto mySecondCustomProto =
protoFuzzer.makeMessageOfType(
MyCustomProto.getDefaultInstance()
);
Instead, create a static builder method and replace references to the static ProtoFuzzer field with calls to the builder:
private static final MyCustomProto myFirstCustomProto =
buildMyCustomProtoFuzzer().makeMessageOfType(
MyCustomProto.getDefaultInstance()
);
private static final MyCustomProto mySecondCustomProto =
buildMyCustomProtoFuzzer().makeMessageOfType(
MyCustomProto.getDefaultInstance()
);
...
private static ProtoFuzzer buildMyCustomProtoFuzzer() {
// Customize as appropriate
return ProtoFuzzer.newBuilder().setSeed(...).build();
}