0

I'm new in unit testing . I've to test a public method which uses the following private method .

     public void f1({some parameters}){
             List<Vertex> rVertex = fn(vertexId, graphTraversalSource,2);
            for (Vertex vertex : resultVertex) {
                if (checkingForAProperty(vertex.id().toString())) {
                     // some operations
                }
            }
         }


    private List<Vertex> fn(String v, GraphTraversalSource g, int i) {
        return g.V(v).repeat(in().dedup().simplePath()).until(loops().is(i)).toList();
      }

I thought of the following (terrible) approach (Assume g is mocked here ) :

when (g.V(v)).then(X);
when (X.repeat(any)).then(Y);
when (Y.until(any)).then(Z);

But I could'nt decide how to do that . Any suggestions to do this will be really helpful.

Zing
  • 23
  • 6
  • Why does g need to be mocked? – jonrsharpe Apr 12 '20 at 15:18
  • Because I thought , I do'nt need to make actual connection and make queries here. – Zing Apr 12 '20 at 15:21
  • Ah, do those methods have slow side effects? What's the behaviour you're trying to test here? – jonrsharpe Apr 12 '20 at 15:24
  • @jonrsharpe Overall , the code does the query and performs code changes (implementation part) . So I need the manipulate the "fn" method to test the implementation part . – Zing Apr 12 '20 at 15:31
  • 1
    You may want to take a step back and describe/think about your overall testing methodology for graph traversals. What is it that you want to really verify? Perhaps creating a small TinkerGraph with known test data and verifying against that would be more meaningful for example in this case. You may also want to take a look at the way the Apache TinkerPop tests are written. – Kelvin Lawrence Apr 12 '20 at 15:43
  • @KelvinLawrence Yes . Valid points . But in my case creating a new TinkerGraph is not really an option. – Zing Apr 12 '20 at 15:58
  • 1
    Can you expand on what your case is? The previous two questions asked got closed I suspect because people are not clear what exactly you need to be verifying. https://stackoverflow.com/questions/61154074/how-to-write-unit-test-for-these-type-of-methods https://stackoverflow.com/questions/61037521/how-to-write-unit-test-for-this-method-in-java https://stackoverflow.com/questions/61154074/how-to-write-unit-test-for-these-type-of-methods https://stackoverflow.com/questions/61037521/how-to-write-unit-test-for-this-method-in-java – Kelvin Lawrence Apr 12 '20 at 16:36
  • @KelvinLawrence I have given an overview of my case here . – Zing Apr 12 '20 at 17:36

1 Answers1

1

You could do this using deep stubs:

GraphTraversalSource g = mock(GraphTraversalSource.class, RETURNS_DEEP_STUBS);
when(g.V(any()).repeat(any()).until(any()).thenReturn(z);

However, bear in mind that deep stubbing should be used rarely (see wiki):

This sort of stubbing, e.g. mock to return mock, to return mock, etc. should be used very sporadically, ideally never. It clearly points out violation of the Law of Demeter. You don't want to mess with Demeter. Since you have been warned check out Mockito deep stubs.

beatngu13
  • 7,201
  • 6
  • 37
  • 66