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…
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…
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();
…
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 -…
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…
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…
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",…
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…
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,…
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…
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…
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",…
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());
…
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…