Skip to content

Commit 963a2bf

Browse files
committed
Rework Hibernate tests around "no datasource" cases
The existing "NoConfigNoEntitiesTest" do not test that, they are strictly equivalent to ConfigNoEntitiesDefaultPUDatasourceUrlMissingDynamicInjectionTest, so they can be safely removed. Testing there is no datasource requires that the Datasource extensions not produce an implicit datasource, which would require that we don't add a driver/client to the classpath. Since that's not practical, we'll explicitly disable the implicit datasource instead.
1 parent 62421d0 commit 963a2bf

File tree

9 files changed

+149
-160
lines changed

9 files changed

+149
-160
lines changed

extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/NoConfigNoEntitiesTest.java

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.quarkus.hibernate.orm.config;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.assertj.core.api.Assertions;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.extension.RegisterExtension;
8+
9+
import io.quarkus.test.QuarkusUnitTest;
10+
11+
public class NoDatasourceTest {
12+
13+
@RegisterExtension
14+
static QuarkusUnitTest runner = new QuarkusUnitTest()
15+
.withApplicationRoot((jar) -> jar
16+
.addClass(MyEntity.class))
17+
// Ideally we would not add quarkus-jdbc-h2 to the classpath and there _really_ wouldn't be a datasource,
18+
// but that's inconvenient given our testing setup,
19+
// so we'll just disable the implicit datasource.
20+
.overrideConfigKey("quarkus.datasource.jdbc", "false")
21+
.assertException(t -> assertThat(t)
22+
.hasMessageContainingAll(
23+
"Unable to find datasource '<default>' for persistence unit '<default>'",
24+
"Datasource '<default>' is not configured.",
25+
"To solve this, configure datasource '<default>'",
26+
"Refer to https://quarkus.io/guides/datasource for guidance."));
27+
28+
@Test
29+
public void test() {
30+
Assertions.fail("Startup should have failed");
31+
}
32+
33+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.quarkus.hibernate.orm.config;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import jakarta.enterprise.context.control.ActivateRequestContext;
6+
import jakarta.inject.Inject;
7+
8+
import org.hibernate.Session;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.RegisterExtension;
11+
12+
import io.quarkus.arc.InjectableInstance;
13+
import io.quarkus.test.QuarkusUnitTest;
14+
15+
public class NoEntitiesNoDatasourceTest {
16+
17+
@RegisterExtension
18+
static QuarkusUnitTest runner = new QuarkusUnitTest()
19+
.withEmptyApplication()
20+
// Ideally we would not add quarkus-jdbc-h2 to the classpath and there _really_ wouldn't be a datasource,
21+
// but that's inconvenient given our testing setup,
22+
// so we'll just disable the implicit datasource.
23+
.overrideConfigKey("quarkus.datasource.jdbc", "false");
24+
25+
@Inject
26+
InjectableInstance<Session> session;
27+
28+
// When having no entities, no configuration, and no datasource,
29+
// as long as the Hibernate ORM beans are not injected anywhere,
30+
// we should still be able to start the application.
31+
@Test
32+
@ActivateRequestContext
33+
public void test() {
34+
// But Hibernate ORM should be disabled, so its beans should not be there.
35+
assertThat(session.isUnsatisfied()).isTrue();
36+
}
37+
38+
}

extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/NoEntitiesTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
/**
1717
* Test that a persistence unit without any entities does get started,
18-
* and can be used, be it only for native queries.
18+
* and can be used, be it only for native queries,
19+
* as long as a datasource is present.
1920
*/
2021
public class NoEntitiesTest {
2122

extensions/hibernate-reactive/deployment/src/test/java/io/quarkus/hibernate/reactive/NoConfigNoEntitiesTest.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

extensions/hibernate-reactive/deployment/src/test/java/io/quarkus/hibernate/reactive/config/NoConfigNoEntitiesTest.java

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.quarkus.hibernate.reactive.config;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import io.quarkus.arc.Arc;
6+
import io.quarkus.arc.InjectableInstance;
7+
import jakarta.inject.Inject;
8+
import org.hibernate.reactive.mutiny.Mutiny;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.RegisterExtension;
11+
12+
import io.quarkus.test.QuarkusUnitTest;
13+
14+
public class NoDatasourceTest {
15+
16+
@RegisterExtension
17+
static QuarkusUnitTest runner = new QuarkusUnitTest()
18+
.withApplicationRoot((jar) -> jar
19+
.addClass(MyEntity.class))
20+
// Ideally we would not add quarkus-reactive-pg-client to the classpath and there _really_ wouldn't be a datasource,
21+
// but that's inconvenient given our testing setup,
22+
// so we'll just disable the implicit datasource.
23+
.overrideConfigKey("quarkus.datasource.reactive", "false");
24+
25+
@Inject
26+
InjectableInstance<Mutiny.SessionFactory> sessionFactory;
27+
28+
@Test
29+
public void test() {
30+
// Unlike Hibernate ORM, Hibernate Reactive will silently disable itself if the default datasource is missing, even if there are entities.
31+
// We may want to revisit that someday, but it's not easy to do without deeper interaction between the Hibernate ORM and Reactive extensions.
32+
assertThat(sessionFactory.isUnsatisfied()).isTrue();
33+
}
34+
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.quarkus.hibernate.reactive.config;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import jakarta.enterprise.context.control.ActivateRequestContext;
6+
import jakarta.inject.Inject;
7+
8+
import org.hibernate.reactive.mutiny.Mutiny;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.RegisterExtension;
11+
12+
import io.quarkus.arc.InjectableInstance;
13+
import io.quarkus.test.QuarkusUnitTest;
14+
15+
public class NoEntitiesNoDatasourceTest {
16+
17+
@RegisterExtension
18+
static QuarkusUnitTest runner = new QuarkusUnitTest()
19+
.withEmptyApplication()
20+
// Ideally we would not add quarkus-reactive-pg-client to the classpath and there _really_ wouldn't be a datasource,
21+
// but that's inconvenient given our testing setup,
22+
// so we'll just disable the implicit datasource.
23+
.overrideConfigKey("quarkus.datasource.reactive", "false");
24+
25+
@Inject
26+
InjectableInstance<Mutiny.SessionFactory> sessionFactory;
27+
28+
// When having no entities, no configuration, and no datasource,
29+
// as long as the Hibernate Reactive beans are not injected anywhere,
30+
// we should still be able to start the application.
31+
@Test
32+
@ActivateRequestContext
33+
public void test() {
34+
// But Hibernate Reactive should be disabled, so its beans should not be there.
35+
assertThat(sessionFactory.isUnsatisfied()).isTrue();
36+
}
37+
38+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.quarkus.hibernate.reactive;
1+
package io.quarkus.hibernate.reactive.config;
22

33
import static org.assertj.core.api.Assertions.assertThat;
44

@@ -14,7 +14,8 @@
1414

1515
/**
1616
* Test that a persistence unit without any entities does get started,
17-
* and can be used, be it only for native queries.
17+
* and can be used, be it only for native queries,
18+
* as long as a datasource is present.
1819
*/
1920
public class NoEntitiesTest {
2021

0 commit comments

Comments
 (0)