Questions tagged [assertj]

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

386 questions
4
votes
1 answer

Assertj extract and contains doesn't match

I made a simple test : Person p = new Person(); p.setFirstName("Stéphane"); p.setLastName("Traumat"); assertThat(p) .extracting("firstName", "lastName") .contains(tuple("Stéphane", "Traumat")); And I get a strange result :…
4
votes
1 answer

Generics and wildcards with collections in Java

In a test class using AssertJ, I have code similar to the following: public void someTest() { assertThat(getNames()).has(sameNamesAs(getExpectedNames())); assertThat(getNames()).doesNotHave(sameNamesAs(getOtherNames())); } private…
4
votes
2 answers

How to unit-test single Swing Component with AssertJ?

Are there any examples on how to test single Component or JComponent with AssertJ? Getting started guide shows strange example to test entire application with main class, which is no granulated enough. I am expecting to test custom components…
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
3
votes
2 answers

AssertJ: How to return the actual exception when using method assertThatThrownBy

I am migrating from JUnit 5 and Hamcrest Assertions to AssertJ and I can't figure out the right method for extracting the actual exception from the executable. Here is a JUnit/Hamcrest example: var result = assertThrows(MyException.class, () ->…
Arthur Eirich
  • 3,368
  • 9
  • 31
  • 63
3
votes
2 answers

Simple JUnit test fails

I want to learn how to write JUnit tests and I fail completely. This is my test: @Test public void testGetAllCustomers() { // given List customerList = new ArrayList(); customerList.add(c1); …
FrozenTree
  • 33
  • 3
3
votes
3 answers

Assertion of Particular Exception which contains a field

Currently I have a test which tries to check a particular exception which looks like this: assertThatExceptionOfType(DataIntegrityViolationException.class).isThrownBy( () -> xrepo.save(abc)) …
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
3
votes
1 answer

test if a collection has exactly one element matching a predicate

With Assertj, I can use anyMatch to test if a collection has at least one element matching a predicate, e.g. var list = List.of("abc", "xyz") assertThat(list).anyMatch(element -> element.endsWith("xyz")); But how do I test if a collection has…
Antonio Dragos
  • 1,973
  • 2
  • 29
  • 52
3
votes
3 answers

Compare 2 objects ignoring null values

The project uses TestNg, Java11, Spring test I am writing testNG tests for API I have a java object that has this kind of stucture: class Object1 private Object2 o2; private List o3; Where Object2 is not only composed of primitive…
marie
  • 457
  • 8
  • 27
3
votes
3 answers

Matching custom exceptions

The Javadoc gives this example for the matches method: assertThat(player).matches(p -> p.isRookie()); And indeed, when I define a dummy class Player, the above statement compiles ok. However, when I define a class that derives from Exception, then…
DodgyCodeException
  • 5,963
  • 3
  • 21
  • 42
3
votes
2 answers

AssertJ: generating fluent assertions for Set

I've stumbled on an issue where AssertJ generates the following code in one of the assertion classes: public S hasItems(interface ItemInterface... items) This of course doesn't compile. An example code that causes the problem is as follows: …
3
votes
1 answer

Why do we get occasional failures in Assertj Swing tests?

Our gui unit-tests with Assertj Swing 3.9.2 occasionally fail in a difficult to reproduce way. Sometimes the entire test suite is green, sometimes some of the test cases fail. We are using two different machines with Ubuntu 18.04 LTS and GNOME and…
IvanProsperi94
  • 109
  • 2
  • 7
3
votes
1 answer

AssertJ: Log all passing (and failing) assertions

Is there a way to log all assertions, not just failing ones? I wonder whether AssertJ provides an interface to access the details of the assertion (WritableAssertionInfo) and the result of the assertion? Is there any way to hook into the assertion…
martin
  • 2,150
  • 1
  • 34
  • 48
3
votes
2 answers

AssertJ on list of Optionals

I have a List of Optionals, like List> optionals and I like to use assertj on it to assert several things. But I fail to do this properly - I only find examples on a single Optional. Of course I can do all checks by myself…
Emerson Cod
  • 1,990
  • 3
  • 21
  • 39
3
votes
2 answers

Cannot resolve symbol 'Assertions' <-- Error message when trying to use AssertJ in IntelliJ

Similar to some other Questions, I find IntelliJ mysteriously refuses to recognize AssertJ library. I am asking again as (a) I have tried the various suggestions, and (b) I have a very simple example anyone can try themselves. In IntelliJ 2018 and…
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
3
votes
0 answers

Maven surefire plugin outputs all errors at the end - causing OOME on large tests

Background: We have a regression test suite that tests the generation of some large xml files by comparing them field-by-field to the corresponding baseline files. This is implemented using a junit4 parameterized test running the test for each file…
Timi
  • 774
  • 9
  • 16