0

I'm not 100% sure whether I'm asking the right question, but my scenario is I got something like this:

class A {
  public List<String> methodA(input) {
     B b = new B();
     TreeMap<> treemap = b.methodB(input);
     
     // do something with treemap
  }

}

class B {
  public TreeMap<> methodB(input) {
     // do something
     return treemap;
  }
}

Now, I want to test methodA, and want to try mock methodB with Mockito. Tried when().thenReturn(), but didn't work. Since there are others consuming class A, I don't want to do a refactor. Do anyone know how can I do this?

Erin L
  • 95
  • 1
  • 5
  • There's are several ways to mock a method, but whichever way you choose you need to make sure you're using the mocked object. Which means A should have a constructor that takes a B as a parameter. – Richard Barker Mar 15 '21 at 23:44
  • Maybe a [constructor mock](https://github.com/mockito/mockito/blob/b19644e7cb530526e8ae5fecfe90523cf3fa2756/subprojects/inline/src/test/java/org/mockitoinline/ConstructionMockTest.java) would help? – JojOatXGME Mar 15 '21 at 23:47

0 Answers0