You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: test-case-guide.adoc
+183-2
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,188 @@ There are a number of tenants that make up a good test case as opposed to a poor
13
13
* (V)erifiable - The test should actually reproduce the problem being reported.
14
14
15
15
16
-
== Test templates
16
+
[[junit5]]
17
+
== JUnit 5 extensions
18
+
19
+
JUnit 5 offers better support for integration, compared to JUnit 4, via https://junit.org/junit5/docs/current/user-guide/#extensions[extensions]. Hibernate builds on those concepts in its `hibernate-testing` module allowing set up of test fixtures using annotations. The following sections describe the Hibernate extensions.
20
+
21
+
NOTE: The extensions exist in the `org.hibernate.testing.orm.junit` package, as opposed to the older `org.hibernate.testing.junit4` package used with JUnit 4.
22
+
23
+
24
+
[[junit5-service-registry]]
25
+
=== ServiceRegistryExtension
26
+
27
+
Manages a `ServiceRegistry` as part of the test lifecycle. 2 in fact, depending on the annotation(s) used.
28
+
29
+
`@BootstrapServiceRegistry`:: configures Hibernate's bootstrap `BootstrapServiceRegistry` which manages class-loading, etc. `@BootstrapServiceRegistry` is used to provide Java services and Hibernate `Integrator` implementations for the test.
30
+
`@ServiceRegistry`:: configures Hibernate's standard `StandardServiceRegistry`. `@ServiceRegistry` is used to provide settings, contributors, services, etc.
31
+
32
+
Also exposes `ServiceRegistryScope` via JUnit 5 `ParameterResolver`. `ServiceRegistryScope` allows
33
+
access to the managed `ServiceRegistry` from tests and callbacks.
Manages the domain model for the test as part of its lifecycle.
67
+
68
+
`@DomainModel`:: defines the sources of the domain model used in the test - type contributions, managed classes, XML mappings, etc.
69
+
70
+
If available, this extension uses the `ServiceRegistry` instances available from <<junit5-service-registry>>.
71
+
72
+
Exposes `DomainModelScope` via JUnit5 `ParameterResolver`, allowing access to details about the domain model from the `org.hibernate.mapping` "boot model".
73
+
74
+
75
+
```
76
+
@DomainModel(
77
+
standardDomainModels=StandardDomainModel.ANIMAL,
78
+
annotatedClasses={Entity1.class, Entity2.class},
79
+
xmlMappings="resource/path/to/my-mapping.xml",
80
+
...
81
+
)
82
+
class TheTest {
83
+
@Test void testIt(DomainModelScope scope) {
84
+
MetadataImplementor meta = scope.getDomainModel();
Allows filtering tests based on Dialect used. Implemented as a JUnit `ExecutionCondition` which is used to dynamically determine whether a test should be run. Used in conjunction with:
138
+
139
+
`@RequiresDialect`:: says to only run this test for the given Dialect(s).
140
+
`@SkipForDialect`:: says to skip this test for the given Dialect(s).
141
+
142
+
=== ExpectedExceptionExtension
143
+
144
+
Used with `@ExpectedException` to allow testing that an excepted exception occurs as the "success" condition.
Used with `@FailureExpected` to indicate that a test is (currently) expected to fail. You might use this, e.g., for a test that is the reproducer for a bug report before working on it. It basically just flips the success/failure condition. In fact, a test marked with `@FailureExpected` will be marked a failure if it succeeds.
165
+
166
+
```
167
+
@Test
168
+
@JiraKey("HHH-123456789")
169
+
@FailureExpected
170
+
void bugReproducer(...) {...}
171
+
```
172
+
173
+
174
+
=== LoggingInspectionsExtension and MessageKeyInspectionExtension
175
+
176
+
Both are used for testing log messages.
177
+
178
+
`@LoggingInspections`:: used to watch more than one "message key".
179
+
`MessageKeyInspection`:: used to watch a single "message key".
180
+
181
+
182
+
=== EntityManagerExtension
183
+
184
+
Used in conjunction with `@Jpa` to build tests with an `EntityManagerFactory` fixture.
185
+
186
+
Since Hibernate's `SessionFactory` *is a* `EntityManagerFactory`, `@BootstrapServiceRegistry`, `@ServiceRegistry`, `@DomainModel` and `@SessionFactory` can also be used to perform tests with a (`SessionFactory` as a) `EntityManagerFactory` fixture.
187
+
188
+
The distinction with `@Jpa` is that `EntityManagerExtension` uses the JPA-defined bootstrap APIs. How the
189
+
`SessionFactory` is built is the difference.
190
+
191
+
192
+
== JUnit 4
193
+
194
+
Historically, Hibernate used JUnit 4 for its test suite. Since the release of https://junit.org/junit5/[JUnit 5], we've moved to using the testing approach outlined in <<junit5>>. However, many existing tests still use the legacy JUnit 4 based infrastructure (boilerplate) based on "test templates".
195
+
196
+
197
+
=== Test templates
17
198
18
199
The Hibernate team maintains a set of "test templates" intended to help developers write tests. These test templates are maintained in GitHub @ https://github.com/hibernate/hibernate-test-case-templates/tree/main/orm[hibernate-test-case-templates]
19
200
@@ -22,7 +203,7 @@ The Hibernate team maintains a set of "test templates" intended to help develope
22
203
23
204
NOTE: the test templates are generally not a good starting point for problems building the SessionFactory/EntityManager. In JUnit terms they manage the SessionFactory/EntityManager as set-up and teardown constructs._
24
205
25
-
== Annotations
206
+
=== Annotations
26
207
27
208
When using "test templates" you can annotate a single test or a whole test class with one of the following annotations:
0 commit comments