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.

https://stackask.com/question/spring-boot-integration-test-su-dung-co-ca-mockup-lan-service-that/
  • Answer
unit test
Remain: 5
1 Answer
Avatar
monkey Beginner
monkey Beginner
  1. 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>

  1. Tạo file hello-and-goodbye.feature thế này trong foler src/test/resources:

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 CucumberGoodbyeControllerTest 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 CucumberGoodbyeControllerStepsTest 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);
    }
}

Run CucumberGoodbyeControllerTest file and done!!!

  • 0
  • Reply