添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
QCon San Francisco (Oct 2-6): Learn what's next in software from world-class leaders pushing the boundaries. Attend in-person or online. Register The Silent Platform Revolution: How eBPF Is Fundamentally Transforming Cloud-Native Platforms

There is a silent eBPF revolution reshaping platforms and the cloud-native world in its image, and this is its story.

Mockito , the mocking framework for Java unit tests, has released version 5.0.0, switching the default MockMaker interface to mockito-inline in order to better support future versions of the JDK and allows mocking of constructors, static methods and final classes out of the box. The baseline increased from Java 8 to Java 11, as supporting both versions became costly, and managing changes in the JDK, such as the SecurityManager , proved difficult.

Before this release, Mockito didn't support mocking final classes out of the box, such as the following final class which contains one method:

public final class Answer { public String retrieveAnswer() { return "The answer is 2";

In the unit test, a stub is used to replace the answer of the retrieveAnswer() method:

@Test void testMockito() { Answer mockedAnswer = mock(); String answer = "42 is the Answer to the Ultimate Question of Life, the Universe, and Everything"; when(mockedAnswer.retrieveAnswer()).thenReturn(answer); assertEquals(answer, mockedAnswer.retrieveAnswer());

Running the test displays the following exception:

org.mockito.exceptions.base.MockitoException: Cannot mock/spy class com.app.Answer Mockito cannot mock/spy because : - final class

Mockito could mock final classes by supplying the mockito-inline dependency. However, starting with Mockito 5.0.0, the inline MockMaker is used by default and supports mocking of constructors, static methods and final classes. The subclass MockMaker is still available via the new mockito-subclass artifact, which is necessary on the Graal VM native image as the inline mocker doesn't work.

The ArgumentMatcher interface allows the creation of a custom matcher which is used as method arguments for detailed matches. The ArgumentMatcher now supports varargs, with one argument, via the type() method. For example, in order to mock the following method with a varargs argument:

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 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