Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support external methods referenced by fully-qualified name in @TestBean #33125

Closed
sbrannen opened this issue Jun 30, 2024 · 1 comment
Closed
Assignees
Labels
in: test Issues in the test module type: enhancement A general enhancement
Milestone

Comments

@sbrannen
Copy link
Member

sbrannen commented Jun 30, 2024

Overview

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

However, users may wish to define common factory methods in external classes that can be shared easily across multiple test classes simply by referencing an external method via a fully-qualified method name.

Example

Given the following test utility...

package example;

public class TestBeanFactory {

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

}

... the following should be supported:

@SpringJUnitConfig
class TestBeanForExternalFactoryMethodIntegrationTests {

	@TestBean(methodName = "example.TestBeanFactory#createTestMessage")
	String message;


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


	@Configuration
	static class Config {

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

}

Proposal

To align with the syntax used for fully-qualified method names in @MethodSource and fully-qualified field names in @FieldSource in JUnit Jupiter, I propose that we allow test bean factory methods to be referenced via a fully-qualified method name following the syntax <fully-qualified class name>#<method name>.

Related Issues

@sbrannen sbrannen added in: test Issues in the test module type: enhancement A general enhancement labels Jun 30, 2024
@sbrannen sbrannen added this to the 6.2.0-M5 milestone Jun 30, 2024
@sbrannen sbrannen self-assigned this Jun 30, 2024
@sbrannen sbrannen changed the title Support external methods referened by fully-qualified method name in @TestBean Support external methods referenced by fully-qualified name in @TestBean Jun 30, 2024
@sbrannen

This comment was marked as outdated.

sbrannen added a commit to sbrannen/spring-framework that referenced this issue Jun 30, 2024
Prior to this commit, @⁠TestBean factory methods had to be defined in
the test class, one of its superclasses, or in an implemented
interface. However, users may wish to define common factory methods in
external classes that can be shared easily across multiple test classes
simply by referencing an external method via a fully-qualified method
name.

To address that, this commit introduces support for referencing a
@⁠TestBean factory method via its fully-qualified method name following
the syntax <fully-qualified class name>#<method name>.

Closes spring-projectsgh-33125
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

1 participant