Skip to content

Introduce opt-in support for merging class-level and method-level @Sql declarations [SPR-16021] #20570

Closed
@spring-projects-issues

Description

@spring-projects-issues

Chris MacPherson opened SPR-16021 and commented

I would like to propose a small improvement which would enable another use-case of the @Sql annotation within test cases.

As mentioned in the docs here "method-level declarations override class-level".

For the following usecase I would like this not to occur so that I can set my tests up as follows:

@Sql(scripts = "/fixtures/cleanup.sql", 
    executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public class TestCase {

  @Test
  @Sql(scripts = "/fixtures/myclass/test1.sql")       
  public void aTestForOneUseCase() {
  }

  @Test
  @Sql(scripts = "/fixtures/myclass/test2.sql")       
  public void aTestForAnotherUseCase() {
  }

}

I can achieve in Java this by just adding the class level @Sql, from above, as a second @Sql instance in each test, but this becomes quite ugly and my test class starts to look like a list of annotations.

In Groovy, using Spock this doesn't work though, I get the following error:

Error:(43, 5) Groovyc: Cannot specify duplicate annotation on the same member : org.springframework.test.context.jdbc.Sql

Then I have to use an @SqlGroup for the two scripts which increases the annotations even more.

@SqlGroup([
       @Sql(scripts = "/fixtures/myclass/test1.sql"),
       @Sql(scripts = "/fixtures/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)])

So this is essentially a way to have a clean way to run an @Sql after every test. I've been trying to figure out another good way of doing it without relying on @Transactional.

I noticed this other ticket #18929 which is kind of asking the same thing, but it's not clear to me in the description. What it's asking for does seem to exist anyway, the Sql.ExecutionPhase part. But when I use them, the method level still seems to override the class level even though there's a different executionPhase.

So maybe this just needs the execution phase to be overridden at the method level? Hopefully that won't mess with other use-cases.


Affects: 4.3.11

Reference URL: https://stackoverflow.com/questions/32871817/using-annotation-sql-is-it-possible-to-execute-scripts-in-class-level-before-m

Issue Links:

Referenced from: pull request #1835

1 votes, 3 watchers

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulestatus: supersededAn issue that has been superseded by anothertype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions