-1

I am having one class like this,

class Random {
  public String abc(String one, String two, Map<String, String> three){
    ..
  }
}

Now from test class I have created a mock object like

@Mock
private Random mockRandom;

Then I am trying to mock this like Mockito.lenient().when(mockRandom.abc(any(String.class),any(String.class),any(Map.class))).thenReturn(“Test value”);

After this also when I am running a test case then this abc method is not giving “Test value”.

  • Are you certain that you are calling `abc` *on the mock*? – tgdavies Oct 15 '22 at 06:34
  • Does this answer your question? [Why is my class not using my mock in unit test?](https://stackoverflow.com/questions/74027324/why-is-my-class-not-using-my-mock-in-unit-test) – knittl Oct 15 '22 at 07:47
  • Yes @tgdavies. Please help me out with the Mockito.lenient().when(…) issue if you can – Rohan verma Oct 15 '22 at 08:11
  • I got the answer present there but it was not a solution of this question @knittl – Rohan verma Oct 15 '22 at 08:11
  • 1
    @Rohanverma your question is not answerable. You are not showing the actual test, you are not showing how and where `mockRandom` is used in your SUT. And if you got the answer from the linked question, then why is the answer not a/the solution for this question? – knittl Oct 15 '22 at 08:14
  • Please edit your question to include a complete example showing the test and the class under test. – tgdavies Oct 15 '22 at 10:24

1 Answers1

0

There are two things here which i can think of:

  1. try to use any() instead. It could happen that your arguments are of another type. I think the map.class could be the issue since this is actually a Map<Object,Object> at runtime.

  2. Another thing is that your Random class is final (if you use Lombok @Value for example), so the fix is to make this class not final

Loading
  • 1,098
  • 1
  • 12
  • 25