AutoValue classes represent pure data classes, so mocking them should not be necessary. Construct a real instance of the class instead.

Has Fix?

The problem

@AutoValue is used to represent pure data classes. Mocking these should not be necessary: prefer constructing them in the same way production code would.

To make the argument another way: the fact that AutoValue classes are not final is an implementation detail of the way they’re generated. They should be regarded as logically final insofar as they must not be extended by non-generated code. If they were final, they also would not be mockable.

Instead of mocking:

public void test() {
  MyAutoValue myAutoValue = mock(MyAutoValue.class);

Prefer simply constructing an instance:

public void test() {
  MyAutoValue myAutoValue = MyAutoValue.create("foo");


Suppress false positives by adding the suppression annotation @SuppressWarnings("DoNotMockAutoValue") to the enclosing element.