Skip to content

@DataJpaTest don't work on interface #45252

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
ars-java opened this issue Apr 22, 2025 · 3 comments
Closed

@DataJpaTest don't work on interface #45252

ars-java opened this issue Apr 22, 2025 · 3 comments
Labels
for: external-project For an external project and not something we can fix

Comments

@ars-java
Copy link

ars-java commented Apr 22, 2025

I use @DataJpaTest annotation on JUnit 5 test class. But I have few test classes with the same configuration. I added an interface to extract common methods and configuration. But there's a problem with @DataJpaTest annotation. If it's on the test class then everything is good. But if I put it on to the interface which test class extending then it's not working. It seems to be good to extend using annotation @DataJpaTest to interfaces.

@DataJpaTest // It works
class MyTestClass {
}
interface MyCommonTestInterface1 { ... } // it works too
@DataJpaTest 
class MyTestClass11: MyCommonTestInterface1 {}
@DataJpaTest 
class MyTestClass12: MyCommonTestInterface1 {}
@DataJpaTest 
interface MyCommonTestInterface2 { ... } // Don't work but I wanna remove duplication

class MyTestClass21: MyCommonTestInterface2 {}
class MyTestClass22: MyCommonTestInterface2 {}

The real case is if you're using Testcontainers and wanna start container ones for all tests (to save time), you need to create static container in the base class. I could be using abstract class for such case but I have not 1 container but N (cont_1, ..., cont_n), for example Postgres, Elastic, MyOther Service and I wanna start cont_1, cont_2 for 10 tests and start cont_5, cont_6 for another 20 tests and... Because I use interfaces like Scala traits to add them independently.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 22, 2025
@wilkinsona
Copy link
Member

This is happening due to Spring Framework's processing of imports. Spring Boot (through ImportsContextCustomizerFactory and ImportsContextCustomizer.ImportsSelector), registers the test class as a selected import from where its @Import annotations are processing by Framework. Framework does not consider imports on implemented interfaces so the @DataJpaTest-related imports are ignored.

I think the first step here is to explore the possibility of changing Framework's behavior. Things are a little inconsistent at the moment as, for example, the test context framework will find annotations on implemented interfaces.

@wilkinsona
Copy link
Member

The Framework team are happy to expand @Import processing to consider implemented interfaces. This will be a 7.0.x (at the earliest) feature so support for using a @…Test annotation on an implemented interface will land in Boot 4.0 at the earliest. I'm going to close this one as there's nothing to do in the Boot side other than a Framework upgrade once the new feature's available.

@wilkinsona wilkinsona closed this as not planned Won't fix, can't repro, duplicate, stale Apr 23, 2025
@wilkinsona wilkinsona added for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 23, 2025
@ars-java
Copy link
Author

@wilkinsona Excellent news! Thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix
Projects
None yet
Development

No branches or pull requests

3 participants