@Test
    public void shouldDemonstrateMocking() {
        // intercept the terminating step of toList() and return whatever results needed for the nature of
        // the test. you could just have easily mocked next() or other terminators. they key to this model lies
        // in all other methods simply returning the mock itself so that the chaining aspect of hooking steps
        // together continues to return the mock GraphTraversal. be sure to mock whatever terminator that you
        // intend to use or it will fall into that default path and there will likely be an error or other
        // unexpected behavior
        final GraphTraversal<?,?> mockTraversal = mock(GraphTraversal.class, invocation -> {
            if (invocation.getMethod().getName().equals("toList"))
                return Arrays.asList("peter", "lop", "josh");
            else
                return invocation.getMock();
        });

        // the mock of GraphTraversalSource is basically "g". Your test would want to inject this "g" to your
        // application logic to use rather than a real one that is probably produced by traversal().withRemote(...).
        // the mock returns either itself or the mock of GraphTraversal depending on the return type of the method
        // called. all other calls go to the real method. that could result in errors but if you're just testing the
        // Gremlin language (and that's the use case here) this configuration should be sufficient.
        //
        // as an aside, it may be tempting to try to mock Graph so that it can be given directly to
        // traversal().withEmbedded(...) but that turns out to be hard based on how the construction happens in there
        // with reflection that seems to bypass the mock attempts. this approach seems simpler but just requires your
        // application logic to take the mock "g" somehow.
        final GraphTraversalSource mockG = mock(GraphTraversalSource.class, invocation -> {
            final Class<?> returnType = invocation.getMethod().getReturnType();
            if (returnType.isAssignableFrom(GraphTraversalSource.class)) {
                return invocation.getMock();
            } else if (returnType.isAssignableFrom(GraphTraversal.class)) {
                return mockTraversal;
            } else {
                return invocation.callRealMethod();
            }
        });

        final GraphTraversalSource g = mockG; // traversal().withRemote(...)
        final List<Object> l = g.V().values("name").order().by(Order.shuffle).limit(3).toList();
        assertThat(l, containsInAnyOrder("peter", "lop", "josh"));
    }
This test demonstrates how one method for using Mockito to mock {@code g} to help with unit testing
applications where the logic of the query itself does not need to be validated, but rather surrounding
logic of the application. It is really important to note that all internal processing of the query is
ignored under this model and no TinkerPop code is executed so even invalid queries that are incorrect
will return the static data encoded to the mock.
gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/graph/GraphTraversalSourceTest.java