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

Categories

HOME
netbeans
reserved
smarty
octobercms
setup-deployment
electronics
sql-server-2016
blueprintjs
windows-server
awesome-wm
iggrid
spring-kafka
mapserver
collectd
carthage
ghc
openrefine
google-static-maps
code-review
graphicsmagick
excel-2007
bootstrap-material-design
greendao
elasticsearch-ruby
neo4j-spatial
ping
atlassian-crucible
procdump
google-rich-snippets
yadcf
gtrendsr
repo
retina-display
dynamics-crm-2013
ansible-playbook
preconditions
quadratic-programming
mime
outlook-api
http-redirect
businessworks
wptoolkit
babel-core
avconv
taffy
network-flow
hibernate-tools
typescript1.8
typed-lambda-calculus
android-cursor
windows-iot-core-10
iso8601
bind9
dynamics-sl
pintos
passport-google-oauth
phishing
multiple-regression
ipconfig
asp.net5
itextpdf
independentsoft
phpcas
rtbkit
qtableview
jms2
dukescript
splash
sankey-diagram
apache-commons-fileupload
dundas
clipperlib
streambase
directoryservices
rake-task
operator-precedence
pick
neolane
xojo
fluentautomation
prettify
jsctypes
dir
ril
cos
buster.js
isnullorempty
hosts-file
buildr
spring-portlet-mvc
whoosh
django-tagging
xtype
inotifycollectionchanged
fluent-interface
.nettiers
pascal-fc
defensive-programming
w3wp
data-acquisition

Resources

Encrypt Message