Skip to content

Support @TestPropertySource as a repeated annotation #23299

Closed

Description

@gebezs

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.

Activity

  1. added
    in: test
    type: enhancement
    on Jul 16, 2019
  2. changed the title [-]TestPropertySource should inherit on annotation level[/-] [+]Support @TestPropertySource as a repeated annotation[/+] on Jul 16, 2019
  3. antkorwin commented on Jul 28, 2019

    @antkorwin
    Contributor

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

  4. sbrannen commented on Jul 28, 2019

    @sbrannen
    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.

  5. added
    status: duplicate
    on Jul 28, 2019
  6. gebezs commented on Aug 4, 2019

    @gebezs
    Author

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: teststatus: duplicatetype: enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions