Questions tagged [assertj]

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

386 questions
140
votes
4 answers

Can you add a custom message to AssertJ assertThat?

We have a test suite that primarily uses JUnit assertions with Hamcrest matchers. One of our team started experimenting with AssertJ and impressed people with its syntax, flexibility and declarative-ness. There is one feature that JUnit provides…
Patrick M
  • 10,547
  • 9
  • 68
  • 101
51
votes
3 answers

How to compare recursively ignoring given fields using assertJ?

AssertJ has isEqualToIgnoringGivenFields and isEqualToComparingFieldByFieldRecursively. But, there is no way I can compare two objects recursively by ignoring some fields. As per this discussion, it must be in development. How to still get my…
Tarun Maganti
  • 3,076
  • 2
  • 35
  • 64
36
votes
1 answer

AssertJ: For a Pojo how to check each nested Property/Field in one chained sentence

Having a POJO such as: public class ClientWebRequest { private URI uri; private HttpMethod httpMethod; private final Header header; private final Body body; public ClientWebRequest(){ header = new Header(); …
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
27
votes
3 answers

Testing content of list ignoring some of the fields

I have a scenario where I receive a list from a method call and I would like to assert that the list contains the correct elements. One way to do this would be to look for some detail in each element to see which expected element to compare with -…
homaxto
  • 5,428
  • 8
  • 37
  • 53
23
votes
8 answers

How to perform deeper matching of keys and values with assertj

Say I have a class like this: public class Character { public Character(String name){ this.name = name; } private String name; public String getName() { return name; } } And later, a Map Map characterAges = new…
pduncan
  • 1,330
  • 2
  • 15
  • 26
22
votes
3 answers

Is there a way to use AssertJ assertions with Spring MVC Test?

I have been using AssertJ for some time in my projects. Recently I started using Spring MVC Test for testing Spring MVC controllers. But I am not getting how to use AssertJ with it. All examples I see online all use Hamcrest with Spring MVC…
ajm
  • 12,863
  • 58
  • 163
  • 234
21
votes
4 answers

How to assert that two Lists are equal, ignoring order

I am using AssertJ and I am trying to assert that two List contain same strings, ignoring the order. List expected = Arrays.asList("Something-6144-77.pdf", "d-6144-77.pdf", "something-6144-78.pdf",…
Catfish
  • 18,876
  • 54
  • 209
  • 353
21
votes
2 answers

Test that either one thing holds or another in AssertJ

I am in the process of converting some tests from Hamcrest to AssertJ. In Hamcrest I use the following snippet: assertThat(list, either(contains(Tags.SWEETS, Tags.HIGH)) .or(contains(Tags.SOUPS, Tags.RED))); That is, the list may be either that…
Michael Piefel
  • 18,660
  • 9
  • 81
  • 112
16
votes
4 answers

assertThatThrownBy() check field on custom exception

How can I check particular field value in my custom excepiton using assertJ? Here is exception class: public class SomeException extends RuntimeException { private final Set something; public SomeException (String message,…
tillias
  • 1,035
  • 2
  • 12
  • 30
16
votes
3 answers

Assert value with assertJ in Optional

I have two classes: class Outer { Inner inner = new Inner("value"); } class Inner { private final String value; Inner(String value) { this.value = value; } } public Optional getOptionalValue() { return…
a3dsfcv
  • 1,146
  • 2
  • 20
  • 35
16
votes
5 answers

How to ensure a string has a substring exactly n times?

I want to check whether a String contains a certain substring n times. I know I can do: Assertions.assertThat(myString).contains("xyz"); Or even Assertions.assertThat(myString).containsOnlyOnce("xyz"); But how can I ensure this for n times? I tried…
user4063815
14
votes
3 answers

JUnit5: How to assert several properties of an object with a single assert call?

I want to assert several properties of an object with a single assert call. With JUnit 4 and Hamcrest I would have written something like this: assertThat(product, allOf( hasProperty("name", is("Coat")), hasProperty("available",…
Sasha Shpota
  • 9,436
  • 14
  • 75
  • 148
14
votes
4 answers

Assert List contains only one instance of a class using AssertJ

Could I somehow use AssertJ to assert a List has only one instance of a (sub)class? public class A {} public class B extends A {} public class C extends A {} @Test public void test() { List list = new ArrayList(); list.add(new B()); …
Maarten Dhondt
  • 577
  • 1
  • 9
  • 22
13
votes
2 answers

How to chain multiple assertThat statement in AssertJ

Here is an…
cdxf
  • 5,501
  • 11
  • 50
  • 65
13
votes
1 answer

AssertJ: what is the difference between containsOnly and containsExactlyInAnyOrder

AbstractIterableAssert#containsOnly says: Verifies that the actual group contains only the given values and nothing else, in any order. AbstractIterableAssert#containsExactlyInAnyOrder says: Verifies that the actual group contains exactly the…
radistao
  • 14,889
  • 11
  • 66
  • 92
1
2 3
25 26