BAEL-3201 - Unit testing with Redis test containers
This commit is contained in:
parent
3f5c2b8ed5
commit
282cd67ad4
|
@ -23,6 +23,10 @@
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web-services</artifactId>
|
<artifactId>spring-boot-starter-web-services</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>wsdl4j</groupId>
|
<groupId>wsdl4j</groupId>
|
||||||
<artifactId>wsdl4j</artifactId>
|
<artifactId>wsdl4j</artifactId>
|
||||||
|
@ -38,6 +42,12 @@
|
||||||
<version>3.1.3</version>
|
<version>3.1.3</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testcontainers</groupId>
|
||||||
|
<artifactId>testcontainers</artifactId>
|
||||||
|
<version>1.17.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.redistestcontainers;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class RedisTestcontainersApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(RedisTestcontainersApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,55 @@
|
||||||
|
package com.baeldung.redistestcontainers.hash;
|
||||||
|
|
||||||
|
import org.springframework.data.redis.core.RedisHash;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@RedisHash("product")
|
||||||
|
public class Product implements Serializable {
|
||||||
|
private String id;
|
||||||
|
private String name;
|
||||||
|
private double price;
|
||||||
|
|
||||||
|
// Constructor, getters and setters
|
||||||
|
public Product() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Product(String id, String name, double price) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrice(double price) {
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Product{" +
|
||||||
|
"id='" + id + '\'' +
|
||||||
|
", name='" + name + '\'' +
|
||||||
|
", price=" + price +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.baeldung.redistestcontainers.repository;
|
||||||
|
|
||||||
|
import com.baeldung.redistestcontainers.hash.Product;
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface ProductRepository extends CrudRepository<Product, String> {
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package com.baeldung.redistestcontainers.service;
|
||||||
|
|
||||||
|
import com.baeldung.redistestcontainers.hash.Product;
|
||||||
|
import com.baeldung.redistestcontainers.repository.ProductRepository;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ProductService {
|
||||||
|
|
||||||
|
private final ProductRepository productRepository;
|
||||||
|
|
||||||
|
public ProductService(ProductRepository productRepository) {
|
||||||
|
this.productRepository = productRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Product getProduct(String id) {
|
||||||
|
return productRepository.findById(id).orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
void createProduct(Product product) {
|
||||||
|
productRepository.save(product);
|
||||||
|
}
|
||||||
|
|
||||||
|
void updateProduct(Product product) {
|
||||||
|
productRepository.save(product);
|
||||||
|
}
|
||||||
|
|
||||||
|
void deleteProduct(String id) {
|
||||||
|
productRepository.deleteById(id);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,75 @@
|
||||||
|
package com.baeldung.redistestcontainers.service;
|
||||||
|
|
||||||
|
import com.baeldung.redistestcontainers.hash.Product;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.testcontainers.containers.GenericContainer;
|
||||||
|
import org.testcontainers.utility.DockerImageName;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class ProductServiceIntegrationTest {
|
||||||
|
|
||||||
|
static {
|
||||||
|
GenericContainer<?> redis = new GenericContainer<>(DockerImageName.parse("redis:5.0.3-alpine"))
|
||||||
|
.withExposedPorts(6379);
|
||||||
|
redis.start();
|
||||||
|
System.setProperty("spring.redis.host", redis.getHost());
|
||||||
|
System.setProperty("spring.redis.port", redis.getMappedPort(6379).toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ProductService productService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCreateProduct() {
|
||||||
|
Product product = new Product("1", "Test Product", 10.0);
|
||||||
|
productService.createProduct(product);
|
||||||
|
Product productFromDb = productService.getProduct("1");
|
||||||
|
assertEquals("1", productFromDb.getId());
|
||||||
|
assertEquals("Test Product", productFromDb.getName());
|
||||||
|
assertEquals(10.0, productFromDb.getPrice());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpdateProduct() {
|
||||||
|
Product product = new Product("1", "Test Product", 10.0);
|
||||||
|
productService.createProduct(product);
|
||||||
|
Product productFromDb = productService.getProduct("1");
|
||||||
|
assertEquals("1", productFromDb.getId());
|
||||||
|
assertEquals("Test Product", productFromDb.getName());
|
||||||
|
assertEquals(10.0, productFromDb.getPrice());
|
||||||
|
productFromDb.setName("Updated Product");
|
||||||
|
productFromDb.setPrice(20.0);
|
||||||
|
productService.updateProduct(productFromDb);
|
||||||
|
Product updatedProductFromDb = productService.getProduct("1");
|
||||||
|
assertEquals("Updated Product", updatedProductFromDb.getName());
|
||||||
|
assertEquals(20.0, updatedProductFromDb.getPrice());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDeleteProduct() {
|
||||||
|
Product product = new Product("1", "Test Product", 10.0);
|
||||||
|
productService.createProduct(product);
|
||||||
|
Product productFromDb = productService.getProduct("1");
|
||||||
|
assertEquals("1", productFromDb.getId());
|
||||||
|
assertEquals("Test Product", productFromDb.getName());
|
||||||
|
assertEquals(10.0, productFromDb.getPrice());
|
||||||
|
productService.deleteProduct("1");
|
||||||
|
Product deletedProductFromDb = productService.getProduct("1");
|
||||||
|
assertNull(deletedProductFromDb);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetProduct() {
|
||||||
|
Product product = new Product("1", "Test Product", 10.0);
|
||||||
|
productService.createProduct(product);
|
||||||
|
Product productFromDb = productService.getProduct("1");
|
||||||
|
assertEquals("1", productFromDb.getId());
|
||||||
|
assertEquals("Test Product", productFromDb.getName());
|
||||||
|
assertEquals(10.0, productFromDb.getPrice());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue