Ask Your Question
2

How to make a Mockito mock method return null?

asked 2021-12-10 11:00:00 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-06-22 18:00:00 +0000

lalupa gravatar image

To make a Mockito mock method return null, you simply need to use the thenReturn method and pass in a null value. Here's an example:

SomeObject mockObject = Mockito.mock(SomeObject.class);
Mockito.when(mockObject.someMethod()).thenReturn(null);

In this example, we create a Mockito mock object of type SomeObject. Then we use the when method to specify that when the someMethod method is called on this object, it should return null.

Note that you can also use the thenAnswer method to define more complex behavior, such as returning null only under certain conditions.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-12-10 11:00:00 +0000

Seen: 16 times

Last updated: Jun 22 '21