Skip to content

Commit d1c0292

Browse files
artjomkabsideup
authored andcommitted
modernize Spring boot example (#1175)
* Use static approach for configuring redis container * Adds postgresql test to example * Use jdbc url scheme to startup postresql database * Cleanup + moving test specific configuration from main config * Removes unnecessary test config file, fixes annotation formatting * Removes default JDBC parameters, polishes test result message * Update application.yml * Update AbstractIntegrationTest.java * Update DemoControllerTest.java
1 parent 97e88dd commit d1c0292

File tree

10 files changed

+111
-35
lines changed

10 files changed

+111
-35
lines changed

examples/pom.xml

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
<version>1.10.19</version>
5252
<scope>test</scope>
5353
</dependency>
54+
<dependency>
55+
<groupId>org.postgresql</groupId>
56+
<artifactId>postgresql</artifactId>
57+
<version>42.2.5</version>
58+
</dependency>
5459
</dependencies>
5560

5661
<build>
@@ -72,18 +77,6 @@
7277
<testcontainers.version>-SNAPSHOT</testcontainers.version>
7378
</properties>
7479

75-
<profiles>
76-
<profile>
77-
<activation>
78-
<os>
79-
<family>Windows</family>
80-
</os>
81-
</activation>
82-
<properties>
83-
<testcontainers.version>windows-support-SNAPSHOT</testcontainers.version>
84-
</properties>
85-
</profile>
86-
</profiles>
8780

8881
<repositories>
8982
<repository>

examples/spring-boot/pom.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,20 @@
3737
<groupId>org.springframework.boot</groupId>
3838
<artifactId>spring-boot-starter-web</artifactId>
3939
</dependency>
40-
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-data-jpa</artifactId>
43+
</dependency>
4144
<dependency>
4245
<groupId>org.springframework.boot</groupId>
4346
<artifactId>spring-boot-starter-test</artifactId>
4447
<scope>test</scope>
4548
</dependency>
49+
<dependency>
50+
<groupId>${testcontainers.group}</groupId>
51+
<artifactId>postgresql</artifactId>
52+
<version>${testcontainers.version}</version>
53+
</dependency>
4654
</dependencies>
4755

4856
<build>

examples/spring-boot/src/main/java/com/example/DemoApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
56

67
@SpringBootApplication
8+
@EnableJpaRepositories
79
public class DemoApplication {
810

911
public static void main(String[] args) {

examples/spring-boot/src/main/java/com/example/DemoController.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
public class DemoController {
88

99
private final StringRedisTemplate stringRedisTemplate;
10+
private final DemoService demoService;
1011

11-
public DemoController(StringRedisTemplate stringRedisTemplate){
12+
public DemoController(StringRedisTemplate stringRedisTemplate, DemoService demoService) {
1213
this.stringRedisTemplate = stringRedisTemplate;
14+
this.demoService = demoService;
1315
}
1416

1517
@GetMapping("/foo")
@@ -21,4 +23,9 @@ public String get() {
2123
public void set(@RequestBody String value) {
2224
stringRedisTemplate.opsForValue().set("foo", value);
2325
}
26+
27+
@GetMapping("/{id}")
28+
public DemoEntity getDemoEntity(@PathVariable("id") Long id) {
29+
return demoService.getDemoEntity(id);
30+
}
2431
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.example;
2+
3+
import lombok.Data;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.Id;
9+
10+
11+
@Data
12+
@Entity
13+
public class DemoEntity {
14+
15+
@Id
16+
@GeneratedValue
17+
private Long id;
18+
19+
@Column
20+
private String value;
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example;
2+
3+
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface DemoRepository extends JpaRepository<DemoEntity, Long> {
7+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.example;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
@Service
6+
public class DemoService {
7+
private final DemoRepository demoRepository;
8+
9+
public DemoService(DemoRepository demoRepository) {
10+
this.demoRepository = demoRepository;
11+
}
12+
13+
14+
public DemoEntity getDemoEntity(Long id) {
15+
return demoRepository.findById(id)
16+
.orElseThrow(() ->new RuntimeException("Entity not found"));
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
spring:
2+
jpa:
3+
hibernate:
4+
ddl-auto: create
5+
show-sql: true
6+
properties:
7+
hibernate:
8+
jdbc:
9+
lob:
10+
non_contextual_creation: true
11+
Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
package com.example;
22

3-
import org.junit.ClassRule;
43
import org.junit.runner.RunWith;
54
import org.springframework.boot.test.context.SpringBootTest;
65
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
7-
import org.springframework.boot.test.util.TestPropertyValues;
8-
import org.springframework.context.ApplicationContextInitializer;
9-
import org.springframework.context.ConfigurableApplicationContext;
10-
import org.springframework.test.context.ContextConfiguration;
6+
import org.springframework.test.context.ActiveProfiles;
117
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
128
import org.testcontainers.containers.GenericContainer;
139

1410
@RunWith(SpringJUnit4ClassRunner.class)
15-
@SpringBootTest(classes = DemoApplication.class,webEnvironment = WebEnvironment.RANDOM_PORT)
16-
@ContextConfiguration(initializers = AbstractIntegrationTest.Initializer.class)
11+
@SpringBootTest(classes = DemoApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
12+
"spring.datasource.url=jdbc:tc:postgresql:11-alpine:///databasename",
13+
"spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver"
14+
})
15+
@ActiveProfiles("test")
1716
public abstract class AbstractIntegrationTest {
1817

19-
@ClassRule
20-
public static GenericContainer redis = new GenericContainer("redis:3.0.6").withExposedPorts(6379);
18+
static {
19+
GenericContainer redis = new GenericContainer("redis:3-alpine")
20+
.withExposedPorts(6379);
21+
redis.start();
2122

22-
public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
23-
@Override
24-
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
25-
TestPropertyValues values = TestPropertyValues.of(
26-
"spring.redis.host=" + redis.getContainerIpAddress(),
27-
"spring.redis.port=" + redis.getMappedPort(6379)
28-
);
29-
values.applyTo(configurableApplicationContext);
30-
}
31-
}
23+
System.setProperty("spring.redis.host", redis.getContainerIpAddress());
24+
System.setProperty("spring.redis.port", redis.getFirstMappedPort() + "");
3225

26+
}
3327
}

examples/spring-boot/src/test/java/com/example/DemoControllerTest.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package com.example;
22

3-
import static org.rnorth.visibleassertions.VisibleAssertions.*;
4-
53
import org.junit.Test;
64
import org.springframework.beans.factory.annotation.Autowired;
75
import org.springframework.boot.test.web.client.TestRestTemplate;
86

7+
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
8+
import static org.rnorth.visibleassertions.VisibleAssertions.info;
9+
910
public class DemoControllerTest extends AbstractIntegrationTest {
1011

1112
@Autowired
1213
TestRestTemplate restTemplate;
1314

15+
@Autowired
16+
DemoRepository demoRepository;
17+
1418
@Test
1519
public void simpleTest() {
1620
String fooResource = "/foo";
@@ -21,4 +25,15 @@ public void simpleTest() {
2125
assertEquals("value is set", "bar", restTemplate.getForObject(fooResource, String.class));
2226
}
2327

28+
@Test
29+
public void simpleJPATest() {
30+
DemoEntity demoEntity = new DemoEntity();
31+
demoEntity.setValue("Some value");
32+
demoRepository.save(demoEntity);
33+
34+
DemoEntity result = restTemplate.getForObject("/" + demoEntity.getId(), DemoEntity.class);
35+
36+
assertEquals("value is set", "Some value", result.getValue());
37+
}
38+
2439
}

0 commit comments

Comments
 (0)