I'd like to use the @sql annotation to preload a database i need in order to test my DAO with JUnit. My understanding of the documentation is that if I use the annotation at class level, the DB will be loaded before each test method included in the class. I would prefer doing it only once before the first test (similar behaviour as @BeforeAll in Junit5). How can I do this?
Asked
Active
Viewed 194 times
0
-
1Show what you have tried so far? – Coder Jul 07 '19 at 14:54
-
You can use @BeforeClass annotation. this annotation is used to perform common and costly operations like loading or connecting the database – Dhrumil Patel Jul 07 '19 at 15:37
-
The problem with `@BeforeClass` (JUnit4) or `@BeforeAll` (JUnit5) is that they require static methods, which makes it impossible to autowire my Datasource. The problem was already adressed here: https://stackoverflow.com/questions/29340286/how-to-autowire-field-in-static-beforeclass. One of the suggested solution was to use `@sql`. It works indeed. I'm only annoyed by the fact that the query inside `@sql` is runned before each test methods. – Julien Berthoud Jul 08 '19 at 09:51