Questions tagged [assertj]
AssertJ provides a set of strongly-typed assertions to use for unit testing (either with JUnit or TestNG).
345
questions
0
votes
0
answers
14
views
AssertJ's SoftAssertions.assertSoftly is hanging my test at 892 assertions
Im doing a database comparison test using serenity-bdd. I am currently testing 892 records. All 892 failed as expected.
AssertJ's SoftAssertions.assertSoftly appears to have ran through all 892 ...
0
votes
0
answers
18
views
assertj recursive comparison ignoring regex failing after upgrade from 3.18.1 to 3.21.0
I have updated my springboot , along with that my assertj also seems to have upgraded.
Now the test which was initially working in the older version is failing.
assertThat(updated)....
0
votes
0
answers
24
views
AssertJ.assertThat: Filtering out Map entries in POJO by Key
I'm trying to do an AssertJ assertion filtering out the specific Map entries by their key. The only available method for ignoring - "ignoringFields" doesn't work since the map key is not a ...
0
votes
0
answers
17
views
How to get screenshot for each soft assert failure with AssertJ SoftAssertions
I want to achieve below: -
CustomSoftAssertJ softly = new CustomSoftAssertJ();
softly.assertThat(actual).isEqualTo(expected); // get screen shot on failure
softly.assertThat(actual).isEqualTo(expected)...
0
votes
0
answers
28
views
Await until assertJ asserts true on database update
I am running a test that sends some data to a database and checks it's all been inserted properly. Depending on the machine, this can take varying amounts of time. I am using awaitly/assertj. Is there ...
0
votes
0
answers
29
views
Java JsonUnit how to get actual value from JsonAssert?
Is there any chance to get actual value from JsonUnit JsonAssert?
For example I need something like that
var JsonNode = assertThatJson(json).node("a[0].b[0].key")
.isPresent().getActual();
...
0
votes
1
answer
34
views
How to use logger to print messages with assert
I am trying to implement a logger in my repo and I am having some issues with implementing logger with Junit.
Sample assertion:
logger.info("Asserting the response.");
assertThat(response....
0
votes
0
answers
42
views
How to start writing Unit Tests for SpringBoot(The proper way)?
I'm new to the world of unit testing in Spring and with some research I think I'll be using Junit5 with mockito and AssertJ library for the same. I am just starting out and it does look really ...
2
votes
2
answers
57
views
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<Customer> customerList = new ...
0
votes
1
answer
58
views
How can we compare two hashmaps with case insensitive on both keys and values
I have two maps with both same values, but differing in case, either in key or in value. While asserting, I need to make it as pass. I know the Treemap with case insensitive option can be used, but it ...
1
vote
1
answer
75
views
Creating assertions
So I want to create an assertion class like how AssertJ works. I'm having trouble getting started.
public class Assertion {
static object assertThis(Object o){}
static ...
0
votes
1
answer
59
views
AssertJ Core, is it possible to get error message with actual field/property difference when checking iterables/arrays content?
In Assertj Core you can compare ojects field by field recursively.
In this test address.countryCode differ in the two objects:
@Test
public void shouldBeEqual() {
Person person1 = createPerson();
...
0
votes
2
answers
69
views
How can I test whether a list is sorted with AssertJ [duplicate]
Given
List<Integer> list = ...
I would like to test with AssertJ whether it is sorted. Something like:
assertThat(list).isSorted()
Is it possible?
0
votes
1
answer
44
views
AsserJ - chaing together returns and extracting
Sometimes sonar cloud is complaining about number of assertions in tests. I wanted and found out how I can chain assertions one to another, but theres a case where I cannot figure out how I can chain ...
0
votes
1
answer
66
views
Search JPanel by name
I was following the How to use CardLayout Java Swing tutorial and I got to the point where the panel is added to the layout:
JPanel cards;
final static String BUTTONPANEL = "Card with JButtons&...
1
vote
1
answer
43
views
AssertJ Swing enterText() writing wrong symbols
I'm using Swing to create a GUI for my application that has a JSpinner named "spinner". Inside the tests, created using AssertJ Swing I have something like this:
import org.assertj.swing....
0
votes
0
answers
22
views
Can I start the Application.exe using AssertJ Swing?
I tried to configure AssertJ Swing framework to verify a desktop standalone application written in Java Swing. I am not able to start the Application.exe from this framework...any ideas
1
vote
0
answers
46
views
Configure keybord layout before and after for testing with assertj-swing
I am using assertj-swing to test a simple GUI application. When I enter text in a JTextField, I am getting: IllegalArgumentException: Invalid key code '65406'
It seems it happens because I am using a ...
0
votes
2
answers
58
views
How to assert on a map value object member?
Given are a map like this:
Map<String, BestClass> myMap = new HashMap<>();
and a class BestClass like this:
class BestClass{
int a;
int b;
}
Now, this map is being populated ...
0
votes
1
answer
86
views
kotest equivalent to usingElementComparatorIgnoringFields from assertJ
assertJ allows you to check for element-wise list equality while ignoring user specified fields like so
assertThat(listOfObjects)
.usingElementComparatorIgnoringFields("field1", "...
0
votes
1
answer
201
views
AssertJ: issue with Assertions.assertThat().usingRecursiveComparison().ignoringFields()
I'm using assertj-core:3.21.0 and JDK 17
Here is an simple example that fails. This works in JDK 16 and assertj-core:3.19.0.
@AllArgsConstructor
@Data
class Test {
@...
0
votes
3
answers
181
views
JUnit: Is there a way to assert all array values at once?
I have a map in which its values are a simple pojo.
For example (in a very loosely code):
Class Data{
int a;
Boolean b;
}
Map<Integer, Data> myMap = new HashMap<>();
Now the map is ...
0
votes
1
answer
80
views
Is it possible to test if exception was thrown even if it was caught?
Is it possible to @Test if an appropriate exception was thrown in the main code function eaven if it was catch in a try / catch block?
ex:
public int maxSum(int a, int b) {
try {
if (a + b ...
2
votes
2
answers
141
views
AssertJ-Swing and Junit 5 support
I'm currently working at a Java application that I'm testing using JUnit Jupiter. I am starting to work at the GUI and I was wondering if it was possible to use AssertJ-Swing with JUnit 5.
As a matter ...
0
votes
1
answer
162
views
Assertj not working with Jackson JsonNode
I'm using assertj and Jackson's JsonNode combined. So far I've been using Assertions.assertThat(objectNode0).isEqualTo(objectNode1); and everything works fine.
Now, I need to ignore some fields in the ...
0
votes
1
answer
42
views
Is there a simple way to filter & narrow collections on instance type in assertj?
Can this be written as a single line?
assertThat(actualDeltas)
.filteredOn(delta -> delta instanceof Replacement)
.asInstanceOf(InstanceOfAssertFactories.list(Replacement.class))
I ...
1
vote
1
answer
71
views
AssertJ: How to build custom and nested assertions
I need to compare one PatientDTO dto object with other one PatientModel model object.
Both classes are quite similar:
class PatientDTO {
private String name;
private List<AddressDTO> ...
2
votes
1
answer
180
views
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 ...
1
vote
2
answers
273
views
AssertJ dynamically check if Optinal is empty or present
I'm working with OptionalAssert class of AssertJ and I need to implement a JUnit ParameterizedTest that will check for presence or emptiness of an Optional instance in a dynamic way:
@...
0
votes
2
answers
58
views
assertj eclipse assertions generator plugin
In the past I used an assertj assertions generator plugin that I downloaded from https://joel-costigliola.github.com/assertj-eclipse-plugin/repository/. Now I get a 404 error on that library.
I also ...
0
votes
1
answer
26
views
Possible to generate truth.dev Subjects for my domain model automaticly?
I'm a bit fan of Google's Truth.dev library. I have a large domain model in Java, and want to add several little custom assertions for them in my own Subject files. It's a bit of pain though to create ...
3
votes
1
answer
354
views
Using QuarkusTestExtension in combination with another JUnit extension leads to exception
Is it possible to use the QuarkusTestExtension from Quarkus in combination with the SoftAssertionsExtension from AssertJ?
I would like to run a Quarkus test which implicitly calls softAssertions....
0
votes
2
answers
145
views
Check a list inside a Map entry in a unit test using AssertJ
I want to check a list that is inside a Map value in a unit test using the AssertJ library:
public class Group {
List<Player> players = new ArrayList<>();
public Group(List<...
2
votes
1
answer
291
views
How to attach/embed captured screenshots during custom softAssertion into Cucumber Report?
In soft assertion screenshot get captured when softAssertions.assertAll() is called. So to capture screenshots for each soft Assertion failure, created simple CustomAssertion which extends to ...
0
votes
1
answer
68
views
Use assertThat function of AssertJ in ifPresent() of Java Optional
How can I remove try-catch block for the assertThat in the ifPresent()? The compiler gives me only one option for using assertThat in ifPresent(), which is to surround it with try-catch block. Throw ...
1
vote
1
answer
225
views
Use AssertJ in Spring Boot production code
Is it possible to use AssertJ in Spring Boot production code ?
I managed to use it in unit test, but 'import static org.assertj.core.api.Assertions.*;' could not be resolved in java file of src/main/...
0
votes
0
answers
23
views
Formate AssertK exception message
Thanks in advance
I am asserting two hashmap for difference on keys and value
On assertion I got exception with lots of information , I just need "to contains entries" and "the ...
2
votes
1
answer
147
views
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(() ...
0
votes
0
answers
35
views
How can I make an AssertJ Swing test fail when an EDT violation is detected in onSetUp?
I would like to use an AssertJ Swing test to check for EDT violations as I try to fix the threading problems in an existing application. It would be useful to have a simple test that would fail if any ...
0
votes
1
answer
82
views
Setup for AssertJ Swing - testing GUI
I am trying to test the GUI of a banking system application, but in the TestLogin class I have an error 'Cannot resolve constructor 'FrameFixture(GUI.Login)'. I tried to extend the SampleFrame class ...
0
votes
2
answers
308
views
How can I compare two collections using == with AssertJ?
I just implemented my own Insertion sort and trying to verify functionalities including stability.
For a given list of unsorted elements, I'm trying to verify my code against Collections#sort(List) ...
0
votes
1
answer
506
views
How to check that several fields of an object are not empty with AssertJ?
I have a class with some fields:
class User {
String name;
String id;
// other fields
}
I'm getting a user from the service and want to assert that name and id fields are not empty. The object ...
0
votes
0
answers
31
views
Reusing AssertJ iterable diff validator in a custom assertion class
I implemented custom assertion extending AbstractAssert.
My object contains several collections that should exactly match and I want to present message that field X is not the same in isEqualTo and ...
0
votes
1
answer
608
views
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 ...
1
vote
3
answers
99
views
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))...
1
vote
2
answers
130
views
AssertJ error reporting fails due to a circular reference with toString()
Legacy code to be tested has circular references so something like many-to-one bidirectional relationship entities.
Unfortunately there are also Lombok annotations @ToString & @Data in related ...
0
votes
2
answers
130
views
Compare two lists of different objects by certain two fields
I have two lists of different objects
class objectA {
String aId;
String aTitle;
String aUrl;
...
}
class objectB {
String bId;
String bTitle;
String bUrl;
...
}
List<ObjectA> aObjectList;
List&...
1
vote
1
answer
483
views
assertj Assertions assertThat is not resolved
Weird behavior. I've got a vanilla spring boot project I made with initializr. The build.gradle is below and as you can see it loaded up the test dependency for spring boot starter test, which ...
0
votes
1
answer
81
views
AssertJ how to compare two list by their element value?
Suppose I have a list of User userList.
class User {
String firstName;
String lastName;
}
Is there a way I can compare using AssertJ like
assertThat(userList).lastName.equalsTo(List.of("...
0
votes
1
answer
265
views
How to check if at least one string is contained in another with AssertJ?
I would like to check if at least one string is contained in another using AssertJ.
For example, having:
String actual = "I am a string";
String oneString = "am";
String ...