Do you use JUnit5 for your tests?
Do you use DbSetup to prepare your database for integration tests?
Do you write a lot of boilerplate code because of this?
These are helper annotations that implement the JUnit5 extension API to help reduce boilerplate code when using DbSetup. These are very simple annotations that directly map to DbSetup operations.
This is the main annotation that tells JUnit5 to look for other DbSetup related annotations. Without this, the other annotations will do nothing.
Requirements:
- Annotation target: class only
This annotation tells DbSetup which data source to run operations on.
There can be multiple data sources, but they must all be uniquely named. Operations will also need to specify which data source they are to be launched on.
Requirements:
- Annotation target: field only#
- Target must be of
javax.sql.DataSource
type - Target can both be static or not static
DbSetup will launch the operations that are annotated with this. Because SQL scripts innately require to be ordered, eg satisfying referential integrity, the operations will be launched in order. However, Java is a language that does not preserve declaration order of fields thus we cannot use declared order as our implicit order (how nice would it be if we could do so).
There are 2 ways to declare the order of the operation.
- Explicitly by setting the
order
variable- Eg.
@DbSetupOperation(order = 0) Operation myOperation
- Eg.
- Implicitly by post-pending your variable with the order number
- Eg.
@DbSetupOperation Operation myOperation0
- Eg.
The @DbSetupOperation.order
variable takes precedence if both are available.
If there are multiple data sources, the sources()
field can be used to define which data source that this operation
will be launched on.
Requirements:
- Annotation target: field only#
- Target must be of
com.ninja_squad.dbsetup.operation.Operation
type - Target can both be static or not static
- There can multiple targets
- Targets must all be ordered either explicitly or implicitly
If this annotation is placed on a test method, DbSetup will not be launched for the next test. This is synonymous to
writing dbTracker.skipNextLaunch();
in your test.
- Annotation target: method only
- Target must be a
@Test
otherwise it does nothing
See binder configuration for details on
BinderConfiguration
.
This optional annotation defines the binder configuration to use when running DbSetup.
If there are multiple data sources, the sources()
field can be used to define which data source that this operation
will be launched on.
- Annotation target: field only#
- Target must be an implementation of
com.ninja_squad.dbsetup.bind.BinderConfiguration
- Target can both be static or not static
- There can only be at most a single target per data source
See here for a concrete example on how to use the annotations.
#: The reason why only fields are supported is to prevent unintentional code that does not work, like this:
@DbSetupSource
static DataSource dataSource() {
return new DataSource();
}
The code will compile but every operation will be run on a different data source and is very likely to be incorrect.