BAEL-3201 - Unit testing with Redis test containers (#12441)

* BAEL-3201 - Unit testing with Redis test containers

* BAEL-3201 - Updated test method names

Co-authored-by: Abhinav Pandey <Abhinav2.Pandey@airtel.com>
This commit is contained in:
Abhinav Pandey
2022-07-14 09:50:08 +05:30
committed by GitHub
parent 3860716e20
commit 56fd1ef7b4
6 changed files with 193 additions and 0 deletions
@@ -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);
}
}