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

How do I get my Java GUI to calculate fields properly?
PoolingNHttpClientConnectionManager: what is timeToLive attribute for?
Sending script tags inside JSON poses a security risk?
Android No Activity found - STILL_IMAGE_CAMERA
Libgdx Android: method onStart() not called after onCreate()
in Android API 23 - why can I not programmatically alter ringtones?
Select/open working set in project explorer programmatically [Eclipse plug-in]
Configuration of Java HttpsURLConnection
Error classCast Element to ExecutableElement
nested exception is javax.naming.CommunicationException: abc.com:389 [Root exception is java.net.ConnectException: Connection timed out]
Springboot, mybatis doesn't map DateTime during Unit Testing with Junit
How can i exclude spring bean from autowiring candidates for multiple profiles?
Join in play2 framework with JPA EntityManager - ResultSet as Map?
Why am I getting this error while trying to parse Json from this website's Json API? [duplicate]
How to deal with an exception thrown by file close? [duplicate]
nutch 2.2 generator java Runtime Exception during curl job failed

Categories

HOME
sendgrid
clips
openstack
isabelle
google-play
layout
framework7
sql-server-2016
baqend
pivotal-cloud-foundry
convolution
directx
add
python-unittest
metatrader4
offline
etl
zebra-printers
quickfix
iron-router
google-static-maps
plunker
facebook-instant-articles
google-search-api
telerik-reporting
tibco-mdm
underflow
url-scheme
wpfdatagrid
nhapi
jspm
facebook-apps
jaxb2
gsoap
instant-messaging
data-manipulation
jspresso
pim
xmlreader
nssegmentedcontrol
revolution-slider
espeak
occlusion
streamsets
ssjs
document.write
streamreader
leading-zero
particles.js
fakeiteasy
modelmapper
angular2-meteor
btrace
apple-news
jquery-nestable
logparser
integrity
idisposable
mplayer
recursive-datastructures
forever
tactic
diagnostics
knockout-components
crypt
akka-cluster
cudafy.net
spring-mongodb
or-tools
word-vba-mac
parallel-data-warehouse
heidisql
savon
paypal-express
holder.js
flash-cs5
iad
sframe
cakephp-3.1
mmc
csquery
fscommand
wordpress-theme-customize
preferences
contenttype
expected-exception
reactfx
poller
dealloc
hyprlinkr
viadeo
terminal-services
uv-mapping
ccss
dotnetnuke-5
dataservice
plasma
ember-app-kit
dice
pysimplesoap
enter
work-stealing
boost-filesystem
netbeans-7.1
calling-convention
pyinotify
w3wp.exe
h.323
gallio
infrastructure
revisions
preference
compiler-specific
javap
mtj
eqatec
thread-local-storage
paul-graham
misv

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