Avatar
0
Phạm Tiến Đạt Beginner
[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.
  • Answer
unit test
Remain: 5
1 Answer
Avatar
monkey Enlightened
monkey Enlightened
  1. Add dependencies thế này:
    <code><span><span>&lt;<span>dependency</span>&gt;</span>
        <span>&lt;<span>groupId</span>&gt;</span>io.cucumber<span>&lt;/<span>groupId</span>&gt;</span>
        <span>&lt;<span>artifactId</span>&gt;</span>cucumber-java<span>&lt;/<span>artifactId</span>&gt;</span>
        <span>&lt;<span>version</span>&gt;</span>$</span><span>{cucumber.version}</span><span><span>&lt;/<span>version</span>&gt;</span>
        <span>&lt;<span>scope</span>&gt;</span>test<span>&lt;/<span>scope</span>&gt;</span>
    <span>&lt;/<span>dependency</span>&gt;</span>
    <span>&lt;<span>dependency</span>&gt;</span>
        <span>&lt;<span>groupId</span>&gt;</span>io.cucumber<span>&lt;/<span>groupId</span>&gt;</span>
        <span>&lt;<span>artifactId</span>&gt;</span>cucumber-spring<span>&lt;/<span>artifactId</span>&gt;</span>
        <span>&lt;<span>version</span>&gt;</span>$</span><span>{cucumber.version}</span><span><span>&lt;/<span>version</span>&gt;</span>
        <span>&lt;<span>scope</span>&gt;</span>test<span>&lt;/<span>scope</span>&gt;</span>
    <span>&lt;/<span>dependency</span>&gt;</span>
    <span>&lt;<span>dependency</span>&gt;</span>
        <span>&lt;<span>groupId</span>&gt;</span>io.cucumber<span>&lt;/<span>groupId</span>&gt;</span>
        <span>&lt;<span>artifactId</span>&gt;</span>cucumber-junit<span>&lt;/<span>artifactId</span>&gt;</span>
        <span>&lt;<span>version</span>&gt;</span>$</span><span>{cucumber.version}</span><span><span>&lt;/<span>version</span>&gt;</span>
        <span>&lt;<span>scope</span>&gt;</span>test<span>&lt;/<span>scope</span>&gt;</span>
    <span>&lt;/<span>dependency</span>&gt;</span></span>
    </code>
<code><span>&lt;<span>dependency</span>&gt;</span>
    <span>&lt;<span>groupId</span>&gt;</span>com.tvd12<span>&lt;/<span>groupId</span>&gt;</span>
    <span>&lt;<span>artifactId</span>&gt;</span>test-util<span>&lt;/<span>artifactId</span>&gt;</span>
    <span>&lt;<span>version</span>&gt;</span>1.1.6<span>&lt;/<span>version</span>&gt;</span>
    <span>&lt;<span>scope</span>&gt;</span>test<span>&lt;/<span>scope</span>&gt;</span>
<span>&lt;/<span>dependency</span>&gt;</span>
</code>
  1. 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
    
  1. 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 {}
    
  1. 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
  • Reply