The steps in this library enable Mock Creation in test using Cucumber
MockConfig
.mockOptions()
.clearServices();
.addService("default",mockServer);
The following properties can be configured in the file cucumbersome.properties
mock.services=test1,test2
mock.services.test1.port=8080
mock.services.test1.host=myservice
mock.services.test2.port=8080
mock.services.test2.host=myservice2
Where the mock.services
is a list of services we want to mock, separated by comma.
mock.services=<name>
If no services are specified a single service called "default" will be created.
Per each services we can then specify the properties host and port:
mock.services.<name>.port=8081
mock.services.<name>.host=localhost
mock.services.<name>.fixedPort=9090
if host and port are not specified for a service the default will be used:
Property Name | Default value |
---|---|
port | random available port |
host | |
fixedPort | NONE |
using default parameters.
Please note that the list below is porvided as exemple and may not include all the steps available in the library.
Given the mock with path "/" and method "GET" is added to service "test"
Note: support regex in path and method
And the mock receives a request with body matching json
"""
{
"test": "test"
}
"""
And the mock receives a request with body matching the json path "$..store.book.author"
And the mock receives a request with body containing the string "test"
And the mock receives a request with body equal to string
"""
ExactMatch
"""
And the mock receives a request with body matching regex "Exact.*"
And the mock receives a request with xml body:
"""
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
"""
And the mock receives a request with body matching the xpath "/note/to"
And the mock receives a request with header "MyHeader"
Given the mock receives request with header "MyHeader" with value "MyHeaderValue"
Given the http request headers are:
| myHeader | myValue |
| myHeader2 | myValue2 |
Note: support regex in name and value
Given the mock receives request with query parameter "queryParam" with value "queryParamValue"
And the mock receives a request with query parameters:
| query | myQuery |
| query2 | myQuery2|
Note: support regex in name and value
And the mock responds with status code 200
And the mock responds with status code "200" exactly 3 times
And the mock response will have header "Content-Type" with value "application/json"
And the mock response will have a delay of 5s
Add dependency to your pom.xml
<dependency>
<groupId>com.mariocairone.cucumbersome</groupId>
<artifactId>mock</artifactId>
<version>{{version}}</version>
<scope>test</scope>
</dependency>
Create the test class with the package glue
import org.junit.ClassRule;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import static com.mariocairone.cucumbersome.steps.mock.config.MockConfig.mockOptions;
import com.mariocairone.cucumbersome.steps.mock.container.MockServerContainer;
@RunWith(Cucumber.class)
@CucumberOptions(plugin = { "pretty", "html:target/cucumber",
"json:target/cucumber/cucumber.json",
"junit:target/cucumber/cucumber.xml"},
glue = {"com.mariocairone.cucumbersome.steps"},
features = "classpath:features/mockserver",
strict = true)
public class CucumbersomeMockIT {
@ClassRule
public static MockServerContainer mockServer = new MockServerContainer();
@BeforeClass
public static void setup() {
mockOptions()
.addService("default",mockServer);
}
}
Note: The library include a MockServiceContainer
class that can be used instead of the official MockServiceContainer
. The implementation provided extend the official libriry adding the ability to use a fixed exposed port on the host machine to simplify local debug.
Also please be sure to modify the features attribute to match your project requirement