Skip to content

Commit 433a225

Browse files
committed
GH-1110 - Register AssertablePublishedEvents in ApplicationContext if AssertJ is on the classpath.
We now check for the presence of AssertJ on the classpath and rather register an instance of ApplicationPublishedEvents in the ApplicationContext if present.
1 parent 8df778b commit 433a225

File tree

4 files changed

+72
-9
lines changed

4 files changed

+72
-9
lines changed

Diff for: spring-modulith-test/src/main/java/org/springframework/modulith/test/ModuleContextCustomizerFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void customizeContext(ConfigurableApplicationContext context, MergedConte
8484
beanFactory.registerSingleton(ModuleTestExecutionBeanDefinitionSelector.class.getName(),
8585
new ModuleTestExecutionBeanDefinitionSelector(testExecution));
8686

87-
var events = new DefaultPublishedEvents();
87+
var events = PublishedEventsFactory.createPublishedEvents();
8888
beanFactory.registerSingleton(events.getClass().getName(), events);
8989
context.addApplicationListener(events);
9090
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.modulith.test;
17+
18+
import org.springframework.context.ApplicationEvent;
19+
import org.springframework.context.ApplicationListener;
20+
import org.springframework.util.ClassUtils;
21+
22+
/**
23+
* Creates an instance of {@link PublishedEvents} or {@link AssertablePublishedEvents} depending on whether AssertJ is
24+
* on the classpath or not.
25+
*
26+
* @author Oliver Drotbohm
27+
* @since 1.4
28+
*/
29+
class PublishedEventsFactory {
30+
31+
private static final boolean ASSERT_J_PRESENT = ClassUtils.isPresent("org.assertj.core.api.Assert",
32+
PublishedEventsParameterResolver.class.getClassLoader());
33+
34+
/**
35+
* Returns whether AssertJ is present or not.
36+
*/
37+
static boolean isAssertJPresent() {
38+
return ASSERT_J_PRESENT;
39+
}
40+
41+
/**
42+
* Creates a instance of {@link PublishedEvents} that's also an {@link ApplicationListener} for
43+
* {@link ApplicationEvent}s.
44+
*
45+
* @return will never be {@literal null}.
46+
*/
47+
@SuppressWarnings("unchecked")
48+
static <T extends PublishedEvents & ApplicationListener<ApplicationEvent>> T createPublishedEvents() {
49+
return (T) (isAssertJPresent() ? new DefaultAssertablePublishedEvents() : new DefaultPublishedEvents());
50+
}
51+
}

Diff for: spring-modulith-test/src/main/java/org/springframework/modulith/test/PublishedEventsParameterResolver.java

+3-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.context.support.AbstractApplicationContext;
2828
import org.springframework.test.context.junit.jupiter.SpringExtension;
2929
import org.springframework.util.Assert;
30-
import org.springframework.util.ClassUtils;
3130

3231
/**
3332
* Provides instances of {@link PublishedEvents} as test method parameters.
@@ -36,9 +35,6 @@
3635
*/
3736
class PublishedEventsParameterResolver implements ParameterResolver, AfterEachCallback {
3837

39-
private static final boolean ASSERT_J_PRESENT = ClassUtils.isPresent("org.assertj.core.api.Assert",
40-
PublishedEventsParameterResolver.class.getClassLoader());
41-
4238
private ThreadBoundApplicationListenerAdapter listener;
4339
private final Function<ExtensionContext, ApplicationContext> lookup;
4440

@@ -59,7 +55,8 @@ public boolean supportsParameter(ParameterContext parameterContext, ExtensionCon
5955

6056
var type = parameterContext.getParameter().getType();
6157

62-
if (type.getName().equals("org.springframework.modulith.test.AssertablePublishedEvents") && !ASSERT_J_PRESENT) {
58+
if (type.getName().equals("org.springframework.modulith.test.AssertablePublishedEvents")
59+
&& !PublishedEventsFactory.isAssertJPresent()) {
6360
throw new IllegalStateException(
6461
"Method declares AssertablePublishedEvents as parameter but AssertJ is not on the classpath!");
6562
}
@@ -74,9 +71,7 @@ public boolean supportsParameter(ParameterContext parameterContext, ExtensionCon
7471
@Override
7572
public PublishedEvents resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
7673

77-
var publishedEvents = ASSERT_J_PRESENT
78-
? new DefaultAssertablePublishedEvents()
79-
: new DefaultPublishedEvents();
74+
var publishedEvents = PublishedEventsFactory.createPublishedEvents();
8075

8176
initializeListener(extensionContext);
8277
listener.registerDelegate(publishedEvents);

Diff for: spring-modulith-test/src/test/java/org/springframework/modulith/test/ModuleContextCustomizerUnitTests.java

+17
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
import example.module.SampleTestB;
2222

2323
import org.junit.jupiter.api.Test;
24+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2425
import org.springframework.modulith.core.ApplicationModule;
2526
import org.springframework.modulith.core.ApplicationModuleIdentifier;
2627
import org.springframework.modulith.test.ModuleContextCustomizerFactory.ModuleContextCustomizer;
28+
import org.springframework.test.context.MergedContextConfiguration;
29+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
2730

2831
/**
2932
* Unit tests for {@link ModuleTypeExcludeFilter}.
@@ -65,4 +68,18 @@ void instancesWithSameModuleSetupAreConsideredEqual() {
6568
assertThat(right).isEqualTo(left);
6669
assertThat(left).hasSameHashCodeAs(right);
6770
}
71+
72+
@Test // GH-1110
73+
void registersAssertablePublishedEvents() {
74+
75+
var context = new AnnotationConfigApplicationContext();
76+
var loader = new AnnotationConfigContextLoader();
77+
78+
new ModuleContextCustomizer(SampleTestA.class)
79+
.customizeContext(context, new MergedContextConfiguration(SampleTestA.class, null, null, null, loader));
80+
81+
assertThat(context.getApplicationListeners()).hasSize(1)
82+
.element(0)
83+
.isInstanceOf(AssertablePublishedEvents.class);
84+
}
6885
}

0 commit comments

Comments
 (0)