当前位置:   article > 正文

Spring Boot集成testcontainers快速入门Demo

Spring Boot集成testcontainers快速入门Demo

1.什么是testcontainers?

Testcontainers 是一个用于创建临时 Docker 容器进行单元测试的 Java 库。当我们想要避免使用实际服务器进行测试时,它非常有用。,官网介绍称支持50多种组件。

 

应用场景

数据访问层集成测试:

使用MySQL,PostgreSQL或Oracle数据库的容器化实例测试您的数据访问层代码,但无需在开发人员的计算机上进行复杂的设置,并且测试将始终从已知的数据库状态开始,避免“垃圾”数据的干扰。也可以使用任何其他可以容器化的数据库类型。

应用程序集成测试:

用于在具有相关性(例如数据库,消息队列或Web服务器)的短期测试模式下运行应用程序。

UI /验收测试:

使用与Selenium兼容的容器化Web浏览器进行自动化UI测试。每个测试都可以获取浏览器的新实例,而无需担心浏览器状态,插件版本或浏览器自动升级。您将获得每个测试会话或测试失败的视频记录。

2.代码工程

实验目的:在 Spring Boot 中使用 Testcontainers 测试 Redis。

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>springboot-demo</artifactId>
  7. <groupId>com.et</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>testcontainers</artifactId>
  12. <properties>
  13. <maven.compiler.source>8</maven.compiler.source>
  14. <maven.compiler.target>8</maven.compiler.target>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-autoconfigure</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-test</artifactId>
  28. <scope>test</scope>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-data-redis</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.testcontainers</groupId>
  36. <artifactId>testcontainers</artifactId>
  37. <version>1.17.2</version>
  38. <scope>test</scope>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.projectlombok</groupId>
  42. <artifactId>lombok</artifactId>
  43. </dependency>
  44. </dependencies>
  45. </project>

entity

  1. package com.et.testcontainers.entity;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import org.springframework.data.redis.core.RedisHash;
  5. import java.io.Serializable;
  6. @RedisHash("product")
  7. @AllArgsConstructor
  8. @Data
  9. public class Product implements Serializable {
  10. private String id;
  11. private String name;
  12. private double price;
  13. }

service

  1. package com.et.testcontainers.service;
  2. import com.et.testcontainers.Repository.ProductRepository;
  3. import com.et.testcontainers.entity.Product;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. @Service
  7. public class ProductService {
  8. @Autowired
  9. private ProductRepository productRepository;
  10. public Product getProduct(String id) {
  11. return productRepository.findById(id).orElse(null);
  12. }
  13. public void createProduct(Product product) {
  14. productRepository.save(product);
  15. }
  16. }

Repository

  1. package com.et.testcontainers.Repository;
  2. import com.et.testcontainers.entity.Product;
  3. import org.springframework.data.repository.CrudRepository;
  4. import org.springframework.stereotype.Repository;
  5. @Repository
  6. public interface ProductRepository extends CrudRepository<Product, String> {
  7. }

appliication.properties

  1. spring.redis.host=127.0.0.1
  2. spring.redis.port=6379

启动类

  1. package com.et.testcontainers;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class DemoApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(DemoApplication.class, args);
  8. }
  9. }

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

3.测试

编写测试类

  1. package com.et.testcontainers;
  2. import com.et.testcontainers.entity.Product;
  3. import com.et.testcontainers.service.ProductService;
  4. import org.junit.After;
  5. import org.junit.Before;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.boot.test.context.SpringBootTest;
  12. import org.springframework.test.context.junit4.SpringRunner;
  13. import org.testcontainers.containers.GenericContainer;
  14. import org.testcontainers.utility.DockerImageName;
  15. import static org.junit.Assert.assertEquals;
  16. @RunWith(SpringRunner.class)
  17. @SpringBootTest(classes = DemoApplication.class)
  18. public class DemoTests {
  19. private static Logger log = LoggerFactory.getLogger(DemoTests.class);
  20. @Autowired
  21. ProductService productService;
  22. @Before
  23. public void before() {
  24. log.info("init some data");
  25. }
  26. @After
  27. public void after(){
  28. log.info("clean some data");
  29. }
  30. @Test
  31. public void execute() {
  32. log.info("your method test Code");
  33. }
  34. static {
  35. GenericContainer<?> redis =
  36. new GenericContainer<>(DockerImageName.parse("redis:5.0.3-alpine")).withExposedPorts(6379);
  37. redis.start();
  38. log.info(redis.getHost());
  39. log.info(redis.getMappedPort(6379).toString());
  40. System.setProperty("spring.redis.host", redis.getHost());
  41. System.setProperty("spring.redis.port", redis.getMappedPort(6379).toString());
  42. }
  43. @Test
  44. public void givenProductCreated_whenGettingProductById_thenProductExistsAndHasSameProperties() {
  45. Product product = new Product("1", "Test Product", 10.0);
  46. productService.createProduct(product);
  47. Product productFromDb = productService.getProduct("1");
  48. assertEquals("1", productFromDb.getId());
  49. assertEquals("Test Product", productFromDb.getName());
  50. assertEquals(10.0, productFromDb.getPrice(),0.001);
  51. }
  52. }

执行测试用例,全部通过,这样就可以脱离实际环境测试我们的代码,每次也不用因为环境不对而跳过单元测试

4.参考

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/619786
推荐阅读
相关标签
  

闽ICP备14008679号