Skip to content

Commit

Permalink
chore: adding minimal Cucumber test (refs #22 #73)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Jan 19, 2025
1 parent 28df938 commit 13343c3
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
17 changes: 15 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ buildscript {
dependencies {
classpath "se.bjurr.gradle.java-convention:se.bjurr.gradle.java-convention.gradle.plugin:0.+"
classpath "org.wiremock.tools.gradle:gradle-wiremock-extension-plugins:0.4.0"
classpath "se.thinkcode.cucumber-runner:se.thinkcode.cucumber-runner.gradle.plugin:0.0.11"
}
}

Expand All @@ -17,6 +18,7 @@ apply plugin: "se.bjurr.gradle.java-convention"
sourceCompatibility = 17
targetCompatibility = 17
apply plugin: "org.wiremock.tools.gradle.wiremock-extension-convention"
apply plugin: "se.thinkcode.cucumber-runner"


group 'org.wiremock.integrations'
Expand All @@ -35,18 +37,23 @@ dependencies {
implementation platform("org.eclipse.jetty:jetty-bom:12.0.15")
api "org.wiremock:wiremock-jetty12:${wiremockVersion}"

api "org.springframework.boot:spring-boot-test:3.3.4"
api "org.springframework.boot:spring-boot-test:3.4.1"
api "org.springframework:spring-test:6.1.13"
api "org.slf4j:slf4j-api:2.0.16"
api 'org.junit.jupiter:junit-jupiter-api:5.11.2'

testImplementation "org.wiremock:wiremock-jetty12:${wiremockVersion}"
testImplementation "org.springframework.boot:spring-boot-starter-test:3.3.4"
testImplementation "org.springframework.boot:spring-boot-starter-test:3.4.1"
testImplementation 'org.assertj:assertj-core:3.26.3'
testImplementation platform('org.junit:junit-bom:5.11.2')
testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'org.junit.platform:junit-platform-launcher'
testImplementation 'io.rest-assured:rest-assured:5.5.0'
testImplementation 'io.rest-assured:rest-assured:5.5.0'
testImplementation "org.springframework.boot:spring-boot-starter-web:3.4.1"
testImplementation 'io.cucumber:cucumber-java:7.20.1'
testImplementation 'io.cucumber:cucumber-spring:7.20.1'
testImplementation 'org.assertj:assertj-core:3.26.3'

constraints {
implementation('org.apache.commons:commons-compress:1.26.0') {
Expand All @@ -55,6 +62,12 @@ dependencies {
}
}

cucumber {
glue = 'classpath:usecases.cucumber'
featurePath = 'src/test/java/usecases/cucumber'
}
build.dependsOn tasks.cucumber

// Not using shadowJar, but currently not possible to disable in wiremock-extension-convention
shadowJar {
mergeServiceFiles()
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/usecases/cucumber/CucumberConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package usecases.cucumber;

public class CucumberConstants {
public static final String WIREMOCK_SERVER_NAME = "my-wiremock-server";
}
14 changes: 14 additions & 0 deletions src/test/java/usecases/cucumber/CucumberSpringConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package usecases.cucumber;

import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

import io.cucumber.spring.CucumberContextConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.wiremock.spring.ConfigureWireMock;
import org.wiremock.spring.EnableWireMock;

@CucumberContextConfiguration
@SpringBootTest(webEnvironment = RANDOM_PORT)
@EnableWireMock(
@ConfigureWireMock(name = CucumberConstants.WIREMOCK_SERVER_NAME, registerSpringBean = true))
public class CucumberSpringConfiguration {}
49 changes: 49 additions & 0 deletions src/test/java/usecases/cucumber/Steps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package usecases.cucumber;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import io.cucumber.java.Before;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.restassured.RestAssured;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Steps {

@Qualifier(CucumberConstants.WIREMOCK_SERVER_NAME)
@Autowired
private WireMockServer wireMockServer;

private ExtractableResponse<Response> actualResponse;

@Before
public void beforeEach() {
wireMockServer.resetAll();
RestAssured.baseURI = "http://localhost:" + wireMockServer.port();
}

@Given("^WireMock has endpint (.*)")
public void wireMockHasEndpoint(String endpoint) {
StubMapping okResponse =
WireMock.any(WireMock.urlEqualTo("/" + endpoint)).willReturn(WireMock.status(200)).build();
wireMockServer.addStubMapping(okResponse);
}

@When("^WireMock is invoked with (.*)")
public void wireMockIsInvokedWith(String endpoint) {
actualResponse = RestAssured.when().get("/" + endpoint).then().extract();
}

@Then("^it should respond (.*)")
public void isShouldResponsWith(int status) {
assertThat(actualResponse.statusCode()).isEqualTo(status);
}
}
8 changes: 8 additions & 0 deletions src/test/java/usecases/cucumber/test.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Feature: Test that features can be used

Scenario: Setup WireMock in Given and try it out in then
Given WireMock has endpint ping
When WireMock is invoked with ping
Then it should respond 200
When WireMock is invoked with pang
Then it should respond 404

0 comments on commit 13343c3

Please sign in to comment.