[Unit Test] Có cách nào cấu hình sử dụng @MockBean với Spring boot test and Cucumber?
Xin chào,
Mọi người cho mình hỏi có cách nào cấu hình sử dụng @MockBean với Spring boot test and Cucumber?
Dựa trên câu hỏi thread này mình setup chạy bình thường. Nhưng nếu mình apply thêm cả cucumber vào thì @MockBean không được.
unit test
Remain: 5
1 Answer
monkey
Enlightened
monkey
Enlightened
- Add dependencies thế này:
<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>- Tạo file <code>hello-and-goodbye.feature</code> thế này trong foler <code>src/test/resources</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
- Tạo file <code>CucumberGoodbyeControllerTest</code> thế này:
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 {}
- Tạo file <code>CucumberGoodbyeControllerStepsTest</code> thế này:
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 file and done!!!
-
0