Skip to content

Extend ApplicationContextRunner to accept simple bean definitions #16011

@snicoll

Description

@snicoll

Inspired by ProjectTester in Spring Initializr, I'd like us to extend ApplicationContextRunner to allow registering beans in addition to Configurations.

Lots of tests are declaring a test @Configuration class with one or two bean definitions to export additional beans in the context to test. This requires quite a lot of code in the end and a muscle memory to remember what the inner class at the end of the test does.

Consider this:

@Test
public void cacheManagerBackOff() {
	this.contextRunner.withUserConfiguration(CustomCacheManagerConfiguration.class)
			.run((context) -> assertThat(
					getCacheManager(context, ConcurrentMapCacheManager.class)
							.getCacheNames()).containsOnly("custom1"));
}

To be replaced with this proposal by

@Test
public void cacheManagerBackOff() {
	this.contextRunner
			.withBean(CacheManager.class,
					() -> new ConcurrentMapCacheManager("custom1"))
			.run((context) -> assertThat(
					getCacheManager(context, ConcurrentMapCacheManager.class)
							.getCacheNames()).containsOnly("custom1"));
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions