2485486a4b34819ed6f2593c2233504997670a94
[JavaForFun] /
1 package de.spring.example.persistence.repository;
2
3 import static org.hamcrest.CoreMatchers.is;
4 import static org.junit.Assert.assertThat;
5
6 import java.time.OffsetDateTime;
7
8 import javax.inject.Inject;
9
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 import org.springframework.test.context.ContextConfiguration;
13 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14 import org.springframework.transaction.annotation.Transactional;
15
16 import de.spring.example.persistence.domain.Ad;
17
18 @RunWith(SpringJUnit4ClassRunner.class)
19 @ContextConfiguration( {"classpath*:spring-configuration/*.xml",
20         "classpath*:spring-configuration-test/*.xml"} )
21 @Transactional
22 public class AdRespositoryShould {
23         // Ad
24         public static final Long COMPANY_ID = 2L;
25         public static final Long COMPANY_CATEG_ID = 3L;
26         public static final String AD_MOBILE_IMAGE = "slippers.jpg";
27         public static final OffsetDateTime CREATED_AT = OffsetDateTime.now().minusDays(1);
28         public static final OffsetDateTime UPDATED_AT = OffsetDateTime.now();
29
30         @Inject
31         AdRepository adRepository;
32         
33         @Test public void
34         find_one_Ad_by_id() {
35                 Ad ad = createAd();
36                 Ad createdAd = adRepository.save(ad);
37                 
38                 Ad storedAd = adRepository.findOne(createdAd.getId());
39                 
40                 assertThat(createdAd, is(storedAd));
41         }
42         
43         @Test public void
44         find_one_Ad_by_id_using_native_query() {
45                 Ad ad = createAd();
46                 Ad createdAd = adRepository.save(ad);
47                 
48                 Ad storedAd = adRepository.findByIdNativeQuery(createdAd.getId());
49                                 
50                 assertThat(createdAd, is(storedAd));
51         }
52         
53         @Test public void
54         find_one_Ad_by_id_using_named_query() {
55                 Ad ad = createAd();
56                 Ad createdAd = adRepository.save(ad);
57                 
58                 Ad storedAd = adRepository.findByIdQuery(createdAd.getId());
59                                 
60                 assertThat(createdAd, is(storedAd));
61         }
62         
63         private static final Ad createAd() {    
64                 return new Ad(null, null, COMPANY_ID, COMPANY_CATEG_ID, AD_MOBILE_IMAGE,
65                                 CREATED_AT, UPDATED_AT);
66         }
67 }