Project

General

Profile

IntegrationTestWriting » History » Version 9

Andreas Kohlbecker, 03/05/2013 02:52 PM

1 1 Andreas Kohlbecker
{{>toc}}
2 1 Andreas Kohlbecker
3 1 Andreas Kohlbecker
4 1 Andreas Kohlbecker
-----
5 1 Andreas Kohlbecker
6 1 Andreas Kohlbecker
7 1 Andreas Kohlbecker
# How to write [[IntegrationTests]] and how to generate test data for them
8 1 Andreas Kohlbecker
9 1 Andreas Kohlbecker
10 1 Andreas Kohlbecker
Related pages:
11 1 Andreas Kohlbecker
12 4 Andreas Kohlbecker
* [[IntegrationTests|IntegrationTests - Testing java code using Maven and Unitils]]
13 2 Andreas Kohlbecker
14 4 Andreas Kohlbecker
 
15 2 Andreas Kohlbecker
16 2 Andreas Kohlbecker
17 2 Andreas Kohlbecker
----
18 2 Andreas Kohlbecker
19 2 Andreas Kohlbecker
20 2 Andreas Kohlbecker
21 2 Andreas Kohlbecker
### CdmTransactionalIntegrationTest
22 2 Andreas Kohlbecker
23 2 Andreas Kohlbecker
24 7 Andreas Kohlbecker
In most cases you may want your test to be subclass of `CdmTransactionalIntegrationTest` 
25 2 Andreas Kohlbecker
26 7 Andreas Kohlbecker
After a test method is run the *transaction is usually rolled back*.
27 2 Andreas Kohlbecker
28 7 Andreas Kohlbecker
If you need something to be really persisted into the data base call the `commitAndStartNewTransaction()` 
29 2 Andreas Kohlbecker
30 7 Andreas Kohlbecker
method after the `service.saveOrUpdate(Someting)` 
31 2 Andreas Kohlbecker
32 2 Andreas Kohlbecker
33 2 Andreas Kohlbecker
34 5 Andreas Kohlbecker
### dbUnit test data
35 2 Andreas Kohlbecker
36 2 Andreas Kohlbecker
37 2 Andreas Kohlbecker
Unitils profides integation with DbUnit wich support for testing with databases and loading of DbUnit data sets. DbUnit is a JUnit extension (also usable with Ant) targeted at database-driven projects that, among other things, puts your database into a known state between test runs. This is an excellent way to avoid the myriad of problems that can occur when one test case corrupts the database and causes subsequent tests to fail or exacerbate the damage. 
38 2 Andreas Kohlbecker
39 2 Andreas Kohlbecker
40 2 Andreas Kohlbecker
41 3 Andreas Kohlbecker
### Test dependencies and load strategy
42 2 Andreas Kohlbecker
43 2 Andreas Kohlbecker
44 7 Andreas Kohlbecker
Even if DbUnit should turn your database into a known and controlled state it can happen that the database contains *leftovers from previous tests*. In order to explicitly wipe out all potential remains you can in case use specific load strategy class, the `CleanSweepInsertLoadStrategy` which will provide you a really virgin database:
45 2 Andreas Kohlbecker
46 2 Andreas Kohlbecker
~~~
47 2 Andreas Kohlbecker
@Test
48 2 Andreas Kohlbecker
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
49 2 Andreas Kohlbecker
public void testArea_area() throws FileNotFoundException {
50 2 Andreas Kohlbecker
  // your test
51 1 Andreas Kohlbecker
}
52 1 Andreas Kohlbecker
~~~
53 3 Andreas Kohlbecker
54 3 Andreas Kohlbecker
55 3 Andreas Kohlbecker
### Generating test data
56 1 Andreas Kohlbecker
57 5 Andreas Kohlbecker
58 7 Andreas Kohlbecker
The DbUnit datasets in the cdm library are provided as flat xml data files. This is quite nice until it comes to extending test or to refactoring library code. 
59 5 Andreas Kohlbecker
60 5 Andreas Kohlbecker
In this case it can become very time consuming and painful to adapt the flat xml data files accordingly. Therefore it is highly recommended to generate the test data from within the the test class itself like in the following little example:
61 5 Andreas Kohlbecker
62 5 Andreas Kohlbecker
~~~
63 5 Andreas Kohlbecker
// @Test //  uncomment and run this test method to create/update the flat xml test data file
64 5 Andreas Kohlbecker
public void createTestData() throws FileNotFoundException {
65 5 Andreas Kohlbecker
        
66 5 Andreas Kohlbecker
        // --- References --- //
67 5 Andreas Kohlbecker
        Reference sec = ReferenceFactory.newDatabase();
68 5 Andreas Kohlbecker
        sec.setTitleCache("Test", true);
69 5 Andreas Kohlbecker
        Reference nomRef = ReferenceFactory.newBook();
70 5 Andreas Kohlbecker
        sec.setTitleCache("Sp.Pl.", true);
71 5 Andreas Kohlbecker
72 5 Andreas Kohlbecker
        referenceService.save(sec);
73 5 Andreas Kohlbecker
        referenceService.save(nomRef);
74 5 Andreas Kohlbecker
75 5 Andreas Kohlbecker
        BotanicalName n_lapsana = BotanicalName.NewInstance(Rank.GENUS());
76 5 Andreas Kohlbecker
        n_lapsana.setTitleCache("Lapsana", true);
77 5 Andreas Kohlbecker
        Taxon t_lapsana = Taxon.NewInstance(n_lapsana, sec);
78 5 Andreas Kohlbecker
        t_lapsana.setUuid(T_LAPSANA_UUID);
79 5 Andreas Kohlbecker
        taxonService.saveOrUpdate(t_lapsana);
80 5 Andreas Kohlbecker
81 8 Andreas Kohlbecker
        // create more entities like classification, ....
82 6 Andreas Kohlbecker
83 5 Andreas Kohlbecker
	commitAndStartNewTransaction(null);
84 5 Andreas Kohlbecker
85 8 Andreas Kohlbecker
        // this will write flat xml file to the same package in the test resources 
86 5 Andreas Kohlbecker
        // the test file is named after the test class like: TestClassName.xml
87 5 Andreas Kohlbecker
	writeDbUnitDataSetFile(new String[] {
88 5 Andreas Kohlbecker
	        "TAXONBASE", "TAXONNAMEBASE",
89 5 Andreas Kohlbecker
	        "REFERENCE", "DESCRIPTIONELEMENTBASE", "DESCRIPTIONBASE",
90 5 Andreas Kohlbecker
	        "AGENTBASE", "CLASSIFICATION", "CLASSIFICATION_TAXONNODE", "TAXONNODE",
91 5 Andreas Kohlbecker
	        "HOMOTYPICALGROUP", "LANGUAGESTRING",
92 5 Andreas Kohlbecker
	 });
93 5 Andreas Kohlbecker
}
94 5 Andreas Kohlbecker
~~~
95 3 Andreas Kohlbecker
96 3 Andreas Kohlbecker
97 9 Andreas Kohlbecker
### Term loading in tests
98 9 Andreas Kohlbecker
99 9 Andreas Kohlbecker
100 9 Andreas Kohlbecker
Terms for tests are loaded by the `TestingTermInitializer` it loads the terms from flast xml data files. This assures that tests can rely on fixed term ids in the datbase. 
101 9 Andreas Kohlbecker
102 9 Andreas Kohlbecker
Otherwise most test data in the xml files would be broken after adding or removing terms in the cdmlib. 
103 9 Andreas Kohlbecker
104 9 Andreas Kohlbecker
The files to load are configured in the test application context:
105 9 Andreas Kohlbecker
106 9 Andreas Kohlbecker
~~~
107 9 Andreas Kohlbecker
    <bean id="termInitializer" class="eu.etaxonomy.cdm.database.TestingTermInitializer">
108 9 Andreas Kohlbecker
        <property name="termsDataSet" value="classpath:/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"/>
109 9 Andreas Kohlbecker
        <property name="termsDtd" value="classpath:/eu/etaxonomy/cdm/persistence/dao/hibernate/dataset.dtd"/>
110 9 Andreas Kohlbecker
    </bean>
111 9 Andreas Kohlbecker
~~~
112 9 Andreas Kohlbecker
113 9 Andreas Kohlbecker
114 9 Andreas Kohlbecker
115 2 Andreas Kohlbecker
116 2 Andreas Kohlbecker
117 2 Andreas Kohlbecker
 
Add picture from clipboard (Maximum size: 40 MB)