Skip to content

Commit

Permalink
fix: fix demo-redis module build error, add README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
helltractor committed Dec 9, 2024
1 parent 2ab64a1 commit 546fcc7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 38 deletions.
8 changes: 8 additions & 0 deletions demo-redis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Demo-Redis

### Reference
* [redis](https://redis.io/)
* [redis commands](https://redis.io/commands)
* [spring-data-redis](https://docs.spring.io/spring-data/redis/reference/index.html)
* [testcontainers](https://www.testcontainers.org/modules/redis/)
* [testcontainers-java](https://github.com/testcontainers/testcontainers-java/tree/main/examples/spring-boot)
11 changes: 0 additions & 11 deletions demo-redis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,12 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.helltractor.demo;

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;

@SpringBootTest(classes = RedisApplication.class)
@ActiveProfiles("test")
abstract class AbstractIntegrationTest {

static GenericContainer<?> redis = new GenericContainer<>(DockerImageName.parse("redis:latest"))
.withExposedPorts(6379);

@DynamicPropertySource
static void redisProperties(DynamicPropertyRegistry registry) {
redis.start();
registry.add("spring.redis.host", redis::getHost);
registry.add("spring.redis.port", redis::getFirstMappedPort);
}

}
Original file line number Diff line number Diff line change
@@ -1,48 +1,23 @@
package com.helltractor.demo;

import com.helltractor.demo.config.RedisConfiguration;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.junit.jupiter.Container;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
@ContextConfiguration(classes = {RedisConfiguration.class})
@Testcontainers
@DirtiesContext
class RedisCommandTest {

final Logger logger = LoggerFactory.getLogger(RedisCommandTest.class);

@Value("${spring.data.redis.port}")
static int port;

@Container
static final GenericContainer redis = new GenericContainer("redis:latest")
.withExposedPorts(port);

class RedisCommandTest extends AbstractIntegrationTest {

@Autowired
RedisTemplate redisTemplate;

@BeforeEach
void setUp() {
logger.debug("RedisTemplate: {}", redisTemplate);
// clear redis history data
redisTemplate.getConnectionFactory().getConnection().flushAll();
}
Expand Down

0 comments on commit 546fcc7

Please sign in to comment.