Skip to content

Commit

Permalink
modernize Spring boot example (#1175)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
artjomka authored and bsideup committed Jan 24, 2019
1 parent 97e88dd commit d1c0292
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 35 deletions.
17 changes: 5 additions & 12 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>
</dependencies>

<build>
Expand All @@ -72,18 +77,6 @@
<testcontainers.version>-SNAPSHOT</testcontainers.version>
</properties>

<profiles>
<profile>
<activation>
<os>
<family>Windows</family>
</os>
</activation>
<properties>
<testcontainers.version>windows-support-SNAPSHOT</testcontainers.version>
</properties>
</profile>
</profiles>

<repositories>
<repository>
Expand Down
10 changes: 9 additions & 1 deletion examples/spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,20 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${testcontainers.group}</groupId>
<artifactId>postgresql</artifactId>
<version>${testcontainers.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories
public class DemoApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
public class DemoController {

private final StringRedisTemplate stringRedisTemplate;
private final DemoService demoService;

public DemoController(StringRedisTemplate stringRedisTemplate){
public DemoController(StringRedisTemplate stringRedisTemplate, DemoService demoService) {
this.stringRedisTemplate = stringRedisTemplate;
this.demoService = demoService;
}

@GetMapping("/foo")
Expand All @@ -21,4 +23,9 @@ public String get() {
public void set(@RequestBody String value) {
stringRedisTemplate.opsForValue().set("foo", value);
}

@GetMapping("/{id}")
public DemoEntity getDemoEntity(@PathVariable("id") Long id) {
return demoService.getDemoEntity(id);
}
}
21 changes: 21 additions & 0 deletions examples/spring-boot/src/main/java/com/example/DemoEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example;

import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;


@Data
@Entity
public class DemoEntity {

@Id
@GeneratedValue
private Long id;

@Column
private String value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example;


import org.springframework.data.jpa.repository.JpaRepository;

public interface DemoRepository extends JpaRepository<DemoEntity, Long> {
}
18 changes: 18 additions & 0 deletions examples/spring-boot/src/main/java/com/example/DemoService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example;

import org.springframework.stereotype.Service;

@Service
public class DemoService {
private final DemoRepository demoRepository;

public DemoService(DemoRepository demoRepository) {
this.demoRepository = demoRepository;
}


public DemoEntity getDemoEntity(Long id) {
return demoRepository.findById(id)
.orElseThrow(() ->new RuntimeException("Entity not found"));
}
}
11 changes: 11 additions & 0 deletions examples/spring-boot/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
spring:
jpa:
hibernate:
ddl-auto: create
show-sql: true
properties:
hibernate:
jdbc:
lob:
non_contextual_creation: true

Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
package com.example;

import org.junit.ClassRule;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.testcontainers.containers.GenericContainer;

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

@ClassRule
public static GenericContainer redis = new GenericContainer("redis:3.0.6").withExposedPorts(6379);
static {
GenericContainer redis = new GenericContainer("redis:3-alpine")
.withExposedPorts(6379);
redis.start();

public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues values = TestPropertyValues.of(
"spring.redis.host=" + redis.getContainerIpAddress(),
"spring.redis.port=" + redis.getMappedPort(6379)
);
values.applyTo(configurableApplicationContext);
}
}
System.setProperty("spring.redis.host", redis.getContainerIpAddress());
System.setProperty("spring.redis.port", redis.getFirstMappedPort() + "");

}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package com.example;

import static org.rnorth.visibleassertions.VisibleAssertions.*;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;

import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
import static org.rnorth.visibleassertions.VisibleAssertions.info;

public class DemoControllerTest extends AbstractIntegrationTest {

@Autowired
TestRestTemplate restTemplate;

@Autowired
DemoRepository demoRepository;

@Test
public void simpleTest() {
String fooResource = "/foo";
Expand All @@ -21,4 +25,15 @@ public void simpleTest() {
assertEquals("value is set", "bar", restTemplate.getForObject(fooResource, String.class));
}

@Test
public void simpleJPATest() {
DemoEntity demoEntity = new DemoEntity();
demoEntity.setValue("Some value");
demoRepository.save(demoEntity);

DemoEntity result = restTemplate.getForObject("/" + demoEntity.getId(), DemoEntity.class);

assertEquals("value is set", "Some value", result.getValue());
}

}

0 comments on commit d1c0292

Please sign in to comment.