@Test
  public void testOptimizeWaypointsBeforeWaypoints() throws Exception {
    try (LocalTestServerContext sc =
        new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) {
      List<LatLng> waypoints = getOptimizationWaypoints();
      LatLng origin = waypoints.get(0);
      LatLng destination = waypoints.get(1);
      DirectionsResult result =
          DirectionsApi.newRequest(sc.context)
              .origin(origin)
              .destination(destination)
              .departureTime(Instant.now())
              .optimizeWaypoints(true)
              .waypoints(waypoints.subList(2, waypoints.size()).toArray(new LatLng[0]))
              .await();

      sc.assertParamValue(origin.toUrlValue(), "origin");
      sc.assertParamValue(destination.toUrlValue(), "destination");
      sc.assertParamValue(
          "optimize:true|"
              + waypoints.get(2).toUrlValue()
              + "|"
              + waypoints.get(3).toUrlValue()
              + "|"
              + waypoints.get(4).toUrlValue()
              + "|"
              + waypoints.get(5).toUrlValue(),
          "waypoints");

      assertNotNull(result.toString());
    }
  }
Tests that calling {@code optimizeWaypoints(true)} works in either order.
src/test/java/com/google/maps/DirectionsApiTest.java