public class Answer {
public int count(String... arguments) {
return arguments.length;
It was always possible to match zero arguments or two or more arguments:
when(mockedAnswer.count()).thenReturn(2);
when(mockedAnswer.count(any(), any())).thenReturn(2);
The mock might also use one
any()
method as argument:
Answer mockedAnswer = mock();
when(mockedAnswer.count(any())).thenReturn(2);
However, before Mockito 5, the
any()
method would match any number of arguments, instead of one argument. The mock above would match with the following method invocations:
mockedAnswer.count()
mockedAnswer.count("one")
mockedAnswer.count("one", "two")
Mockito 5 allows to match exactly one varargs argument with:
when(mockedAnswer.count(any())).thenReturn(2);
Alternatively, Mockito 5 allows matching any number of arguments:
when(mockedAnswer.count(any(String[].class))).thenReturn(2);
Mockito 5 may be used after adding the following Maven dependency:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.0.0</version>
<scope>test</scope>
</dependency>
Alternatively, the following Gradle dependency may be used:
testImplementation 'org.mockito:mockito-core:5.0.0'
More information about Mockito 5.0.0 can be found in the detailed explanations inside the
release notes
on GitHub.
Inspired by this content? Write for InfoQ.
Writing for InfoQ
has opened many doors and increased career opportunities
for me. I was able to deeply engage with experts and thought leaders to learn more about the topics I covered. And I can also disseminate my learnings to the wider tech community and understand how the technologies are used in the real world.
Vivian Hu
DevOps News Editor @InfoQ; Director of Products @Second State
Write for InfoQ
Inspired by this content? Write for InfoQ.
I discovered InfoQ’s contributor program earlier this year and have enjoyed it since then! In addition to providing me with a platform to share learning with a global community of software developers,
InfoQ’s peer-to-peer review system has significantly improved my writing
. If you’re searching for a place to share your software expertise, start contributing to InfoQ.
Oghenevwede Emeni
Articles contributor @InfoQ; Software Developer, CEO @Pact
Write for InfoQ
Inspired by this content? Write for InfoQ.
I started writing news for the InfoQ .NET queue as a way of keeping up to date with technology, but I got so much more out of it. I met knowledgeable people,
got global visibility, and improved my writing skills
.
Edin Kapić
.NET News Editor @InfoQ; Lead Engineer @Vista, former Microsoft MVP
Write for InfoQ
Inspired by this content? Write for InfoQ.
Becoming an editor for InfoQ was one of the
best decisions of my career
. It has challenged me and
helped me grow in so many ways
. We'd love to have more people
join our team
.
Thomas Betts
Lead Editor, Software Architecture and Design @InfoQ; Senior Principal Engineer
Write for InfoQ
Could you be our next Editor-in-Chief?
InfoQ seeks a
full-time Editor-in-Chief
to join C4Media's international, always remote team. Join us to cover the most innovative technologies of our time, collaborate with the world's brightest software practitioners, and help more than 1.6 million dev teams adopt new technologies and practices that push the boundaries of what software and teams can deliver!
Apply
The InfoQ
Newsletter
A round-up of last week’s content on InfoQ sent out every Tuesday. Join a community of over 250,000 senior developers.
View an example
Hello stranger!
You need to
Register an InfoQ account
or
Login
or login to post comments. But there's so much more behind being registered.
Get the most out of the InfoQ experience.
The InfoQ
Newsletter
A round-up of last week’s content on InfoQ sent out every Tuesday. Join a community of over 250,000 senior developers.
View an example
Get a quick overview of content published on a variety of innovator and early adopter technologies
Learn what you don’t know that you don’t know
Stay up to date with the latest information from the topics you are interested in
June 13-15, 2023.
QCon New York International Software Conference returns this June 13-15. Technical leaders who are driving innovation and change in software will share the latest trends and techniques from their real-world projects to help you solve common challenges.
Level-up on emerging software trends and get the assurance you're adopting the right patterns and practices.
SAVE YOUR SPOT NOW