Skip to content

[BAEL-8422] Exception handling through spring AOP and AspectJ #17512

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

Conversation

rjraunak846
Copy link

No description provided.


// Then
List<ILoggingEvent> logsList = listAppender.list;
assertThat(logsList).isNotEmpty();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not necessary, the following assertion already implies the list is not empty (same in the other test)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed that. Thanks.

@Test
public void givenInvalidPaymentDetails_whenValidationFails_thenAspectLogsException() {
// When
assertThatThrownBy(() -> paymentService.validatePaymentDetails("")).isInstanceOf(IllegalArgumentException.class)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the Baeldung formatter, we should be putting chained method calls line by line https://github.com/Baeldung/dev-settings/tree/main/javaCodeStyle/intellij

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have already configured my IntelliJ with that code style, but still on formatting the code, it doesn't split all the chained calls to new lines.

// Then
List<ILoggingEvent> logsList = listAppender.list;
assertThat(logsList).isNotEmpty();
assertThat(logsList).extracting(Object::toString)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are still not showing that the Exception is logged:

logger.error("Exception occurred: {}", ex.getMessage(), ex);

The last parameter ex is untested at the moment

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last parameter "ex" is not actually needed. I have removed it in the latest commit.

assertThat(logsList).extracting(Object::toString) .containsExactly("[ERROR] Exception occurred: Payment details cannot be null or empty.");

This assertion captures the logs and checks whether the exception that actually occurred matches the expected log.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry but I have to disagree here. It's essential when logging exceptions to preserve the stack trace - can we find a way to test this please?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that preserving the stacktrace is important. But I can't think of a way to create an assertion for that. One way can be to actually assert each line of the stacktrace printed but that will be too clumsy.

On a side note, isn't it implicit that if the code has reached the error logger and thrown an error message that we have already asserted, then the stack trace is already tested? Let me know if you think otherwise and has a solution to test.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I'll show you how to do this later when I get some time

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And no it's not implicit. The test would still be green if I replaced the ex parameter with a dummy new Exception()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup! Got it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do something like:

        assertThat(logsList)
            .extracting(event -> event.getThrowableProxy().getMessage())
            .containsExactly("Payment processing failed due to invalid details.");

or alternatively, wire a mock/spy Logger into the ExceptionHandlingAspect and use Mockito to verify

@lor6 lor6 closed this Jun 23, 2025
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

Successfully merging this pull request may close these issues.

None yet

3 participants