@@ -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
@@ -3579,8 +3578,7 @@ a Hibernate-based `UserRepository`:
3579
3578
3580
3579
As explained in <<testcontext-tx-rollback-and-commit-behavior>>, there is no need to
3581
3580
clean up the database after the `createUser()` method runs, since any changes made to the
3582
- database are automatically rolled back by the `TransactionalTestExecutionListener`. See
3583
- <<testing-examples-petclinic>> for an additional example.
3581
+ database are automatically rolled back by the `TransactionalTestExecutionListener`.
3584
3582
3585
3583
[[testcontext-tx-rollback-and-commit-behavior]]
3586
3584
===== Transaction Rollback and Commit Behavior
@@ -5861,111 +5859,6 @@ include::testing-webtestclient.adoc[leveloffset=+2]
5861
5859
5862
5860
5863
5861
5864
- [[testing-examples-petclinic]]
5865
- === PetClinic Example
5866
-
5867
- The PetClinic application, available on
5868
- https://github.com/spring-projects/spring-petclinic[GitHub], shows several features of
5869
- the Spring TestContext Framework in a JUnit 4 environment. Most test functionality is
5870
- included in the `AbstractClinicTests`, for which a partial listing follows:
5871
-
5872
- [source,java,indent=0]
5873
- [subs="verbatim,quotes"]
5874
- ----
5875
- import static org.junit.Assert.assertEquals;
5876
- // import ...
5877
-
5878
- @ContextConfiguration <1>
5879
- public abstract class AbstractClinicTests extends AbstractTransactionalJUnit4SpringContextTests { <2>
5880
-
5881
- @Autowired <3>
5882
- protected Clinic clinic;
5883
-
5884
- @Test
5885
- public void getVets() {
5886
- Collection<Vet> vets = this.clinic.getVets();
5887
- assertEquals("JDBC query must show the same number of vets",
5888
- super.countRowsInTable("VETS"), vets.size()); <4>
5889
- Vet v1 = EntityUtils.getById(vets, Vet.class, 2);
5890
- assertEquals("Leary", v1.getLastName());
5891
- assertEquals(1, v1.getNrOfSpecialties());
5892
- assertEquals("radiology", (v1.getSpecialties().get(0)).getName());
5893
- // ...
5894
- }
5895
-
5896
- // ...
5897
- }
5898
- ----
5899
-
5900
- <1> Load the application context from the default location: `AbstractClinicTests-context.xml`.
5901
- <2> This test case extends the `AbstractTransactionalJUnit4SpringContextTests` class, from
5902
- which it inherits configuration for Dependency Injection (through the
5903
- `DependencyInjectionTestExecutionListener`) and transactional behavior (through the
5904
- `TransactionalTestExecutionListener`).
5905
- <3> The `clinic` instance variable (the application object being tested) is set by
5906
- Dependency Injection through `@Autowired` semantics.
5907
- <4> The `getVets()` method shows how you can use the inherited `countRowsInTable()`
5908
- method to easily verify the number of rows in a given table, thus verifying correct
5909
- behavior of the application code being tested. This allows for stronger tests and
5910
- lessens dependency on the exact test data. For example, you can add additional rows in
5911
- the database without breaking tests.
5912
-
5913
-
5914
- Like many integration tests that use a database, most of the tests in
5915
- `AbstractClinicTests` depend on a minimum amount of data already being in the database
5916
- before the test cases run. Alternatively, you can populate the database within the test
5917
- fixture set up of your test cases (again, within the same transaction as the tests).
5918
-
5919
- The PetClinic application supports three data access technologies: JDBC, Hibernate, and
5920
- JPA. By declaring `@ContextConfiguration` without any specific resource locations, the
5921
- `AbstractClinicTests` class has its application context loaded from the default location,
5922
- `AbstractClinicTests-context.xml`, which declares a common `DataSource`. Subclasses
5923
- specify additional context locations that must declare a `PlatformTransactionManager` and
5924
- a concrete implementation of `Clinic`.
5925
-
5926
- For example, the Hibernate implementation of the PetClinic tests contains the following
5927
- implementation. For this example, `HibernateClinicTests` does not contain a single line
5928
- of code. We need only to declare `@ContextConfiguration`, and the tests are inherited
5929
- from `AbstractClinicTests`. Because `@ContextConfiguration` is declared without any
5930
- specific resource locations, the Spring TestContext Framework loads an application
5931
- context from all the beans defined in `AbstractClinicTests-context.xml` (that is, the
5932
- inherited locations) and `HibernateClinicTests-context.xml`, with
5933
- `HibernateClinicTests-context.xml` possibly overriding beans defined in
5934
- `AbstractClinicTests-context.xml`. The following listing shows the definition of the
5935
- `HibernateClinicTests` class:
5936
-
5937
- [source,java,indent=0]
5938
- [subs="verbatim,quotes"]
5939
- ----
5940
- @ContextConfiguration <1>
5941
- public class HibernateClinicTests extends AbstractClinicTests { }
5942
- ----
5943
- <1> Load the application context from `AbstractClinicTests-context.xml` and `HibernateClinicTests-context.xml`.
5944
-
5945
-
5946
- In a large-scale application, the Spring configuration is often split across multiple
5947
- files. Consequently, configuration locations are typically specified in a common base
5948
- class for all application-specific integration tests. Such a base class can also add
5949
- useful instance variables (populated by Dependency Injection, naturally), such as a
5950
- `SessionFactory` in the case of an application that uses Hibernate.
5951
-
5952
- As far as possible, you should have exactly the same Spring configuration files in your
5953
- integration tests as in the deployed environment. One likely point of difference concerns
5954
- database connection pooling and transaction infrastructure. If you are deploying to a
5955
- full-blown application server, you probably use its connection pool (available through
5956
- JNDI) and JTA implementation. Thus, in production, you can use a `JndiObjectFactoryBean`
5957
- or `<jee:jndi-lookup>` for the `DataSource` and `JtaTransactionManager`. JNDI and JTA are
5958
- not available in out-of-container integration tests, so you should use a combination such
5959
- as the Commons DBCP `BasicDataSource` and `DataSourceTransactionManager` or
5960
- `HibernateTransactionManager` for them. You can factor out this variant behavior into a
5961
- single XML file, having the choice between application server and a "`local`"
5962
- configuration separated from all other configuration, which will not vary between the
5963
- test and production environments. In addition, we recommend that you use properties files
5964
- for connection settings. See the PetClinic application for an example.
5965
-
5966
-
5967
-
5968
-
5969
5862
[[testing-resources]]
5970
5863
== Further Resources
5971
5864
See the following resources for more information about testing:
0 commit comments