Skip to content

Support @TestPropertySource as a repeated annotation #23299

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

Closed
gebezs opened this issue Jul 16, 2019 · 3 comments
Closed

Support @TestPropertySource as a repeated annotation #23299

gebezs opened this issue Jul 16, 2019 · 3 comments
Labels
in: test Issues in the test module status: duplicate A duplicate of another issue type: enhancement A general enhancement

Comments

@gebezs
Copy link

gebezs commented Jul 16, 2019

TestPropertySource is inherited in case of class hierarchy however it is not inherited in meta-annotation hierarchy.
The lack of this is made clear by @sbrannen in his answer: https://stackoverflow.com/a/26183692
He says:

The algorithm that the Spring Framework uses when searching for an annotation stops once it has found the first occurrence of the sought annotation.

Expected behaviour

The settings from the @CustomTest and MyTest should merged as it is merged in case of class inheritance.

@TestPropertySource(properties = { "property-a=value-a", "property-b=value-b" })
public @interface CustomTest {
}
@CustomTest
@TestPropertySource(properties = { "property-a=value-x", "property-c=value-c" })
public class MyTest {
  @Value("${property-a}")
  private String propertyA;
  @Value("${property-b}")
  private String propertyB;
  @Value("${property-c}")
  private String propertyC;

  @Test
  public void test() {
    assertEquals("value-x", propertyA);
    assertEquals("value-b", propertyB);
    assertEquals("value-c", propertyC);
  }
}

Rationale

It could give much more flexibility in tests.

@sbrannen sbrannen added in: test Issues in the test module type: enhancement A general enhancement labels Jul 16, 2019
@sbrannen sbrannen changed the title TestPropertySource should inherit on annotation level Support @TestPropertySource as a repeated annotation Jul 16, 2019
@antkorwin
Copy link
Contributor

This is same as #21986 and already fixed here #23320

@sbrannen
Copy link
Member

This is same as #21986 and already fixed here #23320

Indeed. Thanks for pointing that out.

For the sake of completeness, I tested the following against master (i.e., the upcoming Spring Framework 5.2), and it passes.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@TestPropertySource(properties = { "property-a=value-a", "property-b=value-b" })
@interface CustomTest {
}
@RunWith(SpringRunner.class)
@ContextConfiguration
@TestPropertySource(properties = { "property-a=value-x", "property-c=value-c" })
@CustomTest
public class MyTest {

	@Value("${property-a}")
	private String propertyA;

	@Value("${property-b}")
	private String propertyB;

	@Value("${property-c}")
	private String propertyC;

	@Test
	public void test() {
		assertEquals("value-x", propertyA);
		assertEquals("value-b", propertyB);
		assertEquals("value-c", propertyC);
	}

	@Configuration
	static class Config {}
}

I am therefore closing this issue as a duplicate of gh-23320.

@sbrannen sbrannen added the status: duplicate A duplicate of another issue label Jul 28, 2019
@gebezs
Copy link
Author

gebezs commented Aug 4, 2019

Great news. @antkorwin, thanks for fixing this.

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 status: duplicate A duplicate of another issue type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants