如何在Java@SpringBootTest中使用MockMvc和MockRestServiceServer
我正在尝试编写一个@SpringBootTest,它将发出REST请求和模拟响应。我之所以想这样做,是因为我的spring应用程序接收REST请求,作为响应,它使用REST从另一个源获取信息。我希望通过启动REST GET(MockMvc)来触发我的应用程序,但也希望模拟其他源的响应(MockRestServiceServer) 以下是测试的净化版本,删除了我的应用程序:如何在Java@SpringBootTest中使用MockMvc和MockRestServiceServer,java,spring,spring-boot,rest,mocking,Java,Spring,Spring Boot,Rest,Mocking,我正在尝试编写一个@SpringBootTest,它将发出REST请求和模拟响应。我之所以想这样做,是因为我的spring应用程序接收REST请求,作为响应,它使用REST从另一个源获取信息。我希望通过启动REST GET(MockMvc)来触发我的应用程序,但也希望模拟其他源的响应(MockRestServiceServer) 以下是测试的净化版本,删除了我的应用程序: import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api
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 final String URI = "/my-test-uri";
private static final String RESPONSE = "my-response";
@Autowired
private MockMvc mockMvc;
@Test
void testRest() throws Exception {
log.info("Initiating request for GET {}", URI);
mockMvc.perform(get(URI))
.andExpect(status().isOk())
.andExpect(content().string(containsString(RESPONSE)));
}
@TestConfiguration
static class TestConfig {
@Primary
@Bean
public RestTemplateBuilder restTemplateBuilder() {
RestTemplate restTemplate = new RestTemplate();
MockRestServiceServer server = MockRestServiceServer.createServer(restTemplate);
server.expect(requestTo(URI))
.andRespond(withSuccess(RESPONSE, MediaType.APPLICATION_JSON));
log.info("Mocking response to GET {}: {}", URI, RESPONSE);
RestTemplateBuilder mockBuilder = mock(RestTemplateBuilder.class);
when(mockBuilder.build()).thenReturn(restTemplate);
return mockBuilder;
}
}
}
测试失败,因为它没有收到TestConfig.restTemplateBuilder中定义的响应。我同时提供REST请求程序(testRest()方法)和REST响应程序(TestConfig.restTemplateBuilder)。我做错了什么
以下是失败的原因:
Status expected:<200> but was:<404>
Expected :200
Actual :404
<Click to see difference>
java.lang.AssertionError: Status expected:<200> but was:<404>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:59)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:122)
at org.springframework.test.web.servlet.result.StatusResultMatchers.lambda$matcher$9(StatusResultMatchers.java:627)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:196)
at com.mycompany.RestTest.testRest(RestTest.java:54)
...
at java.base/java.lang.Thread.run(Thread.java:830)
状态应为:但为:
预计:200
实际:404
java.lang.AssertionError:状态应为:但为:
位于org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:59)
位于org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:122)
位于org.springframework.test.web.servlet.result.StatusResultMatchers.lambda$matcher$9(StatusResultMatchers.java:627)
位于org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:196)
位于com.mycompany.restest.testRest(restest.java:54)
...
位于java.base/java.lang.Thread.run(Thread.java:830)
请发布您要完成的确切错误,提前感谢您的帮助