Questions tagged [assertj]

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

386 questions
2
votes
1 answer

assertj: compare fields between dto and entity classes

I need to compare a DTO class with its Entity class. For example, an AddressDTO class would be: @Setter @Getter @NoArgsConstructor @AllArgsConstructor public class AddressDTO { private StringTypeDTO text; private List line; …
Jordi
  • 20,868
  • 39
  • 149
  • 333
2
votes
1 answer

Java does not catch exception

I am trying to implement lambda expression for constructor. My constructor can throw an IllegalArgumentException. I tried different ways. First way is just calling lambda expression: catchThrowable(() -> new SomeClass(testVar1, null, testVar2)); It…
Farrukh Nabiyev
  • 346
  • 2
  • 11
2
votes
1 answer

How to recursively compare two objects with different field names but same values with AssertJ

I want to compare two objects (DTO and an Entity) using AssertJ's recursive comparison, for purpose of unit-testing DTO->Entity mapper. Both of these objects have fields with pretty much the same values, but some of the fields have different names…
2
votes
1 answer

analog of the method `isEqualToComparingFieldByFieldRecursively`

Can you please suggest an analog of the method isEqualToComparingFieldByFieldRecursively from the library AssertJ. Because this method is deprecated.
Onaxiz
  • 25
  • 2
2
votes
0 answers

XmlUnit - how to ignore namespace prefix and compare 2 xml files?

I have two xml files. First contains namespace prefix and the second one doesn't. So when I assert them I want to ignore these prefixes and check for equality. @Test public void test1(){ String control = "
fcka
  • 21
  • 2
2
votes
2 answers

Is the method "hasOnlyElementsOfType" deprecated in Assertj 3.16.1

I've this code that no longer works after updating to Assertj 3.16.1 Throwable thrown = catchThrowable(() ->…
Sammy65
  • 627
  • 2
  • 12
  • 28
2
votes
0 answers

Adding a failure to SoftAssertions from multiple threads

SoftAssertions in AssertJ is useful when I need to make sure multiple conditions hold or collect as many failures as possible: final SoftAssertions softly = new SoftAssertions(); softly.assertThat("foo").hasToString("foo"); …
Bass
  • 4,977
  • 2
  • 36
  • 82
2
votes
1 answer

usingRecursiveFieldByFieldElementComparator not working

I'm stuck trying to get the second test of the code below to work. Both have the same payload structure, the one that works use Map<> only, the second one uses classes. Why does the first test work but the second doesn't? It seems a bug with…
Fabricio
  • 7,705
  • 9
  • 52
  • 87
2
votes
2 answers

Compare maps ignoring given fields

I feel like I'm missing something obvious here, but the documentation is letting me down. I'm attempting to compare two maps while ignoring a set of fields in assertJ. I would like to see this assert pass: private static final String[]…
SomeGuy
  • 485
  • 3
  • 12
2
votes
0 answers

AssertJ Swing occasional test failure

Context I got an AssertJ-Swing (3.9.2) test giving me headaches, sometimes it fails with a ComponentLookupError declaring that the entire component hierarchy is empty. This is the exact scenario of this question, with the addition that I'm using VNC…
2
votes
3 answers

Simplifying code for testing directory content/files

I have the following test code which checks the content of a particular directory structure assertThat(install_should_not_fail).isDirectory().satisfies(isnf -> { assertThat(new File(isnf, "maven-metadata-local.xml")).isNotEmpty(); assertThat(new…
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
2
votes
1 answer

AssertJ: A way to check for an object's field value if it contains a particular pattern (regex)

Hi Java Gurus and AssertJ Gurus, I would like to ask if there is a way in AssertJ to verify if an object exists from a list of Objects (e.g. ArrayList listOfTestObjects). Where that particular object's field (getter method's return)…
Artanis Zeratul
  • 963
  • 2
  • 14
  • 40
2
votes
2 answers

JUnit AssertionError: Expecting code to raise a throwable

I am trying to write a test for a method that throws custom exception. It fails with Assertion error. What could be done to properly catch the exception and pass the test? Service method: @Service public class CustomServiceImpl implements…
Vishnu
  • 65
  • 1
  • 3
  • 10
2
votes
0 answers

Dynamically Switch Between Soft Assertions and Hard Assertions using AssertJ

I want to provide an API to an API consumer. This API can be used to perform certain tests easily. I'm using AssertJ custom assertions to build this API. While the API consumer calls or uses my custom assertions, I would like him to easily configure…
sidnc86
  • 376
  • 2
  • 13
2
votes
2 answers

Testing @Async annotated method in @SpringBootTest

I have a service SomeService with one method to do some logic. @Override public CompletableFuture process(User user) { Objects.requiredNonNull(user, "user must not be null"); // other logic... } Then I have a test for…
JiKra
  • 1,618
  • 3
  • 29
  • 45