-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
61 changed files
with
1,395 additions
and
9 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
buildSrc/src/main/groovy/io.micronaut.build.internal.micronaut-test-suite.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
repositories { | ||
mavenCentral() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
When performing a test with a backing database, often some data is required in the database prior to running the tests. | ||
As of `micronaut-test` version 4.1.0, there is an annotation api:test.annotation.Sql[]. | ||
|
||
This annotation can be used to specify the location of one or more sql files to be executed at one of four phases in your test execution: | ||
|
||
* `BEFORE_CLASS` - executed once before the tests are run (the default). | ||
* `BEFORE_METHOD` - executed before each test method. | ||
* `AFTER_METHOD` - executed after each test method. | ||
* `AFTER_CLASS` - executed once after all the tests are run. | ||
The files are executed in the order specified in the annotation. | ||
|
||
For example given the two SQL scripts | ||
|
||
.test/resources/create.sql | ||
[source,sql] | ||
---- | ||
include::test-junit5/src/test/resources/create.sql[] | ||
---- | ||
|
||
and | ||
|
||
.test/resources/datasource_1_insert.sql | ||
[source,sql] | ||
---- | ||
include::test-junit5/src/test/resources/datasource_1_insert.sql[] | ||
---- | ||
|
||
We can annotate a test to run these two scripts prior to the test. | ||
|
||
[source, java, role="multi-language-sample"] | ||
---- | ||
include::{junit5tests}/SqlDatasourceTest.java[tags="clazz"] | ||
---- | ||
|
||
[source, groovy, role="multi-language-sample"] | ||
---- | ||
include::{spocktests}/SqlDatasourceSpec.groovy[tags="clazz"] | ||
---- | ||
|
||
[source, kotlin, role="multi-language-sample"] | ||
---- | ||
include::{kotest5tests}/SqlDatasourceTest.kt[tags="clazz"] | ||
---- | ||
|
||
<1> Specify the location of the SQL scripts to be executed for a DataSource with the name `default`. | ||
|
||
== Phases | ||
|
||
The default phase for the scripts to be executed is `BEFORE_CLASS`. | ||
To run the scripts at a different phase, we can specify the `phase` attribute of the annotation. | ||
|
||
[source, java] | ||
---- | ||
include::test-suite-at-sql-jpa/src/test/java/example/micronaut/TwoProductsThenNoneTest.java[tags="rollback"] | ||
---- | ||
<1> A script to run after each test in the specification. | ||
|
||
== Named Datasources | ||
|
||
If you have multiple datasources configured, you can specify the datasource name to use for the SQL scripts. | ||
|
||
[source, java, role="multi-language-sample"] | ||
---- | ||
include::{junit5tests}/SqlNamedDatasourceTest.java[tags="clazz"] | ||
---- | ||
|
||
[source, groovy, role="multi-language-sample"] | ||
---- | ||
include::{spocktests}/SqlNamedDatasourceSpec.groovy[tags="clazz"] | ||
---- | ||
|
||
[source, kotlin, role="multi-language-sample"] | ||
---- | ||
include::{kotest5tests}/SqlNamedDatasourceTest.kt[tags="clazz"] | ||
---- | ||
|
||
<1> Specify the location of the SQL scripts to be executed for a DataSource with the given name. | ||
|
||
== R2DBC | ||
|
||
For R2DBC, the `Sql` annotation can be used in the same way as for JDBC however it is required to pass the `resourceType` as `ConnectionFactory.class`. | ||
|
||
[source,java] | ||
---- | ||
include::test-suite-sql-r2dbc/src/test/java/io/micronaut/test/r2dbc/MySqlConnectionTest.java[tags="clazz"] | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
test-core/src/main/java/io/micronaut/test/annotation/Sql.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.test.annotation; | ||
|
||
import io.micronaut.context.annotation.AliasFor; | ||
import io.micronaut.core.annotation.Experimental; | ||
|
||
import javax.sql.DataSource; | ||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Repeatable; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Annotation that can be applied to a test scenario to execute SQL against a test database prior to the sceario being run. | ||
* | ||
* @since 4.1.0 | ||
* @author Tim Yates | ||
*/ | ||
@Target({ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Inherited | ||
@Experimental | ||
@Repeatable(Sql.Sqls.class) | ||
public @interface Sql { | ||
|
||
/** | ||
* @return The SQL scripts to execute | ||
*/ | ||
@AliasFor(member = "scripts") | ||
String[] value() default {}; | ||
|
||
/** | ||
* The name of the datasource to use for the SQL scripts. | ||
* | ||
* @return The datasource name | ||
*/ | ||
String dataSourceName() default "default"; | ||
|
||
/** | ||
* The phase of the test to execute the SQL scripts. | ||
* @return The phase | ||
*/ | ||
Phase phase() default Phase.BEFORE_ALL; | ||
|
||
/** | ||
* Scripts to execute, e.g. {@code "classpath:foo.sql"}. | ||
* @return The SQL scripts to execute | ||
*/ | ||
@AliasFor(member = "value") | ||
String[] scripts() default {}; | ||
|
||
/** | ||
* @return The type of the resource to use for the SQL scripts. | ||
*/ | ||
Class<?> resourceType() default DataSource.class; | ||
|
||
/** | ||
* Wrapper annotation class to allow multiple Sql annotations per test class or method. | ||
*/ | ||
@Target({ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Inherited | ||
@interface Sqls { | ||
|
||
/** | ||
* @return The SQL scripts to execute | ||
*/ | ||
Sql[] value(); | ||
} | ||
|
||
/** | ||
* The phase of the test to execute the SQL scripts. | ||
*/ | ||
enum Phase { | ||
/** | ||
* Execute the SQL before all tests. | ||
*/ | ||
BEFORE_ALL, | ||
|
||
/** | ||
* Execute the SQL before each test. | ||
*/ | ||
BEFORE_EACH, | ||
|
||
/** | ||
* Execute the SQL after all tests. | ||
*/ | ||
AFTER_ALL, | ||
|
||
/** | ||
* Execute the SQL after each test. | ||
*/ | ||
AFTER_EACH | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.