小编Sha*_*aun的帖子

如何在Java中使用MockMvc和MockRestServiceServer @SpringBootTest

我正在尝试编写一个 @SpringBootTest ,它将发出 REST 请求和模拟响应。我想这样做是因为我的 Spring 应用程序接收 REST 请求,并作为响应,使用 REST 从另一个源获取信息。我想通过启动 REST GET (MockMvc) 来触发我的应用程序,但也想模拟其他源的响应 (MockRestServiceServer)。

这是测试的清理版本,删除了我的应用程序:

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.client.RestTemplate;

import static org.hamcrest.Matchers.containsString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
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;

@Slf4j
@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureMockMvc
class RestTest {
    private static …
Run Code Online (Sandbox Code Playgroud)

java rest spring mocking spring-boot

5
推荐指数
0
解决办法
1342
查看次数

标签 统计

java ×1

mocking ×1

rest ×1

spring ×1

spring-boot ×1