Selaa lähdekoodia

Updated junit to 4.12 and created a base for the hibernate tests

Dan 11 vuotta sitten
vanhempi
commit
1e6ed67121

+ 1
- 1
pom.xml Näytä tiedosto

@@ -127,7 +127,7 @@
127 127
     <dependency>
128 128
       <groupId>junit</groupId>
129 129
       <artifactId>junit</artifactId>
130
-      <version>4.4</version>
130
+      <version>4.12</version>
131 131
       <scope>test</scope>
132 132
     </dependency>
133 133
     <dependency>

+ 0
- 76
src/test/java/se/citerus/dddsample/infrastructure/persistence/hibernate/AbstractRepositoryTest.java Näytä tiedosto

@@ -1,76 +0,0 @@
1
-package se.citerus.dddsample.infrastructure.persistence.hibernate;
2
-
3
-import org.hibernate.SessionFactory;
4
-import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
5
-import org.springframework.transaction.support.TransactionTemplate;
6
-import se.citerus.dddsample.application.util.SampleDataGenerator;
7
-import se.citerus.dddsample.domain.model.handling.HandlingEventFactory;
8
-import se.citerus.dddsample.domain.model.handling.HandlingEventRepository;
9
-
10
-import java.lang.reflect.Field;
11
-
12
-/*public abstract class AbstractRepositoryTest extends AbstractTransactionalDataSourceSpringContextTests {
13
-
14
-  SessionFactory sessionFactory;
15
-  SimpleJdbcTemplate sjt;
16
-  HandlingEventFactory handlingEventFactory;
17
-  HandlingEventRepository handlingEventRepository;
18
-
19
-  public void setHandlingEventFactory(HandlingEventFactory handlingEventFactory) {
20
-    this.handlingEventFactory = handlingEventFactory;
21
-  }
22
-
23
-  public void setHandlingEventRepository(HandlingEventRepository handlingEventRepository) {
24
-    this.handlingEventRepository = handlingEventRepository;
25
-  }
26
-
27
-  protected AbstractRepositoryTest() {
28
-    setAutowireMode(AUTOWIRE_BY_NAME);
29
-    setDependencyCheck(false);
30
-  }
31
-
32
-  public void setSessionFactory(SessionFactory sessionFactory) {
33
-    this.sessionFactory = sessionFactory;
34
-    transactionManager = new HibernateTransactionManager(sessionFactory);
35
-  }
36
-
37
-  public SessionFactory getSessionFactory() {
38
-    return sessionFactory;
39
-  }
40
-
41
-  protected void flush() {
42
-    sessionFactory.getCurrentSession().flush();
43
-  }
44
-
45
-  @Override
46
-  protected String[] getConfigLocations() {
47
-    return new String[] {"/context-infrastructure-persistence.xml", "context-domain.xml"};
48
-  }
49
-
50
-  @Override
51
-  protected void onSetUpInTransaction() throws Exception {
52
-    // TODO store Sample* and object instances here instead of handwritten SQL
53
-    SampleDataGenerator.loadSampleData(jdbcTemplate, new TransactionTemplate(transactionManager));
54
-    sjt = new SimpleJdbcTemplate(jdbcTemplate);
55
-  }
56
-
57
-  protected Session getSession() {
58
-    return sessionFactory.getCurrentSession();
59
-  }
60
-
61
-  // Instead of exposing a getId() on persistent classes
62
-  protected Long getLongId(Object o) {
63
-    if (getSession().contains(o)) {
64
-      return (Long) getSession().getIdentifier(o);
65
-    } else {
66
-      try {
67
-        Field id = o.getClass().getDeclaredField("id");
68
-        id.setAccessible(true);
69
-        return (Long) id.get(o);
70
-      } catch (Exception e) {
71
-        throw new RuntimeException(e);
72
-      }
73
-    }
74
-  }
75
-}
76
-*/

+ 54
- 23
src/test/java/se/citerus/dddsample/infrastructure/persistence/hibernate/LocationRepositoryTest.java Näytä tiedosto

@@ -1,31 +1,62 @@
1 1
 package se.citerus.dddsample.infrastructure.persistence.hibernate;
2 2
 
3
+import org.junit.Before;
4
+import org.junit.Test;
5
+import org.junit.runner.RunWith;
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.jdbc.core.JdbcTemplate;
8
+import org.springframework.orm.hibernate3.HibernateTransactionManager;
9
+import org.springframework.test.context.ContextConfiguration;
10
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
11
+import org.springframework.test.context.transaction.TransactionConfiguration;
12
+import org.springframework.transaction.annotation.Transactional;
13
+import org.springframework.transaction.support.TransactionTemplate;
14
+import se.citerus.dddsample.application.util.SampleDataGenerator;
3 15
 import se.citerus.dddsample.domain.model.location.Location;
4 16
 import se.citerus.dddsample.domain.model.location.LocationRepository;
5 17
 import se.citerus.dddsample.domain.model.location.UnLocode;
6 18
 
19
+import javax.sql.DataSource;
7 20
 import java.util.List;
8 21
 
9
-/*public class LocationRepositoryTest extends AbstractRepositoryTest {
10
-  private LocationRepository locationRepository;
11
-  
12
-  public void testFind() throws Exception {
13
-    final UnLocode melbourne = new UnLocode("AUMEL");
14
-    Location location = locationRepository.find(melbourne);
15
-    assertNotNull(location);
16
-    assertEquals(melbourne, location.unLocode());
17
-
18
-    assertNull(locationRepository.find(new UnLocode("NOLOC")));
19
-  }
20
-
21
-  public void testFindAll() throws Exception {
22
-    List<Location> allLocations = locationRepository.findAll();
23
-
24
-    assertNotNull(allLocations);
25
-    assertEquals(7, allLocations.size());
26
-  }
27
-
28
-  public void setLocationRepository(LocationRepository locationRepository) {
29
-    this.locationRepository = locationRepository;
30
-  }
31
-}*/
22
+import static org.junit.Assert.*;
23
+
24
+@RunWith(SpringJUnit4ClassRunner.class)
25
+@ContextConfiguration(value = {"/context-infrastructure-persistence.xml", "/context-domain.xml"})
26
+@TransactionConfiguration(transactionManager = "transactionManager")
27
+@Transactional
28
+public class LocationRepositoryTest {
29
+    @Autowired
30
+    private LocationRepository locationRepository;
31
+
32
+    @Autowired
33
+    private DataSource dataSource;
34
+
35
+    @Autowired
36
+    private HibernateTransactionManager transactionManager;
37
+
38
+    @Before
39
+    public void setup() {
40
+        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
41
+        SampleDataGenerator.loadSampleData(jdbcTemplate, new TransactionTemplate(transactionManager));
42
+    }
43
+
44
+    @Test
45
+    public void testFind() throws Exception {
46
+        final UnLocode melbourne = new UnLocode("AUMEL");
47
+        Location location = locationRepository.find(melbourne);
48
+        assertNotNull(location);
49
+        assertEquals(melbourne, location.unLocode());
50
+
51
+        assertNull(locationRepository.find(new UnLocode("NOLOC")));
52
+    }
53
+
54
+    @Test
55
+    public void testFindAll() throws Exception {
56
+        List<Location> allLocations = locationRepository.findAll();
57
+
58
+        assertNotNull(allLocations);
59
+        assertEquals(7, allLocations.size());
60
+    }
61
+
62
+}