本文整理了Java中org.springframework.test.web.client.MockRestServiceServer
类的一些代码示例,展示了MockRestServiceServer
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MockRestServiceServer
类的具体详情如下:
包路径:org.springframework.test.web.client.MockRestServiceServer
类名称:MockRestServiceServer
[英]Main entry point for client-side REST testing. Used for tests that involve direct or indirect use of the RestTemplate. Provides a way to set up expected requests that will be performed through the RestTemplate as well as mock responses to send back thus removing the need for an actual server.
Below is an example that assumes static imports from MockRestRequestMatchers, MockRestResponseCreators, and ExpectedCount:
RestTemplate restTemplate = new RestTemplate()
MockRestServiceServer server = MockRestServiceServer.bindTo(restTemplate).build();
server.expect(manyTimes(), requestTo("/hotels/42")).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess("{ \"id\" : \"42\", \"name\" : \"Holiday Inn\"}", MediaType.APPLICATION_JSON));
Hotel hotel = restTemplate.getForObject("/hotels/{id}", Hotel.class, 42);
// Use the hotel instance...
// Verify all expectations met
server.verify();
Note that as an alternative to the above you can also set the MockMvcClientHttpRequestFactory on a RestTemplate which allows executing requests against an instance of org.springframework.test.web.servlet.MockMvc.
[中]客户端REST测试的主要入口点。用于涉及直接或间接使用REST模板的测试。提供了一种设置预期请求的方法,这些请求将通过RestTemplate执行,并提供模拟响应以发送回,从而消除了对实际服务器的需求。
下面是一个假设从MockRestRequestMatchers、MockResponseSecreator和ExpectedCount进行静态导入的示例:
RestTemplate restTemplate = new RestTemplate()
MockRestServiceServer server = MockRestServiceServer.bindTo(restTemplate).build();
server.expect(manyTimes(), requestTo("/hotels/42")).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess("{ \"id\" : \"42\", \"name\" : \"Holiday Inn\"}", MediaType.APPLICATION_JSON));
Hotel hotel = restTemplate.getForObject("/hotels/{id}", Hotel.class, 42);
// Use the hotel instance...
// Verify all expectations met
server.verify();
注意,作为上述方法的替代方法,您还可以在RestTemplate上设置MockMvcClientHttpRequestFactory,该模板允许对org实例执行请求。springframework。测验网状物servlet。MockMvc。
代码示例来源:origin: spring-projects/spring-framework
@Test
public void performGetWithResponseBodyFromFile() {
Resource responseBody = new ClassPathResource("ludwig.json", this.getClass());
this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
@SuppressWarnings("unused")
Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
// hotel.getId() == 42
// hotel.getName().equals("Holiday Inn")
this.mockServer.verify();
}
代码示例来源:origin: spring-projects/spring-framework
@Before
public void setup() {
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new StringHttpMessageConverter());
converters.add(new MappingJackson2HttpMessageConverter());
this.restTemplate = new RestTemplate();
this.restTemplate.setMessageConverters(converters);
this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void ignoreExpectOrder() {
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate)
.ignoreExpectOrder(true).build();
server.expect(requestTo("/foo")).andRespond(withSuccess());
server.expect(requestTo("/bar")).andRespond(withSuccess());
this.restTemplate.getForObject("/bar", Void.class);
this.restTemplate.getForObject("/foo", Void.class);
server.verify();
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void resetAndReuseServer() {
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate).build();
server.expect(requestTo("/foo")).andRespond(withSuccess());
this.restTemplate.getForObject("/foo", Void.class);
server.verify();
server.reset();
server.expect(requestTo("/bar")).andRespond(withSuccess());
this.restTemplate.getForObject("/bar", Void.class);
server.verify();
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Set up an expectation for a single HTTP request. The returned
* {@link ResponseActions} can be used to set up further expectations as
* well as to define the response.
* <p>This method may be invoked any number times before starting to make
* request through the underlying {@code RestTemplate} in order to set up
* all expected requests.
* @param matcher request matcher
* @return a representation of the expectation
*/
public ResponseActions expect(RequestMatcher matcher) {
return expect(ExpectedCount.once(), matcher);
}
代码示例来源:origin: spring-projects/spring-integration
@Test
public void testWithResponseTypeExpressionSetAsClass() throws Exception {
this.mockServer = MockRestServiceServer.createServer(this.restTemplateWithConverters);
this.mockServer.expect(requestTo("/testApps/outboundResponse"))
.andExpect(method(HttpMethod.POST))
.andRespond(withSuccess(HttpMethod.POST.name(), MediaType.TEXT_PLAIN));
this.resTypeExpressionSetSerializationChannel.send(new GenericMessage<Class<?>>(String.class));
Message<?> message = this.replyChannel.receive(5000);
assertNotNull(message);
assertTrue(message.getPayload() instanceof String);
this.mockServer.verify();
}
代码示例来源:origin: cloudfoundry/uaa
@Before
public void setUp(){
notificationsTemplate = new RestTemplate();
mockNotificationsServer = MockRestServiceServer.createServer(notificationsTemplate);
response.put("resources", resources);
mockNotificationsServer.expect(requestTo("http://notifications.example.com/registration"))
.andExpect(method(PUT))
.andExpect(jsonPath("$.source_description").value("CF_Identity"))
代码示例来源:origin: spring-projects/spring-framework
private void executeAndVerify() throws URISyntaxException {
this.restTemplate.getForObject(new URI("/person/1"), Person.class);
this.mockServer.verify();
}
代码示例来源:origin: spring-projects/spring-framework
@Test(expected = AssertionError.class)
public void exactExpectOrder() {
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate)
.ignoreExpectOrder(false).build();
server.expect(requestTo("/foo")).andRespond(withSuccess());
server.expect(requestTo("/bar")).andRespond(withSuccess());
this.restTemplate.getForObject("/bar", Void.class);
}
代码示例来源:origin: spring-projects/spring-framework
/**
* A shortcut for {@code bindTo(restTemplate).build()}.
* @param restTemplate the RestTemplate to set up for mock testing
* @return the mock server
*/
public static MockRestServiceServer createServer(RestTemplate restTemplate) {
return bindTo(restTemplate).build();
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public MockRestServiceServer build(RequestExpectationManager manager) {
MockRestServiceServer server = new MockRestServiceServer(manager);
MockClientHttpRequestFactory factory = server.new MockClientHttpRequestFactory();
if (this.restTemplate != null) {
if (this.bufferContent) {
this.restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(factory));
}
else {
this.restTemplate.setRequestFactory(factory);
}
}
if (this.asyncRestTemplate != null) {
this.asyncRestTemplate.setAsyncRequestFactory(factory);
}
return server;
}
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void contentTypeNoMatch() throws Exception {
this.mockServer.expect(content().contentType("application/json;charset=UTF-8")).andRespond(withSuccess());
try {
executeAndVerify("foo");
}
catch (AssertionError error) {
String message = error.getMessage();
assertTrue(message, message.startsWith("Content type expected:<application/json;charset=UTF-8>"));
}
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void resetAndReuseServerWithUnorderedExpectationManager() {
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate)
.ignoreExpectOrder(true).build();
server.expect(requestTo("/foo")).andRespond(withSuccess());
this.restTemplate.getForObject("/foo", Void.class);
server.verify();
server.reset();
server.expect(requestTo("/foo")).andRespond(withSuccess());
server.expect(requestTo("/bar")).andRespond(withSuccess());
this.restTemplate.getForObject("/bar", Void.class);
this.restTemplate.getForObject("/foo", Void.class);
server.verify();
}
代码示例来源:origin: spring-projects/spring-framework
private void executeAndVerify() throws URISyntaxException {
this.restTemplate.put(new URI("/composers"), people);
this.mockServer.verify();
}
代码示例来源:origin: spring-projects/spring-framework
/**
* A shortcut for {@code bindTo(asyncRestTemplate).build()}.
* @param asyncRestTemplate the AsyncRestTemplate to set up for mock testing
* @return the created mock server
* @deprecated see deprecation notice on
* {@link org.springframework.web.client.AsyncRestTemplate} itself
*/
@Deprecated
public static MockRestServiceServer createServer(org.springframework.web.client.AsyncRestTemplate asyncRestTemplate) {
return bindTo(asyncRestTemplate).build();
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void performGet() {
String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
@SuppressWarnings("unused")
Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
// We are only validating the request. The response is mocked out.
// hotel.getId() == 42
// hotel.getName().equals("Holiday Inn")
this.mockServer.verify();
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void contentAsBytes() throws Exception {
this.mockServer.expect(content().bytes("foo".getBytes())).andRespond(withSuccess());
executeAndVerify("foo");
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void buildMultipleTimes() {
MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(this.restTemplate);
MockRestServiceServer server = builder.build();
server.expect(requestTo("/foo")).andRespond(withSuccess());
this.restTemplate.getForObject("/foo", Void.class);
server.verify();
server = builder.ignoreExpectOrder(true).build();
server.expect(requestTo("/foo")).andRespond(withSuccess());
server.expect(requestTo("/bar")).andRespond(withSuccess());
this.restTemplate.getForObject("/bar", Void.class);
this.restTemplate.getForObject("/foo", Void.class);
server.verify();
server = builder.build();
server.expect(requestTo("/bar")).andRespond(withSuccess());
this.restTemplate.getForObject("/bar", Void.class);
server.verify();
}
代码示例来源:origin: spring-projects/spring-framework
@Before
public void setup() {
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new StringHttpMessageConverter());
converters.add(new MappingJackson2HttpMessageConverter());
this.restTemplate = new RestTemplate();
this.restTemplate.setMessageConverters(converters);
this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
}
代码示例来源:origin: spring-projects/spring-framework
private void executeAndVerify() throws URISyntaxException {
this.restTemplate.put(new URI("/composers"), this.people);
this.mockServer.verify();
}
内容来源于网络,如有侵权,请联系作者删除!