Skip to content

Commit

Permalink
Merge pull request #2 from testcontainers/spring-boot-example
Browse files Browse the repository at this point in the history
Add Spring Boot example of usage
  • Loading branch information
rnorth authored Sep 9, 2016
2 parents ff5dd65 + 269c58c commit fc74c90
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 12 deletions.
12 changes: 12 additions & 0 deletions disque-job-queue/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
<scope>test</scope>
</dependency>
</dependencies>


Expand Down
13 changes: 1 addition & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<module>redis-backed-cache</module>
<module>disque-job-queue</module>
<module>selenium-container</module>
<module>spring-boot</module>
</modules>

<packaging>pom</packaging>
Expand Down Expand Up @@ -36,18 +37,6 @@
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.rnorth.visible-assertions</groupId>
<artifactId>visible-assertions</artifactId>
Expand Down
12 changes: 12 additions & 0 deletions redis-backed-cache/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
<scope>test</scope>
</dependency>
</dependencies>


Expand Down
12 changes: 12 additions & 0 deletions selenium-container/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@
<artifactId>selenium</artifactId>
<version>${testcontainers.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
<scope>test</scope>
</dependency>
</dependencies>


Expand Down
57 changes: 57 additions & 0 deletions spring-boot/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
<artifactId>testcontainers-java-examples</artifactId>
<groupId>org.testcontainers.examples</groupId>
<version>NOVERSION</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-boot</artifactId>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.3.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.5.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
32 changes: 32 additions & 0 deletions spring-boot/src/main/java/com/example/DemoApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

@Autowired
StringRedisTemplate stringRedisTemplate;

@RequestMapping(value = "/foo", method = RequestMethod.GET)
public String get() {
return stringRedisTemplate.opsForValue().get("foo");
}

@RequestMapping(value = "/foo", method = RequestMethod.PUT)
public void set(@RequestBody String value) {
stringRedisTemplate.opsForValue().set("foo", value);
}
}
34 changes: 34 additions & 0 deletions spring-boot/src/test/java/com/example/AbstractIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example;

import org.junit.ClassRule;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.testcontainers.containers.GenericContainer;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(value = DemoApplication.class, initializers = AbstractIntegrationTest.Initializer.class)
@WebIntegrationTest(randomPort = true)
public abstract class AbstractIntegrationTest {

@ClassRule
public static GenericContainer redis = new GenericContainer("redis:3.0.6").withExposedPorts(6379);

public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
EnvironmentTestUtils.addEnvironment("testcontainers", configurableApplicationContext.getEnvironment(),
"spring.redis.host=" + redis.getContainerIpAddress(),
"spring.redis.port=" + redis.getMappedPort(6379)
);
}
}

@Value("${local.server.port}")
protected int port;
}
23 changes: 23 additions & 0 deletions spring-boot/src/test/java/com/example/DemoApplicationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example;

import org.junit.Test;
import org.springframework.web.client.RestTemplate;


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

public class DemoApplicationTest extends AbstractIntegrationTest {

RestTemplate restTemplate = new RestTemplate();

@Test
public void simpleTest() {
String fooResource = "http://localhost:" + port + "/foo";

info("putting 'bar' to " + fooResource);
restTemplate.put(fooResource, "bar");

assertEquals("value is set", "bar", restTemplate.getForObject(fooResource, String.class));
}

}
8 changes: 8 additions & 0 deletions spring-boot/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<configuration>

<include resource="org/springframework/boot/logging/logback/base.xml"/>

<logger name="org.apache.http" level="WARN"/>
<logger name="com.github.dockerjava" level="WARN"/>
<logger name="org.zeroturnaround.exec" level="WARN"/>
</configuration>

0 comments on commit fc74c90

Please sign in to comment.