bc53430c290a717bc5828243c0e48f203bede380
[JavaForFun] /
1 package de.spring.example.persistence.repository;
2
3 import static org.hamcrest.CoreMatchers.is;
4 import static org.hamcrest.CoreMatchers.notNullValue;
5 import static org.junit.Assert.assertThat;
6
7 import javax.inject.Inject;
8
9 import org.junit.ClassRule;
10 import org.junit.Ignore;
11 import org.junit.Test;
12 import org.junit.runner.RunWith;
13 import org.springframework.test.context.ContextConfiguration;
14 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
15 import org.springframework.transaction.annotation.Transactional;
16
17 import com.palantir.docker.compose.DockerComposeRule;
18 import com.palantir.docker.compose.connection.waiting.HealthChecks;
19
20 import de.spring.example.persistence.domain.AdDescription;
21
22 @RunWith(SpringJUnit4ClassRunner.class)
23 @ContextConfiguration( {"classpath*:spring-configuration/*.xml",
24         "classpath*:spring-configuration-docker-test/*.xml"} )
25 @Transactional
26
27 // IT DOES NOT WORK FOR THESE REASONS:
28 // 1. Spring context is loaded before the DockerComposeRule
29 // 2. DockerComposeRule can work with random ports, the problem is:
30 // the Spring Context was loaded before DockerComposeRule and dataSource
31 // requires some port for connection to data base.
32
33 // These issues can be fixed using org.junit.Suite and running
34 // DockerComposeRule in the Suite instead of in every Test.
35 // But if I want to run just one test that solution does not work because
36 // DockerComposeRule will be declared in the Suite instead of in the Test.
37 // We will have to run our tests always from the Suite not being able to run
38 // just one Test from the IDE :(
39 @Ignore
40 public class AdDescriptionRepositoryDockerComposeRuleShould {
41         
42         @Inject
43         AdDescriptionRepository adDescriptionRepository;
44
45         @ClassRule
46     public static final DockerComposeRule DOCKER = DockerComposeRule.builder()
47             .file("src/integTest/resources/docker/docker-compose.yml")
48             .waitingForService("mysql", HealthChecks.toHaveAllPortsOpen())
49             .saveLogsTo("build/dockerLogs")
50             .build();
51         
52         @Test public void
53         find_ad_descriptions_by_ad() {
54                 Iterable<AdDescription> adDescriptions = adDescriptionRepository.findAll();
55                 
56                 for (AdDescription adDescription : adDescriptions) {
57                         assertThat(adDescription, is(notNullValue()));
58                 }
59         }
60         
61 }