Avatar
0
monkey Enlightened
monkey Enlightened
Spring Boot Integration test sử dụng có cả mockup lẫn service thật
Spring Boot Integration test sử dụng có cả mockup lẫn service thật theo các bước như sau:

  1. Khởi tạo project theo các bước này: https://stackask.com/question/huong-dan-su-dung-spring-boot-test-controller/
  2. Giả sử chúng ta có GoodbyeController thế này:

package com.example.spring_test.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.spring_test.service.GoodByeService;
import com.example.spring_test.service.HelloService;

import lombok.AllArgsConstructor;

@RestController
@AllArgsConstructor
public class GoodbyeController {

    private final HelloService helloService;
    private final GoodByeService goodByeService;

    @GetMapping("/hello-and-goodbye")
    public String hello(@RequestParam String who) {
        return helloService.hello(who) + " and " + goodByeService.goodBye(who);
    }
}

Và lớp GoodByeService thế này:

package com.example.spring_test.service;

import org.springframework.stereotype.Service;

@Service
public class GoodByeService {

    public String goodBye(String who) {
        return "Goodbye " + who + '!';
    }
}

Tuy nhiên chúng vì một lý do nào đó (có thê do lớp GoodbyeService cần gọi sang service thứ 3) nên chúng ta cần mockup nó, vậy hãy sang bước tiếp theo.

  • Answer
spring boot integration-test
Remain: 5
1 Answer
Avatar
monkey Enlightened
monkey Enlightened
Giả sử bạn có lớp startup thế này:

package com.example.spring_test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringJUnitTestStartup {

    public static void main(String[] args) {
        SpringApplication.run(SpringJUnitTestStartup.class);
    }
}

  1. Chúng ta cần tạo ra lớp BaseIntegrationTest thế này:

package com.example.spring_test.test;

import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import com.example.spring_test.SpringJUnitTestStartup;
import com.example.spring_test.service.GoodByeService;
import com.example.spring_test.test.BaseIntegrationTest.Initializer;
import com.example.spring_test.test.BaseIntegrationTest.SomeTestConfiguration;

@ExtendWith(SpringExtension.class)
@SpringBootTest(
    classes = SpringJUnitTestStartup.class,
    webEnvironment = WebEnvironment.DEFINED_PORT
)
@ContextConfiguration(
    initializers = Initializer.class,
    classes = SomeTestConfiguration.class
)
public class BaseIntegrationTest {

    @MockBean
    protected GoodByeService goodByeService;

    public static class Initializer
        implements ApplicationContextInitializer {

        @Override
        public void initialize(ConfigurableApplicationContext applicationContext) {
            // init something
        }
    }

    public static class SomeTestConfiguration {

        @Bean
        public Object someBean() {
            return "someBean";
        }
    }
}

  1. Cuối cùng chúng ta sẽ tạo ra lớp GoodbyeControllerTest thế này:

package com.example.spring_test.test;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.test.web.servlet.MockMvc;

@AutoConfigureMockMvc
public class GoodbyeControllerTest extends BaseIntegrationTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void helloAndGoodByeTest() throws Exception {
        // given
        final String who = "World";
        final String goodBye = "I will never say good bye";
        when(goodByeService.goodBye(who)).thenReturn(goodBye);

        // when
        // then
        final String expected = "Hello World! and " + goodBye;
        mockMvc.perform(get("/hello-and-goodbye?who=World"))
               .andExpect(status().isOk())
               .andExpect(content().string(expected));

        verify(goodByeService, times(1)).goodBye(who);
    }
}

Done!!!

  • 1
  • Reply
Mình thấy ide báo lỗi.

Mình sửa thêm như này thì được.

public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext>
 –  Phạm Tiến Đạt 1654210578000