initial commit for wiremock
This commit is contained in:
		
							parent
							
								
									29deacf3b9
								
							
						
					
					
						commit
						e8bbe6eba6
					
				| @ -1,4 +1,5 @@ | |||||||
| <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"> | <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"> | ||||||
| 	<modelVersion>4.0.0</modelVersion> | 	<modelVersion>4.0.0</modelVersion> | ||||||
| 	<groupId>com.baeldung</groupId> | 	<groupId>com.baeldung</groupId> | ||||||
| 	<artifactId>rest-testing</artifactId> | 	<artifactId>rest-testing</artifactId> | ||||||
| @ -102,6 +103,12 @@ | |||||||
| 			<version>${mockito.version}</version> | 			<version>${mockito.version}</version> | ||||||
| 			<scope>test</scope> | 			<scope>test</scope> | ||||||
| 		</dependency> | 		</dependency> | ||||||
|  | 		<dependency> | ||||||
|  | 			<groupId>com.github.tomakehurst</groupId> | ||||||
|  | 			<artifactId>wiremock</artifactId> | ||||||
|  | 			<version>1.58</version> | ||||||
|  | 			<scope>test</scope> | ||||||
|  | 		</dependency> | ||||||
| 
 | 
 | ||||||
| 	</dependencies> | 	</dependencies> | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -0,0 +1,53 @@ | |||||||
|  | package com.baeldung.rest.wiremock.introduction; | ||||||
|  | 
 | ||||||
|  | import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||||||
|  | import static com.github.tomakehurst.wiremock.client.WireMock.configureFor; | ||||||
|  | import static com.github.tomakehurst.wiremock.client.WireMock.get; | ||||||
|  | import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; | ||||||
|  | import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; | ||||||
|  | import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; | ||||||
|  | import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching; | ||||||
|  | import static com.github.tomakehurst.wiremock.client.WireMock.verify; | ||||||
|  | import static org.junit.Assert.assertEquals; | ||||||
|  | 
 | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.io.InputStream; | ||||||
|  | import java.util.Scanner; | ||||||
|  | 
 | ||||||
|  | import org.apache.http.HttpResponse; | ||||||
|  | import org.apache.http.client.methods.HttpGet; | ||||||
|  | import org.apache.http.impl.client.CloseableHttpClient; | ||||||
|  | import org.apache.http.impl.client.HttpClients; | ||||||
|  | import org.junit.Rule; | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | import com.github.tomakehurst.wiremock.junit.WireMockRule; | ||||||
|  | 
 | ||||||
|  | public class JUnitManaged { | ||||||
|  |     @Rule | ||||||
|  |     public WireMockRule wireMockRule = new WireMockRule(); | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenJUnitManagedServer_whenStubbingURL_thenCorrect() throws IOException { | ||||||
|  |         configureFor("localhost", 8080); | ||||||
|  |         stubFor(get(urlPathMatching("/baeldung/.*")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody("\"testing-library\": \"WireMock\""))); | ||||||
|  | 
 | ||||||
|  |         CloseableHttpClient httpClient = HttpClients.createDefault(); | ||||||
|  |         HttpGet request = new HttpGet("http://localhost:8080/baeldung/wiremock"); | ||||||
|  |         HttpResponse httpResponse = httpClient.execute(request); | ||||||
|  |         String stringResponse = convertResponseToString(httpResponse); | ||||||
|  | 
 | ||||||
|  |         verify(getRequestedFor(urlEqualTo("/baeldung/wiremock"))); | ||||||
|  |         assertEquals(200, httpResponse.getStatusLine().getStatusCode()); | ||||||
|  |         assertEquals("application/json", httpResponse.getFirstHeader("Content-Type").getValue()); | ||||||
|  |         assertEquals("\"testing-library\": \"WireMock\"", stringResponse); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private String convertResponseToString(HttpResponse response) throws IOException { | ||||||
|  |         InputStream responseStream = response.getEntity().getContent(); | ||||||
|  |         Scanner scanner = new Scanner(responseStream, "UTF-8"); | ||||||
|  |         String stringResponse = scanner.useDelimiter("\\Z").next(); | ||||||
|  |         scanner.close(); | ||||||
|  |         return stringResponse; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,52 @@ | |||||||
|  | package com.baeldung.rest.wiremock.introduction; | ||||||
|  | 
 | ||||||
|  | import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||||||
|  | import static com.github.tomakehurst.wiremock.client.WireMock.configureFor; | ||||||
|  | import static com.github.tomakehurst.wiremock.client.WireMock.get; | ||||||
|  | import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; | ||||||
|  | import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; | ||||||
|  | import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; | ||||||
|  | import static com.github.tomakehurst.wiremock.client.WireMock.verify; | ||||||
|  | import static org.junit.Assert.assertEquals; | ||||||
|  | 
 | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.io.InputStream; | ||||||
|  | import java.util.Scanner; | ||||||
|  | 
 | ||||||
|  | import org.apache.http.HttpResponse; | ||||||
|  | import org.apache.http.client.methods.HttpGet; | ||||||
|  | import org.apache.http.impl.client.CloseableHttpClient; | ||||||
|  | import org.apache.http.impl.client.HttpClients; | ||||||
|  | import org.junit.Test; | ||||||
|  | 
 | ||||||
|  | import com.github.tomakehurst.wiremock.WireMockServer; | ||||||
|  | 
 | ||||||
|  | public class ProgrammaticallyManaged { | ||||||
|  |     WireMockServer wireMockServer = new WireMockServer(); | ||||||
|  |     CloseableHttpClient httpClient = HttpClients.createDefault(); | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void givenProgrammaticallyManagedServer_whenUsingSimpleStubbing_thenCorrect() throws IOException { | ||||||
|  |         wireMockServer.start(); | ||||||
|  | 
 | ||||||
|  |         configureFor("localhost", 8080); | ||||||
|  |         stubFor(get(urlEqualTo("/baeldung")).willReturn(aResponse().withBody("Welcome to Baeldung!"))); | ||||||
|  | 
 | ||||||
|  |         HttpGet request = new HttpGet("http://localhost:8080/baeldung"); | ||||||
|  |         HttpResponse httpResponse = httpClient.execute(request); | ||||||
|  |         String stringResponse = convertResponseToString(httpResponse); | ||||||
|  | 
 | ||||||
|  |         verify(getRequestedFor(urlEqualTo("/baeldung"))); | ||||||
|  |         assertEquals("Welcome to Baeldung!", stringResponse); | ||||||
|  | 
 | ||||||
|  |         wireMockServer.stop(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private String convertResponseToString(HttpResponse response) throws IOException { | ||||||
|  |         InputStream responseStream = response.getEntity().getContent(); | ||||||
|  |         Scanner scanner = new Scanner(responseStream, "UTF-8"); | ||||||
|  |         String stringResponse = scanner.useDelimiter("\\Z").next(); | ||||||
|  |         scanner.close(); | ||||||
|  |         return stringResponse; | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user