Project

General

Profile

IntegrationTestWriting » History » Version 5

Andreas Kohlbecker, 03/05/2013 02:28 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
* In most cases you may want your test to be subclass of `CdmTransactionalIntegrationTest` 
25
26
  * After a test method is run the transaction is usuallay rolled back.
27
28
  * If you need something to be really persisted into the data base call the `commitAndStartNewTransaction()` 
29
30
  method after the `service.saveOrUpdate(Someting)` 
31
32
33
34 5 Andreas Kohlbecker
### dbUnit test data
35 2 Andreas Kohlbecker
36
37
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
39
40
41 3 Andreas Kohlbecker
### Test dependencies and load strategy
42 2 Andreas Kohlbecker
43
44
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
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
The DbUnit datasets in the cdm library are provided as flat xml data files. This is quite nice Untils it comes to extending test ore to refactoring library code. 
59
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
	commitAndStartNewTransaction(null);
82
83
        // this will write falt xml file to the same package in the test resources 
84
        // the test file is named after the test class like: TestClassName.xml
85
	writeDbUnitDataSetFile(new String[] {
86
	        "TAXONBASE", "TAXONNAMEBASE",
87
	        "REFERENCE", "DESCRIPTIONELEMENTBASE", "DESCRIPTIONBASE",
88
	        "AGENTBASE", "CLASSIFICATION", "CLASSIFICATION_TAXONNODE", "TAXONNODE",
89
	        "HOMOTYPICALGROUP", "LANGUAGESTRING",
90
	 });
91
}
92
~~~
93 3 Andreas Kohlbecker
94
95 2 Andreas Kohlbecker
96
97