-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from testcontainers/spring-boot-example
Add Spring Boot example of usage
- Loading branch information
Showing
9 changed files
with
191 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
spring-boot/src/main/java/com/example/DemoApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
spring-boot/src/test/java/com/example/AbstractIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
spring-boot/src/test/java/com/example/DemoApplicationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |