java


Assert 2 Lists<String> are equal ignoring order


I'm using assertJ and I'm trying to assert that 2 List<String> have the same strings in their arrays ignoring the order.
List<String> expected = Arrays.asList("Something-6144-77.pdf", "d-6144-77.pdf", "something-6144-78.pdf", "Something-6144-8068.pdf");
List<String> actual = new ArrayList<String>();
assertThat(actual.size()).isEqualTo(expected.size());
// This line gives the error: "The method containsExactlyInAnyOrder(String...) in the type ListAssert<String> is not applicable for the arguments (List<String>)"
assertThat(actual).containsExactlyInAnyOrder(expected);
How can I fix the error that is appearing when trying to use containsExactlyInAnyOrder: "The method containsExactlyInAnyOrder(String...) in the type ListAssert<String> is not applicable for the arguments (List<String>)"?
The error message gives you the solution:
The method containsExactlyInAnyOrder(String...)
String... is a any number of strings but can be passed as an array as well:
assertThat(actual).containsExactlyInAnyOrder((String[]) expected.toArray(new String[expected.size()]));
The cast is necessary here and that code is given under the assumption that the expected element is created different than in your example, as it doesn't make sense to convert an array to a list and back.
Here some documentation to varargs (Arbitrary number of arguments, the ...): https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html
Since the method takes String..., you should pass an array instead of a list:
String[] expected = new String[] {
"Something-6144-77.pdf"
, "d-6144-77.pdf"
, "something-6144-78.pdf"
, "Something-6144-8068.pdf"
};
or call it with the list of items inlined:
assertThat(actual).containsExactlyInAnyOrder(
"Something-6144-77.pdf"
, "d-6144-77.pdf"
, "something-6144-78.pdf"
, "Something-6144-8068.pdf"
);

Related Links

BlackBerry - get error code 411
how to create draggable toggle button in Android
Permutation of a 2 dimensional arraylist
Cast a String object to XMLNode object
how to resolve eclipse error Source not found
Can Mirth send JavaBeans on JMS?
How are Java interfaces used? [closed]
Using a external jar. No suitable driver found for jdbc:sqlserver
Java synchronizing a method in a class
Hibernate : refresh on detached object - weird behavior
Java program Not running in Eclipse [duplicate]
Make Graphics from JPanel visible outside JPanel
stdint.h for Java? [duplicate]
Design an Avro schema basis on my JSON document
creation of jtable of this format in java
Java - Synchronized

Categories

HOME
testing
openlayers
react-virtualized
fme
jsp-tags
yahoo-oauth
frameworks
ckeditor
yum
swagger-ui
node-notifier
session-variables
metatrader4
adobe-analytics
spring-xd
modx-revolution
php-7.1
phaser
leiningen
percona
try-catch
progressive-web-apps
pc
http-status-code-504
undefined
physics-engine
jquery-ajaxq
intentfilter
sox
emgucv
opennlp
sparse-matrix
underflow
greendao
spring-mybatis
wtx
tinymce-4
gammu
geopositioning
adobe-premiere
gesture
mixture-model
twitch
unspecified
azure-ml
android-tabhost
sfdc
ibpy
qwt
logfiles
menuitem
smartcontracts
jquery-nestable
blogengine.net
pebble-watch
recursive-datastructures
kbuild
knpmenubundle
hittest
account-kit
parallel-data-warehouse
grails-tomcat-plugin
savon
home-directory
disque
simplewebrtc
pagedlist
ios4
itextpdf
wso2cloud
collapse
comobject
simple-framework
tableau-online
key-management
graph-api-explorer
directoryservices
formatjs
fpml
javafx-webengine
markers
html-helper
document-classification
jquery-layout
phpthumb
dealloc
quartz-graphics
ruby-datamapper
random-seed
spring-validator
flashvars
wsdl-2.0
free-variable
selected
funscript
dice
multipage
parametric-equations
hgsubversion
whoosh
joyent
calling-convention
visitor-statistic
mirah
thunderbird-lightning
pascal-fc
ugc
rtml

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App