Skip to content

Commit d0ead4c

Browse files
committed
Allow to customize an existing application (for tests, mocking, etc.)
Fixes gh-44
1 parent c508a35 commit d0ead4c

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

jafu/src/main/java/org/springframework/fu/jafu/JafuApplication.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public abstract class JafuApplication {
1919

2020
private final ApplicationContextInitializer<GenericApplicationContext> initializer;
2121

22+
private ApplicationContextInitializer<GenericApplicationContext> customizer;
23+
2224
protected JafuApplication(ApplicationContextInitializer<GenericApplicationContext> initializer) {
2325
this.initializer = initializer;
2426
}
@@ -79,10 +81,16 @@ protected void load(ApplicationContext context, Object[] sources) {
7981
app.setAdditionalProfiles(Arrays.stream(profiles.split(",")).map(it -> it.trim()).toArray(String[]::new));
8082
}
8183
app.addInitializers(this.initializer);
84+
if (this.customizer != null) app.addInitializers(this.customizer);
8285
System.setProperty("spring.backgroundpreinitializer.ignore", "true");
8386
return app.run(args);
8487
}
8588

89+
public JafuApplication customize(Consumer<ApplicationDsl> customizer) {
90+
this.customizer = new ApplicationDsl(customizer);
91+
return this;
92+
}
93+
8694
protected abstract void initializeWebApplicationContext(SpringApplication app);
8795

8896
}

jafu/src/test/java/org/springframework/fu/jafu/ApplicationDslTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,30 @@ void applicationProperties() {
5151
context.close();
5252
}
5353

54+
@Test
55+
void replaceBeanOfExistingApplication() {
56+
var app = application(a -> a.beans(b -> b.bean(Bar.class, () -> new Bar("original"))));
57+
var context = app.customize(a -> a.beans(b -> b.bean(Bar.class, () -> new Bar("customized"), c -> c.setPrimary(true)))).run();
58+
assertEquals("customized", context.getBean(Bar.class).value);
59+
context.close();
60+
}
61+
5462

5563
static class Foo {}
5664

65+
static class Bar {
66+
67+
private final String value;
68+
69+
public Bar(String value) {
70+
this.value = value;
71+
}
72+
73+
public String getValue() {
74+
return value;
75+
}
76+
}
77+
5778
static class City {
5879

5980
private String name;

kofu/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ dependencies {
4040
testRuntimeOnly("de.flapdoodle.embed:de.flapdoodle.embed.mongo")
4141
testImplementation(project(":coroutines:data-mongodb"))
4242
testImplementation(project(":coroutines:webflux"))
43+
testImplementation("io.mockk:mockk:1.9")
4344
}
4445

4546
tasks.withType<DokkaTask> {

kofu/src/main/kotlin/org/springframework/fu/kofu/KofuApplication.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import org.springframework.context.support.GenericApplicationContext
1414
*/
1515
abstract class KofuApplication(private val initializer: ApplicationContextInitializer<GenericApplicationContext>) {
1616

17+
private var customizer: (ApplicationDsl.() -> Unit)? = null
18+
1719
/**
1820
* Run the current application
1921
* @param profiles [ApplicationContext] profiles separated by commas.
@@ -31,10 +33,15 @@ abstract class KofuApplication(private val initializer: ApplicationContextInitia
3133
app.setAdditionalProfiles(*profiles.split(",").map { it.trim() }.toTypedArray())
3234
}
3335
app.addInitializers(initializer)
36+
if (customizer != null) app.addInitializers(ApplicationDsl(customizer!!))
3437
System.setProperty("spring.backgroundpreinitializer.ignore", "true")
3538
return app.run(*args)
3639
}
3740

41+
fun customize(customizer: ApplicationDsl.() -> Unit) {
42+
this.customizer = customizer
43+
}
44+
3845
protected abstract fun initializeWebApplicationContext(app: SpringApplication)
3946

4047
}

kofu/src/test/kotlin/org/springframework/fu/kofu/ApplicationDslTests.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.fu.kofu
1818

19+
import io.mockk.every
20+
import io.mockk.mockk
1921
import org.junit.jupiter.api.Assertions.assertEquals
2022
import org.junit.jupiter.api.Assertions.assertFalse
2123
import org.junit.jupiter.api.Test
@@ -44,7 +46,7 @@ class ApplicationDslTests {
4446
fun `Create an application with a custom bean`() {
4547
val app = application {
4648
beans {
47-
bean<Foo>()
49+
bean<Foo>(isPrimary = true)
4850
}
4951
}
5052
with(app.run()) {
@@ -82,8 +84,31 @@ class ApplicationDslTests {
8284
}
8385
}
8486

87+
@Test
88+
fun `Mock a bean of an existing application`() {
89+
val app = application {
90+
beans {
91+
bean { Bar("original") }
92+
}
93+
}
94+
app.customize {
95+
beans {
96+
bean(isPrimary = true) {
97+
mockk<Bar>().apply {
98+
every { value } returns "customized"
99+
}
100+
}
101+
}
102+
}
103+
with(app.run()) {
104+
assertEquals("customized", getBean<Bar>().value)
105+
close()
106+
}
107+
}
108+
85109

86110
class Foo
111+
class Bar(val value: String)
87112

88113
// Switch to data classes when https://github.com/spring-projects/spring-boot/issues/8762 will be fixed
89114
class City {

0 commit comments

Comments
 (0)