Mocking return with any passed argument
Mocking return with any passed argument
While trying to mock MavenXpp3Reader the read()
method remains null despite my attempts to mock the return. Here is my attempt
read()
String testVer = "1.0.0.TEST";
MavenXpp3Reader mockReader = mock(MavenXpp3Reader.class);
Model mockModel = mock(Model.class);
when(mockModel.getVersion()).thenReturn(testVer);
when(mockReader.read(new FileReader("pom.xml"))).thenReturn(mockModel);
Model model = mockReader.read(new FileReader("pom.xml"));
model
remains null. Basically, I want to return mockModel
whenever MavenXpp3Reader.read()
is called, no matter what arguments are passed.
model
mockModel
MavenXpp3Reader.read()
2 Answers
2
Try to use any()
from Mockito framework instead of (new FileReader("pom.xml"))
any()
(new FileReader("pom.xml"))
For example:
import static org.mockito.ArgumentMatchers.any;
...
when(mockReader.read(any(Reader.class)).thenReturn(mockModel);
...
Your general idea is correct. But here
any()
cannot work here because read()
is overloaded. So the invocation will be considered as ambiguous by the compiler.– davidxxx
Aug 20 at 21:04
any()
read()
@davidxxx thank you, I have updated my answer
– chmilevfa
Aug 20 at 21:06
Much better now.
– davidxxx
Aug 21 at 8:01
Basically, I want to return mockModel whenever MavenXpp3Reader.read()
is called, no matter what arguments are passed.
You could use Mockito.any()
in the mock recording but it will not compile because MavenXpp3Reader.read()
is overloaded.
You should so specify the class matching to a specific overload :
Mockito.any()
MavenXpp3Reader.read()
when(mockReader.read(Mockito.any(Reader.class))).thenReturn(mockModel);
But in most of case you want to avoid any matcher because that is not strict enough.
About your mock recording :
when(mockReader.read(new FileReader("pom.xml"))).thenReturn(mockModel);
will not be used here :
Model model = mockReader.read(new FileReader("pom.xml"));
because the way which you specify the FileReader
argument (without a soft argument matcher) makes Mockito to rely on the equals()
method of the classes to consider the match and new FileReader("pom.xml").equals(new FileReader("pom.xml"))
returns false
as FileReader
doesn't override equals()
.
But it will work :
FileReader
equals()
new FileReader("pom.xml").equals(new FileReader("pom.xml"))
false
FileReader
equals()
FileReader reader = new FileReader("pom.xml")
when(mockReader.read(reader)).thenReturn(mockModel);
Model model = mockReader.read(reader);
I think question about a bit different thing: I want to return mockModel whenever MavenXpp3Reader.read() is called, no matter what arguments are passed
– chmilevfa
Aug 20 at 20:54
@chmilevfa You are right. I updated about this point.
– davidxxx
Aug 20 at 21:02
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Can Mockito stub a method without regard to the argument?
– Slaw
Aug 20 at 20:52