-
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.
fix: TestSqlAnnotationHandler fails if context stopped in test (#951)
* fix: TestSqlAnnotationHandler fails if context stopped in test The TestSqlAnnotationHandler tried to load a ResourceLoader bean even if there were no Sql annotations. This fails if the context has been stopped in the test. This PR does 2 things: 1. It only tries to load the bean if it has work to do 2. If it has work to do, and the context is stopped, it logs a warning and skips processing the scripts Co-authored-by: Phil Hardwick <[email protected]> * Add test with Sql annotations --------- Co-authored-by: Phil Hardwick <[email protected]>
- Loading branch information
1 parent
19f84e3
commit 889dd61
Showing
3 changed files
with
112 additions
and
17 deletions.
There are no files selected for viewing
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
27 changes: 27 additions & 0 deletions
27
test-junit5/src/test/java/io/micronaut/test/junit5/ApplicationStopTest.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,27 @@ | ||
package io.micronaut.test.junit5; | ||
|
||
import io.micronaut.context.ApplicationContext; | ||
import io.micronaut.runtime.EmbeddedApplication; | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest; | ||
import jakarta.inject.Inject; | ||
import org.junit.jupiter.api.MethodOrderer; | ||
import org.junit.jupiter.api.Order; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestMethodOrder; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
@MicronautTest | ||
class ApplicationStopTest { | ||
|
||
@Inject | ||
private ApplicationContext applicationContext; | ||
|
||
@Test | ||
void stoppingTheContextDoesntCauseFailures() { | ||
applicationContext.stop(); | ||
assertTrue(true); | ||
// should not error | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
test-junit5/src/test/java/io/micronaut/test/junit5/SqlDatasourceApplicationStopTest.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,59 @@ | ||
package io.micronaut.test.junit5; | ||
|
||
import io.micronaut.context.ApplicationContext; | ||
import io.micronaut.context.annotation.Property; | ||
import io.micronaut.test.annotation.Sql; | ||
import io.micronaut.test.annotation.TransactionMode; | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest; | ||
import jakarta.inject.Inject; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
@DbProperties | ||
@MicronautTest(transactionMode = TransactionMode.SINGLE_TRANSACTION) | ||
@Property(name = "datasources.default.dialect", value = "H2") | ||
@Property(name = "datasources.default.driverClassName", value = "org.h2.Driver") | ||
@Property(name = "datasources.default.schema-generate", value = "CREATE_DROP") | ||
@Property(name = "datasources.default.url", value = "jdbc:h2:mem:devDb;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE") | ||
@Property(name = "datasources.default.username", value = "sa") | ||
@Sql({"classpath:create.sql", "classpath:datasource_1_insert.sql"}) // <1> | ||
class SqlDatasourceApplicationStopTest { | ||
|
||
@Inject | ||
ApplicationContext applicationContext; | ||
|
||
@Inject | ||
DataSource dataSource; | ||
|
||
@Test | ||
void stoppingTheContextDoesntCauseFailures() throws Exception { | ||
assertEquals(List.of("Aardvark", "Albatross"), readAllNames(dataSource)); | ||
applicationContext.stop(); | ||
assertTrue(true); | ||
// should not error | ||
} | ||
|
||
List<String> readAllNames(DataSource dataSource) throws SQLException { | ||
var result = new ArrayList<String>(); | ||
try ( | ||
Connection ds = dataSource.getConnection(); | ||
PreparedStatement ps = ds.prepareStatement("select name from MyTable"); | ||
ResultSet rslt = ps.executeQuery() | ||
) { | ||
while(rslt.next()) { | ||
result.add(rslt.getString(1)); | ||
} | ||
} | ||
return result; | ||
} | ||
} |