Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom exception assertions #175

Open
aepedraza opened this issue Dec 26, 2024 · 1 comment
Open

Add custom exception assertions #175

aepedraza opened this issue Dec 26, 2024 · 1 comment

Comments

@aepedraza
Copy link

Currently, the documentation shows how to extend an assertion in 2.6.2 Custom Assertions but there is no documentation for custom exception asserts, which seems to use a slightly different mechanism with some other class hierarchy.

It would be nice to include this in the guide and in the examples repository

The goal for this section in the guide will be to present, given a custom exception, a way to assert on the specific fields in that class. For instance, given a custom exception:

package com.example.exception;

public class DetailException extends RuntimeException {

    private String detail;

    public DetailException(String message, String detail) {
        super(message);
        this.detail = detail;
    }

    public String getDetail() {
        return detail;
    }
}

and a business logic throwing this exception

package com.example.thrower;

import com.example.exception.DetailException;

public class DetailExceptionThrower {

    public void throwException() {
        throw new DetailException("message", "a detailed exception explanation");
    }
}

write an extension to write a test like:

package com.example.thrower;

import org.junit.jupiter.api.Test;

import static org.assertj.extension.DetailExceptionAssertions.assertThatDetailException;

class DetailExceptionThrowerCustomAssertionTest {

    private final DetailExceptionThrower underTest = new DetailExceptionThrower();

    @Test
    void test_customAssertion() {
        assertThatDetailException()
                .isThrownBy(underTest::throwException)
                .withMessage("message")
                .withDetail("a detailed exception explanation");
    }
}

is it even possible to write an extension like this?

@aepedraza
Copy link
Author

Added a small POC in this repository, in my personal account

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant