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

<?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>springboot-demo</artifactId><groupId>com.et</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>testcontainers</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.testcontainers</groupId><artifactId>testcontainers</artifactId><version>1.17.2</version><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies>
</project>

entity

package com.et.testcontainers.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.data.redis.core.RedisHash;import java.io.Serializable;@RedisHash("product")
@AllArgsConstructor
@Data
public class Product implements Serializable {private String id;private String name;private double price;}

service

package com.et.testcontainers.service;import com.et.testcontainers.Repository.ProductRepository;
import com.et.testcontainers.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class ProductService {@Autowiredprivate ProductRepository productRepository;public Product getProduct(String id) {return productRepository.findById(id).orElse(null);}public void createProduct(Product product) {productRepository.save(product);}
}

Repository

package com.et.testcontainers.Repository;import com.et.testcontainers.entity.Product;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;@Repository
public interface ProductRepository extends CrudRepository<Product, String> {
}

appliication.properties

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

啟動類

package com.et.testcontainers;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}

以上只是一些關鍵代碼,所有代碼請參見下面代碼倉庫

代碼倉庫

  • GitHub - Harries/springboot-demo: a simple springboot demo with some components for example: redis,solr,rockmq and so on.

3.測試

編寫測試類

package com.et.testcontainers;import com.et.testcontainers.entity.Product;
import com.et.testcontainers.service.ProductService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;import static org.junit.Assert.assertEquals;@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class DemoTests {private static Logger log = LoggerFactory.getLogger(DemoTests.class);@AutowiredProductService productService;@Beforepublic void before()  {log.info("init some data");}@Afterpublic void after(){log.info("clean some data");}@Testpublic void execute()  {log.info("your method test Code");}static {GenericContainer<?> redis =new GenericContainer<>(DockerImageName.parse("redis:5.0.3-alpine")).withExposedPorts(6379);redis.start();log.info(redis.getHost());log.info(redis.getMappedPort(6379).toString());System.setProperty("spring.redis.host", redis.getHost());System.setProperty("spring.redis.port", redis.getMappedPort(6379).toString());}@Testpublic void givenProductCreated_whenGettingProductById_thenProductExistsAndHasSameProperties() {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(),0.001);}
}

執行測試用例,全部通過,這樣就可以脫離實際環境測試我們的代碼,每次也不用因為環境不對而跳過單元測試

4.參考

  • 使用 Testcontainers 測試 Redis - spring 中文網
  • Testcontainers
  • Spring Boot集成testcontainers快速入門Demo | Harries Blog?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/16476.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/16476.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/16476.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

ubuntu20安裝Labelme

conda create --namelabelme python3 進入conda環境 source activate labelme 安裝labelme pip install labelme 遇到網絡問題 使用清華源 pip install labelme -i https://pypi.tuna.tsinghua.edu.cn/simple/ 輸入labelme 打開

Google的MLP-MIXer的復現(pytorch實現)

Google的MLP-MIXer的復現&#xff08;pytorch實現&#xff09; 該模型原論文實現用的jax框架實現&#xff0c;先貼出原論文的代碼實現&#xff1a; # Copyright 2024 Google LLC. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may …

GEC210編譯環境搭建

一、下載編譯工具鏈 下載&#xff1a;點擊跳轉 二、解壓到 /usr/local/arm 目錄 sudo mv gec210.zip /usr/local/arm cd /usr/local/arm sudo unzip gec210.zip 三、添加到環境變量 PATH/usr/local/arm/arm-cortex_a8-linux-gnueabi-4.7.3/bin:$PATH 四、測試驗證 在終端…

python數據分析-基于數據挖掘對APP評分的預測

前言 當我們談論關于APP用戶分析與電子商務之間的聯系時&#xff0c;機器學習在這兩個領域的應用變得至關重要。App用戶分析和電子商務之間存在著密切的關聯&#xff0c;因為用戶行為和偏好的深入理解對于提高用戶體驗、增加銷售以及優化產品功能至關重要。故本文基于K-近鄰模…

OFDM 802.11a的FPGA實現(二十)使用AXI-Stream FIFO進行跨時鐘(含代碼)

目錄 1.前言 2.AXI-Stream FIFO時序 3.AXI-Stream FIFO配置信息 4.時鐘控制模塊MMCM 5.ModelSim仿真 6.總結 1.前言 至此&#xff0c;通過前面的文章講解&#xff0c;對于OFDM 802.11a的發射基帶的一個完整的PPDU幀的所有處理已經全部完成&#xff0c;其結構如下圖所示&…

opencv-C++ VS2019配置安裝

最新opencv-c安裝及配置教程(VS2019 C & opencv4.4.0)_c opencv配置-CSDN博客

夜雨觸花感懷

夜雨觸花感懷 雨落有軌跡&#xff0c;業成無坦途。 ?雞毛飛虛空&#xff0c;尋德問心路。 ?恰如求耕耘&#xff0c;大話量寸土。 ?好吃品五味&#xff0c;難得評真俗。

CAN總線簡介

1. CAN總線概述 1.1 CAN定義與歷史背景 CAN&#xff0c;全稱為Controller Area Network&#xff0c;是一種基于消息廣播的串行通信協議。它最初由德國Bosch公司在1983年為汽車行業開發&#xff0c;目的是實現汽車內部電子控制單元&#xff08;ECUs&#xff09;之間的可靠通信。…

用Vuex存儲可配置下載的ip地址(用XML進行ajax請求配置文件)

1.在public文件夾下創建一個名為Configuration的文件在創建一個Configuration.txt里面就放IP地址&#xff08;這里的名字可以隨便命名一定性的被人解讀文件含義&#xff09; 例如&#xff1a; http://172.171.208.1:80032.在store文件夾中創建一個名為 ajaxModule.js 的 Vuex …

2. CSS選擇器與偽類

2.1 基本選擇器回顧 在開始介紹CSS3選擇器之前&#xff0c;我們先回顧一下CSS的基本選擇器。這些選擇器是所有CSS開發的基礎。 2.1.1 元素選擇器 元素選擇器用于選中指定類型的HTML元素。 /* 選中所有的<p>元素 */ p {color: blue; }2.1.2 類選擇器 類選擇器用于選中…

03自動輔助導航駕駛NOP其實就是NOA

蔚來NOP是什么意思&#xff1f;蔚來NOP是啥 蔚來NOP的意思就是NavigateonPilot智能輔助導航駕駛&#xff0c;也就是大家俗稱的高階輔助駕駛&#xff0c;在車主設定好導航路線&#xff0c;并且符合開啟NOP條件的前提下&#xff0c;蔚來NOP可以代替駕駛員完成從A點到B點的智能輔助…

深入理解數倉開發(二)數據技術篇之數據同步

1、數據同步 數據同步我們之前在數倉當中使用了多種工具&#xff0c;比如使用 Flume 將日志文件從服務器采集到 Kafka&#xff0c;再通過 Flume 將 Kafka 中的數據采集到 HDFS。使用 MaxWell 實時監聽 MySQL 的 binlog 日志&#xff0c;并將采集到的變更日志&#xff08;json 格…

【二叉樹】:LeetCode:100.相同的數(分治)

&#x1f381;個人主頁&#xff1a;我們的五年 &#x1f50d;系列專欄&#xff1a;初階初階結構刷題 &#x1f389;歡迎大家點贊&#x1f44d;評論&#x1f4dd;收藏?文章 1.問題描述&#xff1a; 2.問題分析&#xff1a; 二叉樹是區分結構的&#xff0c;即左右子樹是不一…

[JDK工具-6] jmap java內存映射工具

文章目錄 1. 介紹2. 主要選項3. 生成java堆轉儲快照 jmap -dump4. 顯示堆詳細信息 jmap -heap pid5. 顯示堆中對象統計信息 jmap -histo pid jmap(Memory Map for Java) 1. 介紹 位置&#xff1a;jdk\bin 作用&#xff1a; jdk安裝后會自帶一些小工具&#xff0c;jmap命令(Mem…

PySide6升級導致的Fatal Python error: could not initialize part 2問題及其解決方法

問題出現 把PySide6從6.6.1升級到6.7.1&#xff0c;結果運行程序的時候就報如下錯誤&#xff1a; Traceback (most recent call last): File "signature_bootstrap.py", line 77, in bootstrap File "signature_bootstrap.py", line 93, in find_inc…

Kafka SASL_SSL集群認證

背景 公司需要對kafka環境進行安全驗證,目前考慮到的方案有Kerberos和SSL和SASL_SSL,最終考慮到安全和功能的豐富度,我們最終選擇了SASL_SSL方案。處于知識積累的角度,記錄一下kafka SASL_SSL安裝部署的步驟。 機器規劃 目前測試環境公搭建了三臺kafka主機服務,現在將詳…

H3CNE-7-TCP和UDP協議

TCP和UDP協議 TCP&#xff1a;可靠傳輸&#xff0c;面向連接 -------- 速度慢&#xff0c;準確性高 UDP&#xff1a;不可靠傳輸&#xff0c;非面向連接 -------- 速度快&#xff0c;但準確性差 面向連接&#xff1a;如果某應用層協議的四層使用TCP端口&#xff0c;那么正式的…

智能家居完結 -- 整體設計

系統框圖 前情提要: 智能家居1 -- 實現語音模塊-CSDN博客 智能家居2 -- 實現網絡控制模塊-CSDN博客 智能家居3 - 實現煙霧報警模塊-CSDN博客 智能家居4 -- 添加接收消息的初步處理-CSDN博客 智能家居5 - 實現處理線程-CSDN博客 智能家居6 -- 配置 ini文件優化設備添加-CS…

【MySQL】聊聊count的相關操作

在平時的操作中&#xff0c;經常使用count進行操作&#xff0c;計算統計的數據。那么具體的原理是如何的&#xff1f;為什么有時候執行count很慢。 count的實現方式 select count(*) from student;對于MyISAM引擎來說&#xff0c;會把一個表的總行數存儲在磁盤上&#xff0c;…