Skip to content

Support @TestBean factory methods defined in interfaces #32943

Closed
@sbrannen

Description

@sbrannen

Overview

Currently, @TestBean factory methods must be defined in the test class or one of its superclasses.

However, users may wish to define common factory methods in interfaces that can be shared easily across multiple test classes simply by implementing the necessary interface(s).

Example

Given the following interface...

interface TestBeanFactory {

	public static String createTestMessage() {
		return "test";
	}

}

... the following should be supported:

@SpringJUnitConfig
public class TestBeanInterfaceIntegrationTests implements TestBeanFactory {

	@TestBean(methodName = "createTestMessage")
	String message;


	@Test
	void test() {
		assertThat(message).isEqualTo("test");
	}


	@Configuration
	static class Config {

		@Bean
		String message() {
			return "prod";
		}
	}

}

However, that test class currently fails with an IllegalStateException because the createTestMessage() method is not found in the implemented TestBeanFactory interface.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions