Java Testcontainers庫實現(xiàn)測試功能
1.Testcontainers介紹:
Testcontainers是一個Java庫,它支持JUnit測試,提供公共數(shù)據(jù)庫、SeleniumWeb瀏覽器或任何可以在Docker容器中運行的輕量級、一次性實例。
測試容器使以下類型的測試更加容易:
數(shù)據(jù)訪問層集成測試:
使用MySQL,PostgreSQL或Oracle數(shù)據(jù)庫的容器化實例測試您的數(shù)據(jù)訪問層代碼,但無需在開發(fā)人員的計算機上進行復雜的設置,并且測試將始終從已知的數(shù)據(jù)庫狀態(tài)開始,避免“垃圾”數(shù)據(jù)的干擾。也可以使用任何其他可以容器化的數(shù)據(jù)庫類型。
應用程序集成測試:
用于在具有相關性(例如數(shù)據(jù)庫,消息隊列或Web服務器)的短期測試模式下運行應用程序。
UI /驗收測試:
使用與Selenium兼容的容器化Web瀏覽器進行自動化UI測試。每個測試都可以獲取瀏覽器的新實例,而無需擔心瀏覽器狀態(tài),插件版本或瀏覽器自動升級。您將獲得每個測試會話或測試失敗的視頻記錄。
更多:
可以簽出各種貢獻的模塊,或使用 GenericContainer作為基礎創(chuàng)建自己的自定義容器類。
2.Testcontainers實踐示例:
Testcontainers提供了多種現(xiàn)成的與測試關聯(lián)的應用程序容器,如下圖:
在本文中,將演示集成postgresql容器和mockserver容器的測試。
Testcontainers必要條件:
1.Docker
2.支持的JVM測試框架:JUnit4,JUnit5,spock...
2.1 集成postgresql測試依賴:
<dependency> <groupId>org.testcontainers</groupId> <artifactId>testcontainers</artifactId> <version>1.12.5</version> <scope>test</scope> </dependency> <dependency> <groupId>org.testcontainers</groupId> <!--指定數(shù)據(jù)庫名稱,mysql,mariadb等等--> <artifactId>postgresql</artifactId> <version>1.12.5</version> <scope>test</scope> </dependency>
配置:
在項目的src/test/resources/application.properties文件中配置postgresql相關信息
#將驅動程序設置為org.testcontainers.jdbc.ContainerDatabaseDriver,它是一個Testcontainers JDBC代理驅動程序。初始化數(shù)據(jù)源時,此驅動程序將負責啟動所需的Docker容器。
spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver#將JDBC URL設置為JDBC:tc:<database image>:<version>:///以便Testcontainers知道要使用哪個數(shù)據(jù)庫。
#TC_INITSCRIPT=指定的數(shù)據(jù)庫初始化的腳本文件位置
spring.datasource.url=jdbc:tc:postgresql:9.6:///?TC_INITSCRIPT=file:src/main/resources/init_db.sql#將方言明確設置為數(shù)據(jù)庫的方言實現(xiàn),否則在啟動應用程序時會收到異常。當您在應用程序中使用JPA時(通過Spring Data JPA),此步驟是必需的
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect
測試示例:
為了在@DataJpaTest中使用TC,您需要確保使用了應用程序定義的(自動配置的)數(shù)據(jù)源。您可以通過使用@AutoConfigureTestDatabase注釋測試來輕松完成此操作,如下所示:
@RunWith(SpringJUnit4ClassRunner.class) @DataJpaTest @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) public class OwnerRepositoryTests { @Autowired private OwnerRepository ownerRepository; @Test void findAllReturnsJohnDoe() { // as defined in tc-initscript.sql var owners = ownerRepository.findAll(); assertThat(owners.size()).isOne(); assertThat(owners.get(0).getFirstName()).isEqualTo("John"); assertThat(owners.get(0).getLastName()).isEqualTo("Doe"); } }
以上測試將使用Testcontainers提供的postgresql容器進行測試,從而排除了外部環(huán)境對測試的干擾。
當需要用本地數(shù)據(jù)庫進行集成測試時,我們只要使用@SpringBootTest替換如上兩個注解即可:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc public class OwnerResourceTests { @Autowired WebApplicationContext wac; @Test void findAllReturnsJohnDoe() throws Exception { given() .webAppContextSetup(wac) .when() .get("/owners") .then() .status(HttpStatus.OK) .body( "_embedded.owners.firstName", containsInAnyOrder("John"), "_embedded.owners.lastName", containsInAnyOrder("Doe") ); } }
以上測試將使用真實運行環(huán)境的數(shù)據(jù)庫進行測試。
2.2 集成mockServer測試
Mock Server可用于通過將請求與用戶定義的期望進行匹配來模擬HTTP服務。
依賴:
<dependency> <groupId>org.testcontainers</groupId> <artifactId>mockserver</artifactId> <version>1.12.5</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mock-server</groupId> <artifactId>mockserver-netty</artifactId> <version>5.5.4</version> </dependency> <dependency> <groupId>org.mock-server</groupId> <artifactId>mockserver-client-java</artifactId> <version>5.5.4</version> </dependency>
測試示例:
//創(chuàng)建一個MockServer容器
@Rule
public MockServerContainer mockServer = new MockServerContainer();
以及使用Java MockServerClient設置簡單的期望。
new MockServerClient(mockServer.getContainerIpAddress(), mockServer.getServerPort()) .when(request() .withPath("/person") .withQueryStringParameter("name", "peter")) .respond(response() .withBody("Peter the person!")); //...當一個get請求至'/person?name=peter' 時會返回 "Peter the person!"
測試(使用restassured進行測試):
RestAssured.baseURI = "http://" + mockServer.getContainerIpAddress(); RestAssured.port = mockServer.getServerPort(); given().queryParam("name", "peter") .get("/person") .then() .statusCode(HttpStatus.OK.value()) .body(is("Peter the person!"));
完整代碼如下:
@RunWith(SpringJUnit4ClassRunner.class) public class OneTests { @Rule public MockServerContainer mockServer = new MockServerContainer(); @Test public void v() { RestAssured.baseURI = "http://" + mockServer.getContainerIpAddress(); RestAssured.port = mockServer.getServerPort(); new MockServerClient(mockServer.getContainerIpAddress(), mockServer.getServerPort()) .when(request() .withPath("/person") .withQueryStringParameter("name", "peter")) .respond(response() .withBody("Peter the person!")); given().queryParam("name", "peter") .get("/person") .then() .statusCode(HttpStatus.OK.value()) .body(is("Peter the person!")); } }
3.總結:
Testcontainers輕松的解決了集成測試時測試代碼與本地組件耦合,從而出現(xiàn)各種意外失敗的問題(比如本地數(shù)據(jù)庫中存在臟數(shù)據(jù)影響到了集成測試,多個集成測試同時運行時相互干擾導致測試結果意外失敗)。筆者之前專門為集成測試準備了一套數(shù)據(jù)庫,使數(shù)據(jù)和其他環(huán)境隔離掉,但還是會遇到多個集成測試一起跑相互干擾的問題,Testcontainers輕松的解決了筆者的問題。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java使用IntelliJ IDEA連接MySQL的詳細教程
這篇文章主要給大家介紹了關于Java使用IntelliJ IDEA連接MySQL的相關資料,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04Java反射 JavaBean對象自動生成插入,更新,刪除,查詢sql語句操作
這篇文章主要介紹了Java反射 JavaBean對象自動生成插入,更新,刪除,查詢sql語句操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08基于servlet實現(xiàn)統(tǒng)計網(wǎng)頁訪問次數(shù)
這篇文章主要為大家詳細介紹了基于servlet實現(xiàn)統(tǒng)計網(wǎng)頁訪問次數(shù),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02