1.Testcontainers介紹:
Testcontainers是一個Java庫,它支持JUnit測試,提供公共數(shù)據(jù)庫、SeleniumWeb瀏覽器或任何可以在Docker容器中運行的輕量級、一次性實例。
測試容器使以下類型的測試更加容易:
數(shù)據(jù)訪問層集成測試:
使用MySQL,PostgreSQL或Oracle數(shù)據(jù)庫的容器化實例測試您的數(shù)據(jù)訪問層代碼,但無需在開發(fā)人員的計算機(jī)上進(jìn)行復(fù)雜的設(shè)置,并且測試將始終從已知的數(shù)據(jù)庫狀態(tài)開始,避免“垃圾”數(shù)據(jù)的干擾。也可以使用任何其他可以容器化的數(shù)據(jù)庫類型。
應(yīng)用程序集成測試:
用于在具有相關(guān)性(例如數(shù)據(jù)庫,消息隊列或Web服務(wù)器)的短期測試模式下運行應(yīng)用程序。
UI /驗收測試:
使用與Selenium兼容的容器化Web瀏覽器進(jìn)行自動化UI測試。每個測試都可以獲取瀏覽器的新實例,而無需擔(dān)心瀏覽器狀態(tài),插件版本或瀏覽器自動升級。您將獲得每個測試會話或測試失敗的視頻記錄。
更多:
可以簽出各種貢獻(xiàn)的模塊,或使用 GenericContainer作為基礎(chǔ)創(chuàng)建自己的自定義容器類。
2.Testcontainers實踐示例:
Testcontainers提供了多種現(xiàn)成的與測試關(guān)聯(lián)的應(yīng)用程序容器,如下圖:
在本文中,將演示集成postgresql容器和mockserver容器的測試。
Testcontainers必要條件:
1.Docker
2.支持的JVM測試框架:JUnit4,JUnit5,spock...
2.1 集成postgresql測試依賴:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
< 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相關(guān)信息
#將驅(qū)動程序設(shè)置為org.testcontainers.jdbc.ContainerDatabaseDriver,它是一個Testcontainers JDBC代理驅(qū)動程序。初始化數(shù)據(jù)源時,此驅(qū)動程序?qū)⒇?fù)責(zé)啟動所需的Docker容器。
spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver#將JDBC URL設(shè)置為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è)置為數(shù)據(jù)庫的方言實現(xiàn),否則在啟動應(yīng)用程序時會收到異常。當(dāng)您在應(yīng)用程序中使用JPA時(通過Spring Data JPA),此步驟是必需的
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect
測試示例:
為了在@DataJpaTest中使用TC,您需要確保使用了應(yīng)用程序定義的(自動配置的)數(shù)據(jù)源。您可以通過使用@AutoConfigureTestDatabase注釋測試來輕松完成此操作,如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@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容器進(jìn)行測試,從而排除了外部環(huán)境對測試的干擾。
當(dāng)需要用本地數(shù)據(jù)庫進(jìn)行集成測試時,我們只要使用@SpringBootTest替換如上兩個注解即可:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@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ù)庫進(jìn)行測試。
2.2 集成mockServer測試
Mock Server可用于通過將請求與用戶定義的期望進(jìn)行匹配來模擬HTTP服務(wù)。
依賴:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< 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設(shè)置簡單的期望。
1
2
3
4
5
6
7
|
new MockServerClient(mockServer.getContainerIpAddress(), mockServer.getServerPort()) .when(request() .withPath( "/person" ) .withQueryStringParameter( "name" , "peter" )) .respond(response() .withBody( "Peter the person!" )); //...當(dāng)一個get請求至'/person?name=peter' 時會返回 "Peter the person!" |
測試(使用restassured進(jìn)行測試):
1
2
3
4
5
6
7
|
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!" )); |
完整代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
@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.總結(jié):
Testcontainers輕松的解決了集成測試時測試代碼與本地組件耦合,從而出現(xiàn)各種意外失敗的問題(比如本地數(shù)據(jù)庫中存在臟數(shù)據(jù)影響到了集成測試,多個集成測試同時運行時相互干擾導(dǎo)致測試結(jié)果意外失敗)。筆者之前專門為集成測試準(zhǔn)備了一套數(shù)據(jù)庫,使數(shù)據(jù)和其他環(huán)境隔離掉,但還是會遇到多個集成測試一起跑相互干擾的問題,Testcontainers輕松的解決了筆者的問題。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/dongxishaonian/p/12653200.html