java
What does Mockito.anyLong() do?
I have to test a method which takes a Long as argument. I have to test it for any value of the Long and also in case it's null. Does Mockito.anyLong() automatically test both cases? null and any value of the Long? Or randomly picks up a value between any long value and null? Considering that these are the docs about anyLong(): anyLong public static long anyLong() any long, Long or null. See examples in javadoc for Matchers class Returns: 0. Thanks in advance.
It's a matcher, not a test value generator. It's used for saying things like "I expect this stubbed method to be called with any long, Long or null but I don't care about the exact value".
If you have long parameter and you manage to pass null as an argument to mock there will be NPE. If you have Long parameter anyLong() will allow Long and null, long will be autoboxed to Long by compiler. Try this public class X { long x(Long arg) { return 0; } public static void main(String[] args) { X x = mock(X.class); when(x.x(anyLong())).thenReturn(0L); System.out.println(x.x(null)); } } it will print 0 0
Related Links
How to format java.time.LocalDate in a textField? [duplicate]
Android widget talk to service and service talk to widget
Bamboo build plan on branches but not on develop
Jave jsoup web scraper
How to get major Safari 10 version number with Selenium 3.3.1?
How to send request Header is “Content-Type”:“application/json” when GET on Volley
Replace repeated xml tags value using regex
Custom Hibernate validator not working in a DropWizard application
JSON assign Java String
Selenium behavior changes in Windows 7 and Windows Server 2016
httpcontext in Java RESTful service
Weblogic Spring application gives error for css and js
How to save and load image move in android?
Deploy multiple Java apps on a server
Java JDBC types and code snippets
As String class is the subclass of Object class how the return type of toString() method in the Object class is String