Project

General

Profile

IntegrationTestWriting » History » Version 11

Andreas Müller, 03/12/2013 12:52 PM

1 1 Andreas Kohlbecker
{{>toc}}
2
3
4
-----
5
6
7
# How to write [[IntegrationTests]] and how to generate test data for them
8
9
10
Related pages:
11
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
17
----
18
19
20
21
### CdmTransactionalIntegrationTest
22
23
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
33
34 5 Andreas Kohlbecker
### dbUnit test data
35 2 Andreas Kohlbecker
36
37 10 Andreas Müller
Unitils provides 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
40
41 3 Andreas Kohlbecker
### Test dependencies and load strategy
42 2 Andreas Kohlbecker
43
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
~~~
47
@Test
48
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
49
public void testArea_area() throws FileNotFoundException {
50
  // your test
51 1 Andreas Kohlbecker
}
52
~~~
53 3 Andreas Kohlbecker
54
55
### 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
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
62
~~~
63
// @Test //  uncomment and run this test method to create/update the flat xml test data file
64
public void createTestData() throws FileNotFoundException {
65
        
66
        // --- References --- //
67
        Reference sec = ReferenceFactory.newDatabase();
68
        sec.setTitleCache("Test", true);
69
        Reference nomRef = ReferenceFactory.newBook();
70
        sec.setTitleCache("Sp.Pl.", true);
71
72
        referenceService.save(sec);
73
        referenceService.save(nomRef);
74
75
        BotanicalName n_lapsana = BotanicalName.NewInstance(Rank.GENUS());
76
        n_lapsana.setTitleCache("Lapsana", true);
77
        Taxon t_lapsana = Taxon.NewInstance(n_lapsana, sec);
78
        t_lapsana.setUuid(T_LAPSANA_UUID);
79
        taxonService.saveOrUpdate(t_lapsana);
80
81 8 Andreas Kohlbecker
        // create more entities like classification, ....
82 6 Andreas Kohlbecker
83 5 Andreas Kohlbecker
	commitAndStartNewTransaction(null);
84
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
	writeDbUnitDataSetFile(new String[] {
88
	        "TAXONBASE", "TAXONNAMEBASE",
89
	        "REFERENCE", "DESCRIPTIONELEMENTBASE", "DESCRIPTIONBASE",
90
	        "AGENTBASE", "CLASSIFICATION", "CLASSIFICATION_TAXONNODE", "TAXONNODE",
91
	        "HOMOTYPICALGROUP", "LANGUAGESTRING",
92
	 });
93
}
94
~~~
95 3 Andreas Kohlbecker
96 11 Andreas Müller
But be CAREFUL. This will overwrite existing dataset files.
97
98
99
100
### Null values
101
102
103
Are expressed by [NULL], e.g.
104
105
~~~
106
<AGENTBASE DTYPE="Person" LIFESPAN_END="[NULL]"/>
107
~~~
108
109 3 Andreas Kohlbecker
110 9 Andreas Kohlbecker
### Term loading in tests
111
112
113
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. 
114
115
Otherwise most test data in the xml files would be broken after adding or removing terms in the cdmlib. 
116
117
The files to load are configured in the test application context:
118
119
~~~
120
    <bean id="termInitializer" class="eu.etaxonomy.cdm.database.TestingTermInitializer">
121
        <property name="termsDataSet" value="classpath:/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"/>
122
        <property name="termsDtd" value="classpath:/eu/etaxonomy/cdm/persistence/dao/hibernate/dataset.dtd"/>
123
    </bean>
124
~~~
125
126
127
128 2 Andreas Kohlbecker
129
130