Questions tagged [assertj]

AssertJ provides a set of strongly-typed assertions to use for unit testing (either with JUnit or TestNG).

386 questions
12
votes
5 answers

Assertj: How to compare 2 objects list by objects content?

Given the following (quick and missing) code: class Pair{ int x; int y; } List l1 = Arrays.asList(new Match(1,2), new Match(1,3), new Match(2,3)); List l2 = Arrays.asList(new Match(1,2), new Match(1,3), new Match(2,3)); How can I compare the…
dushkin
  • 1,939
  • 3
  • 37
  • 82
12
votes
2 answers

AssertJ: Assert if list is sorted inversely or in descending order

I'm using AssertJ for my tests and I noticed there's a method for checking if a List is sorted: public static void sorted(final List actual) { try { assertThat(actual).isSorted(); } catch (AssertionError e) { …
iamkenos
  • 1,446
  • 8
  • 24
  • 49
12
votes
2 answers

Multiply conditions set in AssertJ assertions?

I'm trying to set multiply conditions on the assertJ and could not find in it examplesGit. I currently write: assertThat(A.getPhone()) .isEqualTo(B.getPhone()); assertThat(A.getServiceBundle().getId()) …
a.k
  • 1,035
  • 10
  • 27
11
votes
2 answers

AssertJ fails to assert BigDecimal equality without scale

I am running assertions like so: assertThat(obj.getTotal()).isEqualTo(BigDecimal.valueOf(4)) I am getting Expecting: <4.00> to be equal to: <4> so then I tried assertThat(obj.getTotal()).isEqualTo(BigDecimal.valueOf(4.00)) Expecting: <4.00> to…
夢のの夢
  • 5,054
  • 7
  • 33
  • 63
11
votes
2 answers

How to verify that static method throws an exception using AssertJ?

When I try to test this method static void validatePostcode(final String postcode, final String addressLine) { if(! hasValidPostcode(postcode, addressLine)) { throw new InvalidFieldException("Postcode is null or empty…
Cybex
  • 481
  • 1
  • 6
  • 29
11
votes
2 answers

assertj / Java comparing objects with list field ignoring order of elements in list

I am looking to compare 2 list of objects (say Foo) in test. List fooA; List fooB; Each Foo entry has one of the fields of type List (say Bar) class Foo { private List bars; .... } assertThat(fooA).isEqualTo(fooB); Comparison…
Random
  • 325
  • 1
  • 3
  • 15
11
votes
7 answers

Asserting properties on list elements with assertJ

I have a working hamcrest assertion: assertThat(mylist, contains( containsString("15"), containsString("217"))); The intended behavior is: mylist == asList("Abcd15", "217aB") => success myList == asList("Abcd15", "218") => failure How can I…
CoronA
  • 7,717
  • 2
  • 26
  • 53
11
votes
1 answer

Instrumented tests failure with AndroidJUnitRunner 1.0.0 and AssertJ

I'm trying to update my project to recently released Android Test Support library version 1.0.0. But if I add assertj-core dependency Gradle instrumented test tasks start to fail with "No tests found" message. I can successfully run individual tests…
Sokolof
  • 2,271
  • 2
  • 19
  • 30
11
votes
1 answer

Truth assertions library comparing to AssertJ

I used FEST-Assert and moved to AssertJ after it stopped development. Recently I was pointed to Google repository with another assertions library Truth (http://google.github.io/truth/). Reading the examples I can not find any advantage of start…
Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
11
votes
6 answers

how to use assertj extracting map property

I am using AssertJ. I have a class like MyObj. And I have a List of MyObj. class MyObj { ... Map myMap; ... } When I use: assertThat(list).extracting("myMap"), I cannot use .containsKey() method. I also tried using…
lhoak
  • 131
  • 1
  • 1
  • 4
10
votes
1 answer

Assert same condition on all elements of a collection

I'm working with AssertJ and I need to check that all objects in a list have intField > 0. Something like this: assertThat(myObjectList).extracting(p -> p.getIntField()).isGreaterThan(0); What's the correct way to achieve this? Should I use some…
davioooh
  • 23,742
  • 39
  • 159
  • 250
10
votes
2 answers

Is it possible to exclude some fields from assertJ usingFieldByFieldElementComparator?

How to achieve the below: List streams = new ArrayList<>(); assertThat(streams).usingFieldByFieldElementComparatorIgnoringGivenFields("createdOn").containsOnly(data1, data2);
Vel Ganesh
  • 537
  • 3
  • 12
  • 32
10
votes
2 answers

AssertJ assert on the cause message

Is there a way when using AssertJ agains a method throwing an excetion to check that the message in the cause is equal to some string. I'm currently doing something like: assertThatThrownBy(() -> SUT.method()) …
stikku
  • 536
  • 7
  • 27
9
votes
3 answers

AssertJ casting an extracted field to a Map

I have a Message object with MessageHeaders field. The MessageHeaders class implements a Map. I want to assert that I have specific headers set. I'm having trouble getting the MapAssert methods to come up. Here's what I want to…
George
  • 2,820
  • 4
  • 29
  • 56
9
votes
1 answer

AssertJ: FlatMap list of lists after calling extracting

So I have a Map of String/String list pairs, and what I want to do is after extraction, combine the returned lists into one list on which i can perform more assertions: MyTest.java Map> testMap = new HashMap<>(); List
jbailie1991
  • 1,305
  • 2
  • 21
  • 42
1
2
3
25 26