@@ -195,7 +195,6 @@ applications. It includes the following topics:
195
195
* <<integration-testing-annotations>>
196
196
* <<testcontext-framework>>
197
197
* <<spring-mvc-test-framework>>
198
- * <<testing-examples-petclinic>>
199
198
200
199
201
200
@@ -3708,8 +3707,7 @@ a Hibernate-based `UserRepository`:
3708
3707
3709
3708
As explained in <<testcontext-tx-rollback-and-commit-behavior>>, there is no need to
3710
3709
clean up the database after the `createUser()` method runs, since any changes made to the
3711
- database are automatically rolled back by the `TransactionalTestExecutionListener`. See
3712
- <<testing-examples-petclinic>> for an additional example.
3710
+ database are automatically rolled back by the `TransactionalTestExecutionListener`.
3713
3711
3714
3712
[[testcontext-tx-rollback-and-commit-behavior]]
3715
3713
===== Transaction Rollback and Commit Behavior
@@ -6136,113 +6134,6 @@ include::testing-webtestclient.adoc[leveloffset=+2]
6136
6134
6137
6135
6138
6136
6139
- [[testing-examples-petclinic]]
6140
- === PetClinic Example
6141
-
6142
- The PetClinic application, available on
6143
- https://github.com/spring-projects/spring-petclinic[GitHub], shows several features of
6144
- the Spring TestContext Framework in a JUnit 4 environment. Most test functionality is
6145
- included in the `AbstractClinicTests`, for which a partial listing follows:
6146
-
6147
- ====
6148
- [source,java,indent=0]
6149
- [subs="verbatim,quotes"]
6150
- ----
6151
- import static org.junit.Assert.assertEquals;
6152
- // import ...
6153
-
6154
- @ContextConfiguration <1>
6155
- public abstract class AbstractClinicTests extends AbstractTransactionalJUnit4SpringContextTests { <2>
6156
-
6157
- @Autowired <3>
6158
- protected Clinic clinic;
6159
-
6160
- @Test
6161
- public void getVets() {
6162
- Collection<Vet> vets = this.clinic.getVets();
6163
- assertEquals("JDBC query must show the same number of vets",
6164
- super.countRowsInTable("VETS"), vets.size()); <4>
6165
- Vet v1 = EntityUtils.getById(vets, Vet.class, 2);
6166
- assertEquals("Leary", v1.getLastName());
6167
- assertEquals(1, v1.getNrOfSpecialties());
6168
- assertEquals("radiology", (v1.getSpecialties().get(0)).getName());
6169
- // ...
6170
- }
6171
-
6172
- // ...
6173
- }
6174
- ----
6175
-
6176
- <1> Load the application context from the default location: `AbstractClinicTests-context.xml`.
6177
- <2> This test case extends the `AbstractTransactionalJUnit4SpringContextTests` class, from
6178
- which it inherits configuration for Dependency Injection (through the
6179
- `DependencyInjectionTestExecutionListener`) and transactional behavior (through the
6180
- `TransactionalTestExecutionListener`).
6181
- <3> The `clinic` instance variable (the application object being tested) is set by
6182
- Dependency Injection through `@Autowired` semantics.
6183
- <4> The `getVets()` method shows how you can use the inherited `countRowsInTable()`
6184
- method to easily verify the number of rows in a given table, thus verifying correct
6185
- behavior of the application code being tested. This allows for stronger tests and
6186
- lessens dependency on the exact test data. For example, you can add additional rows in
6187
- the database without breaking tests.
6188
- ====
6189
-
6190
- Like many integration tests that use a database, most of the tests in
6191
- `AbstractClinicTests` depend on a minimum amount of data already being in the database
6192
- before the test cases run. Alternatively, you can populate the database within the test
6193
- fixture set up of your test cases (again, within the same transaction as the tests).
6194
-
6195
- The PetClinic application supports three data access technologies: JDBC, Hibernate, and
6196
- JPA. By declaring `@ContextConfiguration` without any specific resource locations, the
6197
- `AbstractClinicTests` class has its application context loaded from the default location,
6198
- `AbstractClinicTests-context.xml`, which declares a common `DataSource`. Subclasses
6199
- specify additional context locations that must declare a `PlatformTransactionManager` and
6200
- a concrete implementation of `Clinic`.
6201
-
6202
- For example, the Hibernate implementation of the PetClinic tests contains the following
6203
- implementation. For this example, `HibernateClinicTests` does not contain a single line
6204
- of code. We need only to declare `@ContextConfiguration`, and the tests are inherited
6205
- from `AbstractClinicTests`. Because `@ContextConfiguration` is declared without any
6206
- specific resource locations, the Spring TestContext Framework loads an application
6207
- context from all the beans defined in `AbstractClinicTests-context.xml` (that is, the
6208
- inherited locations) and `HibernateClinicTests-context.xml`, with
6209
- `HibernateClinicTests-context.xml` possibly overriding beans defined in
6210
- `AbstractClinicTests-context.xml`. The following listing shows the definition of the
6211
- `HibernateClinicTests` class:
6212
-
6213
- ====
6214
- [source,java,indent=0]
6215
- [subs="verbatim,quotes"]
6216
- ----
6217
- @ContextConfiguration <1>
6218
- public class HibernateClinicTests extends AbstractClinicTests { }
6219
- ----
6220
- <1> Load the application context from `AbstractClinicTests-context.xml` and `HibernateClinicTests-context.xml`.
6221
- ====
6222
-
6223
- In a large-scale application, the Spring configuration is often split across multiple
6224
- files. Consequently, configuration locations are typically specified in a common base
6225
- class for all application-specific integration tests. Such a base class can also add
6226
- useful instance variables (populated by Dependency Injection, naturally), such as a
6227
- `SessionFactory` in the case of an application that uses Hibernate.
6228
-
6229
- As far as possible, you should have exactly the same Spring configuration files in your
6230
- integration tests as in the deployed environment. One likely point of difference concerns
6231
- database connection pooling and transaction infrastructure. If you are deploying to a
6232
- full-blown application server, you probably use its connection pool (available through
6233
- JNDI) and JTA implementation. Thus, in production, you can use a `JndiObjectFactoryBean`
6234
- or `<jee:jndi-lookup>` for the `DataSource` and `JtaTransactionManager`. JNDI and JTA are
6235
- not available in out-of-container integration tests, so you should use a combination such
6236
- as the Commons DBCP `BasicDataSource` and `DataSourceTransactionManager` or
6237
- `HibernateTransactionManager` for them. You can factor out this variant behavior into a
6238
- single XML file, having the choice between application server and a "`local`"
6239
- configuration separated from all other configuration, which will not vary between the
6240
- test and production environments. In addition, we recommend that you use properties files
6241
- for connection settings. See the PetClinic application for an example.
6242
-
6243
-
6244
-
6245
-
6246
6137
[[testing-resources]]
6247
6138
== Further Resources
6248
6139
See the following resources for more information about testing:
0 commit comments