Fluent api stub with mockito

java

RETURNS_DEEP_STUBS

Fluent api usually results in a long call chain. We can stub the whole method chain with RETURNS_DEEP_STUBS setting in mockito. See:

  public class Foo {
    public Bar bar() {
      return new Bar();
    }
  }
  public class Bar {
    public Baz baz() {
      return new Baz();
    }
  }
  public class Baz {
    public String over() {
      return "end";
    }
  }
  public static void main(String[] args) {
    Foo foo = mock(Foo.class, RETURNS_DEEP_STUBS);
    when(foo.bar().baz().over()).thenReturn("hi");
    System.out.println(foo.bar().baz().over());
  }

From the java doc of Mockito.java:

e99ef77c-06ae-4d7c-998c-be533d918b00