StackAsk is a community where developers ask questions, share answers and learn from each others real-world experience, so every solved problem helps the next person who runs into it.
<code><span><span><<span>dependency</span>></span> <span><<span>groupId</span>></span>io.cucumber<span></<span>groupId</span>></span> <span><<span>artifactId</span>></span>cucumber-java<span></<span>artifactId</span>></span> <span><<span>version</span>></span>$</span><span>{cucumber.version}</span><span><span></<span>version</span>></span> <span><<span>scope</span>></span>test<span></<span>scope</span>></span> <span></<span>dependency</span>></span> <span><<span>dependency</span>></span> <span><<span>groupId</span>></span>io.cucumber<span></<span>groupId</span>></span> <span><<span>artifactId</span>></span>cucumber-spring<span></<span>artifactId</span>></span> <span><<span>version</span>></span>$</span><span>{cucumber.version}</span><span><span></<span>version</span>></span> <span><<span>scope</span>></span>test<span></<span>scope</span>></span> <span></<span>dependency</span>></span> <span><<span>dependency</span>></span> <span><<span>groupId</span>></span>io.cucumber<span></<span>groupId</span>></span> <span><<span>artifactId</span>></span>cucumber-junit<span></<span>artifactId</span>></span> <span><<span>version</span>></span>$</span><span>{cucumber.version}</span><span><span></<span>version</span>></span> <span><<span>scope</span>></span>test<span></<span>scope</span>></span> <span></<span>dependency</span>></span></span> </code>
<code><span><<span>dependency</span>></span> <span><<span>groupId</span>></span>com.tvd12<span></<span>groupId</span>></span> <span><<span>artifactId</span>></span>test-util<span></<span>artifactId</span>></span> <span><<span>version</span>></span>1.1.6<span></<span>version</span>></span> <span><<span>scope</span>></span>test<span></<span>scope</span>></span> <span></<span>dependency</span>></span> </code>
Feature: Say hello and good bye Scenario: client makes call to GET /hello-and-goodbye When the client calls /hello-and-goodbye Then the client receives status code of 200 And the client receives server response Hello World! and I will never say good bye
package com.example.spring_test.test; import org.junit.runner.RunWith; import io.cucumber.junit.Cucumber; import io.cucumber.junit.CucumberOptions; @RunWith(Cucumber.class) @CucumberOptions(features = "src/test/resources") public class CucumberGoodbyeControllerTest {}
package com.example.spring_test.test; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.web.client.RestTemplate; import com.example.spring_test.SpringJUnitTestStartup; import com.example.spring_test.controller.GoodbyeController; import com.example.spring_test.service.GoodByeService; import com.tvd12.test.reflect.FieldUtil; import io.cucumber.java.en.And; import io.cucumber.java.en.Then; import io.cucumber.java.en.When; import io.cucumber.spring.CucumberContextConfiguration; @ExtendWith(SpringExtension.class) @SpringBootTest( classes = SpringJUnitTestStartup.class, webEnvironment = WebEnvironment.DEFINED_PORT ) @CucumberContextConfiguration public class CucumberGoodbyeControllerStepsTest { @Autowired private GoodbyeController goodbyeController; private ResponseEntity responseEntity; private final RestTemplate restTemplate = new RestTemplate(); @When("^the client calls /hello-and-goodbye") public void the_client_issues_GET_hello_and_goodbye() throws Throwable{ final String who = "World"; final String goodBye = "I will never say good bye"; final GoodByeService goodByeService = mock(GoodByeService.class); when(goodByeService.goodBye(who)).thenReturn(goodBye); FieldUtil.setFieldValue(goodbyeController, "goodByeService", goodByeService); executeGet("http://localhost:8080/hello-and-goodbye?who=World"); } private void executeGet(String url) throws IOException { final Map headers = new HashMap(); headers.put("Accept", "application/json"); responseEntity = restTemplate.getForEntity(url, String.class); } @Then("^the client receives status code of (\d+)$") public void the_client_receives_status_code_of(int statusCode) { assert responseEntity.getStatusCode().value() == statusCode; } @And("^the client receives server response (.+)$") public void the_client_receives_server_response_body(String response) { assert responseEntity.getBody().equals(response); } }
CucumberGoodbyeControllerTest
2.2K Point(s)
1.2K Point(s)
313 Point(s)
178 Point(s)
138 Point(s)
Your account is signed in on the device(s) below. Sign out of at least one of them to continue signing in on this device.
Your account is signed in on the device(s) below. Sign out directly on one of those devices, or wait until the sign-in again time shown below.
If you cant access those devices, please contact the administrator to sign them out for you.
Input your email to receive reset password link, or if you remember your password, you can click