{{announcement.body}}
{{announcement.title}}

Kotlin Wiremock

DZone 's Guide to

Kotlin Wiremock

Here's a handy Kotline library for stubbing WireMock expectations.

· Java Zone ·
Free Resource

WireMock is a popular library for stubbing web services. It runs an HTTP server that acts as an actual web service. We just set up expectations and run the server.

In this tutorial, we will present the kotlin-wiremock library that provides a nice domain-specific language for stubbing WireMock expectations.

Getting Started

The first step is adding dependencies. 

Maven

XML
 




xxxxxxxxxx
1


 
1
<dependency>
2
    <groupId>com.marcinziolo</groupId>
3
    <artifactId>kotlin-wiremock</artifactId>
4
    <version>1.0.0</version>
5
    <scope>test</scope>
6
</dependency>


Gradle

Kotlin
 




xxxxxxxxxx
1


 
1
testImplementation("com.marcinziolo:kotlin-wiremock:version")



And now we can use kotlin-wiremock. Let's see the JUnit 4 example.

Kotlin
 




xxxxxxxxxx
1
49


 
1
import com.github.tomakehurst.wiremock.junit.WireMockRule
2
import io.restassured.module.kotlin.extensions.Then
3
import io.restassured.module.kotlin.extensions.When
4
import org.hamcrest.Matchers.equalTo
5
import org.junit.Rule
6
import org.junit.Test
7
import java.net.ServerSocket
8
 
          
9
class JUnit4ExampleTest {
10
 
          
11
    val port = findRandomPort()
12
 
          
13
    val url = "http://localhost:$port"
14
 
          
15
    @Rule
16
    @JvmField
17
    var wiremock: WireMockRule = WireMockRule(port)
18
 
          
19
    @Test
20
    fun `url matching`() {
21
        wiremock.get {
22
            url like  "/users/.*"
23
        } returns  {
24
            header = "Content-Type" to "application/json"
25
            statusCode = 200
26
            body = """
27
            {
28
              "id": 1,
29
              "name": "Bob"
30
            }
31
            """
32
        }
33
        assertBobSuccess()
34
    }
35
 
          
36
    private fun assertBobSuccess() {
37
        When {
38
            get("$url/users/1")
39
        } Then {
40
            statusCode(200)
41
            body("id", equalTo(1))
42
            body("name", equalTo("Bob"))
43
        }
44
    }
45
 
          
46
    fun findRandomPort(): Int {
47
        ServerSocket(0).use { socket -> return socket.localPort }
48
    }
49
}


Features

One of the basic features is url matching.  Let's see an example: when calling a server with URL equal /users/1, the server returns a JSON body with Bob's response.

Kotlin
 




x





1
wiremock.get {
2
  url equalTo "/users/1"
3
} returns {
4
  header = "Content-Type" to "application/json"
5
  statusCode = 200
6
  body = """{"id": 1,"name": "Bob"}"""
7
}



We can also use regular expressions for matching url. Notice that we can use method returnsJson so we don't need to specify the Conte-Type header manually.

Kotlin
 




xxxxxxxxxx
1


1
wiremock.get {
2
  url like "/users/.*"
3
} returnsJson {
4
  body = """{"id": 1,"name": "Bob"}"""
5
}



Let's see how we can stub the post request. We return bobResponse only when request JSON body fulfills the following criteria:

  • id key equalTo 1
  • isAdmin key equalTo true
  • points equalTo 3.0
  • data object contains name key which starts with Bob string.

As you can see we have a full spectrum of solutions for mathing the request body. Note that the library gives us the possibility to check value types(string, integer, float, boolean). 

Kotlin
 




xxxxxxxxxx
1


1
val bobResponse: SpecifyResponse = { body = """{"id": 1,"name": "Bob"}""" }
2
wiremock.post {
3
  url equalTo "/users"
4
    body contains "id" equalTo 1
5
    body contains "isAdmin" equalTo true
6
    body contains "points" equalTo 3.0
7
    body contains "data.name" like "Bob.*"
8
} returnsJson bobResponse



We can also check headers and query params:

Kotlin
 




xxxxxxxxxx
1


1
wiremock.post {
2
    headers contains "User-Agent" like "Firefox.*"
3
    queryParams contains "page" equalTo "1" 
4
}



Look how priorities work, when we send Authorization header then the response is 200 otherwise the server replies with 403.

Kotlin
 




xxxxxxxxxx
1
14


 
1
wiremock.post {
2
    url equalTo "/test"
3
    priority = 2
4
} returnsJson  {
5
    statusCode = 403
6
}
7
 
          
8
wiremock.post {
9
    url equalTo "/test"
10
    headers contains "Authorization"
11
    priority = 1
12
} returnsJson  {
13
    statusCode = 200
14
}



We can even simulate delays.

Kotlin
 




xxxxxxxxxx
1


 
1
wiremock.post {
2
    url equalTo "/users"
3
} returnsJson {
4
    delay fixedMs 100
5
    //or gaussian distribution
6
    delay medianMs 100 sigma 0.1
7
}    



We can come up with more sophisticated scenarios. The first request returns bobResponse but we set flag toState to Alice. So the second response returns aliceResponse,

Java
 




xxxxxxxxxx
1
12


1
wiremock.post {
2
     url equalTo "/users"
3
 } returnsJson bobResponse and {
4
     toState = "Alice"
5
 }
6
 
          
7
 wiremock.post {
8
     url equalTo "/users"
9
     whenState = "Alice"
10
 } returnsJson aliceResponse and {
11
     clearState = true
12
 }



Summary

All features presented in this tutorial can be achieved via wireMock API.

Java
 




x
10
9


1
stubFor(any(urlPathEqualTo("/everything"))
2
  .withHeader("Accept", containing("json"))
3
  .withQueryParam("search_term", equalTo("WireMock"))
4
  .withRequestBody(matchingJsonPath("$.things[?(@.name =~ /Required.*/i')]"))
5
  .willReturn(aResponse()
6
              .withHeader("Content-Type", "text/plain")
7
              .withBody("Hello world!")));



But doesn't it look way better in Kotlin Wiremock DSL?

Kotlin
 




xxxxxxxxxx
1


 
1
wiremock.get {
2
  url equalTo "/everything"
3
  header contains "Accept" contains "json"
4
  queryParams contains "search_term" equalTo "WireMock"
5
  body contains "things.name" like "Required.*"
6
} returns {
7
  header = "Content-Type" to "text/plain"
8
  body = "Hello world"
9
}



More examples can be found here:

Topics:
dsl, java, kotlin, stubbing, wiremock

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}