Jakub :
I'm using JUnit 5 with Java to write integration tests. Some of them connect to the database and I'm looking for a way to both:
- filter tests out
- run the database set-up (truncate tables, etc.) before tests
for tests annotated with a specific annotation.
So far I've found that using tags lets me do the following:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.Tag;
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Tag("database")
public @interface Database {}
and then use the @Database
annotation to filter annotated tests out.
Now I'm looking for a solution to hook up a test set-up for all tests annotated with this annotation. What could it be?
johanneslink :
The solution requires two steps:
- Write a Jupiter extension e.g.
MyDatabaseSetup
that does the necessary setup/teardown work - Add
@ExtendWith(MyDatabaseSetup.class)
to your Database annotation
Now every class or test method annotate with @Database
will also use the extension.