You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to be able to use AssertJ soft assertions on ApplicationContext, the same way as it works on regular assertions like Assertions.assertThat(context).doesNotHaveBean(...).hasSingleBean(...). However, it seems to be not possible with current implementation of both ApplicationContextAssert and SoftAssertions (proxying there in particular).
My setup:
Spring Boot 3.2.2
Spring Boot Test 3.2.2
AssertJ 3.25.3
Java 21
How to get there
To give you examples of what I would like to achieve and my path:
privatefinalApplicationContextRunnerrunner = newApplicationContextRunner().withUserConfiguration(MyConfiguration.class);
@Testvoidthis_one_wraps_context_into_object_assert() {
runner.withPropertyValues("myapp.something.enabled=false")
.run(context ->
assertSoftly(softly -> {
// softly.assertThat(context).doesNotHaveBean(MyBean.class); this is not possible because of how AssertJ creates proxiessoftly.assertThat(context)... // here ObjectAssert is created instead of ApplicationContextAssertsoftly.assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(() -> context.getBean(MyBean.class)); // this is the only working solution now
})
);
}
I prefer syntax of doesNotHaveBean instead of checking if exception was thrown, because it's way easier to read, more fluent (I can chain multiple assertions) and checks for startup failures and provides resonable error message in case assertion fails (I can workaround message with as from assertj).
I took another attempt to create soft assertion proxy manually, so that I should be able to use fluent assertions with ApplicationContextAssert:
The problem here is Throwable passed as second argument to constructor, what makes AssertJ soft assertions proxying not work. I was looking at way to use ApplicationContextAssertProvider, but with no success. Workaround would be to create custom class with single constructor extending ApplicationContextAssert, but it's not possible due to package-private scope of the constructor.
Solution
Be able to use AssertJ soft assertions with ApplicationContextAssert:
wilkinsona
changed the title
ApplicationContextAssert does not work with Assertj SoftAssertions
Update ApplicationContextAssert to support AssertJ's soft assertions
Apr 5, 2024
Problem
I would like to be able to use AssertJ soft assertions on
ApplicationContext
, the same way as it works on regular assertions likeAssertions.assertThat(context).doesNotHaveBean(...).hasSingleBean(...)
. However, it seems to be not possible with current implementation of bothApplicationContextAssert
andSoftAssertions
(proxying there in particular).My setup:
How to get there
To give you examples of what I would like to achieve and my path:
I prefer syntax of
doesNotHaveBean
instead of checking if exception was thrown, because it's way easier to read, more fluent (I can chain multiple assertions) and checks for startup failures and provides resonable error message in case assertion fails (I can workaround message withas
from assertj).I took another attempt to create soft assertion proxy manually, so that I should be able to use fluent assertions with
ApplicationContextAssert
:but it fails with
because AssertJ proxying requires single argument constructor (actual value), while
ApplicationContextAssert
has following signature:The problem here is
Throwable
passed as second argument to constructor, what makes AssertJ soft assertions proxying not work. I was looking at way to useApplicationContextAssertProvider
, but with no success. Workaround would be to create custom class with single constructor extendingApplicationContextAssert
, but it's not possible due to package-private scope of the constructor.Solution
Be able to use AssertJ soft assertions with
ApplicationContextAssert
:Acceptable temporary workaround is to create soft assertion proxy manually:
The text was updated successfully, but these errors were encountered: