Java Code Examples for org.springframework.test.web.client.MockRestServiceServer#verify()

The following examples show how to use org.springframework.test.web.client.MockRestServiceServer#verify() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GitHbubReposTest.java    From Java-programming-methodology-Rxjava-articles with Apache License 2.0 6 votes vote down vote up
@Test
public void getRepos() {
    MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate).build();
    mockServer.expect(requestTo(String.format(GitHbubRepos.REPOS+"?direction=desc", user)))
              .andRespond(withSuccess(
                      "[{\"name\":\"zheng\",\"pushed_at\":[2017,6,3,10,56,26]}," +
                              "{\"name\":\"ui-for-docker\",\"pushed_at\":[2016,12,3,22,23,52]}," +
                              "{\"name\":\"micro-service-practice\",\"pushed_at\":[2016,6,17,1,51,13]}," +
                              "{\"name\":\"Java-9-Spring-Webflux\",\"pushed_at\":[2018,4,24,16,5,47]}," +
                              "{\"name\":\"guava\",\"pushed_at\":[2017,5,7,15,17,46]}]",
                      /*pushed_at 可能会有变化,具体请自行修改*/
                      MediaType.APPLICATION_JSON)
              );

    List<Repository> repos = gitHbubRepos.getRepos(user);

    assertThat(repos.size()).isEqualTo(5);
    assertThat(repos.get(0).getPushed().getHour()).isEqualTo(10);
    mockServer.verify();
}
 
Example 2
Source File: UriTemplateArgTest.java    From riptide with MIT License 6 votes vote down vote up
@ParameterizedTest
@MethodSource("data")
void shouldExpand(final String baseUrl, final String uriTemplate, final Object[] uriVariables,
        final String requestUrl) {

    final MockSetup setup = new MockSetup(baseUrl);
    final MockRestServiceServer server = setup.getServer();
    final Http unit = setup.getHttp();

    server.expect(requestTo(requestUrl))
            .andExpect(method(HttpMethod.GET))
            .andRespond(withSuccess());

    unit.get(uriTemplate, uriVariables)
            .dispatch(series(),
                    on(SUCCESSFUL).call(pass()))
            .join();

    server.verify();
}
 
Example 3
Source File: DefaultSkipperClientTests.java    From spring-cloud-skipper with Apache License 2.0 6 votes vote down vote up
@Test
public void testScaleByReleaseAndScaleRequest() {
	RestTemplate restTemplate = new RestTemplate();
	SkipperClient skipperClient = new DefaultSkipperClient("", restTemplate);

	MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate).build();
	mockServer
		.expect(requestTo("/release/scale/mylog"))
		.andExpect(content().json("{\"scale\":[{\"name\":\"app\",\"count\":2,\"properties\":{}}]}"))
		.andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));

	ScaleRequest scaleRequest = ScaleRequest.of("app", 2);
	Release release = skipperClient.scale("mylog", scaleRequest);
	mockServer.verify();

	assertThat(release).isNotNull();
}
 
Example 4
Source File: MethodDelegateTest.java    From riptide with MIT License 6 votes vote down vote up
@ParameterizedTest
@MethodSource("data")
void shouldDelegate(final HttpMethod method, final Tester tester) {
    final MockSetup setup = new MockSetup("https://example.com");
    final Http unit = setup.getHttp();
    final MockRestServiceServer server = setup.getServer();

    server.expect(requestTo("https://example.com"))
            .andExpect(method(method))
            .andRespond(withSuccess());

    tester.test(unit)
            .call(pass())
            .join();

    server.verify();
}
 
Example 5
Source File: SampleTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test // SPR-14694
public void repeatedAccessToResponseViaResource() {

	Resource resource = new ClassPathResource("ludwig.json", this.getClass());

	RestTemplate restTemplate = new RestTemplate();
	restTemplate.setInterceptors(Collections.singletonList(new ContentInterceptor(resource)));

	MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate)
			.ignoreExpectOrder(true)
			.bufferContent()  // enable repeated reads of response body
			.build();

	mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
			.andRespond(withSuccess(resource, MediaType.APPLICATION_JSON));

	restTemplate.getForObject("/composers/{id}", Person.class, 42);

	mockServer.verify();
}
 
Example 6
Source File: ResourceServerTokenRelayAutoConfigurationTests.java    From spring-cloud-security with Apache License 2.0 6 votes vote down vote up
@Test
public void clientConfigured() throws Exception {
	this.context = new SpringApplicationBuilder(ClientConfiguration.class)
			.properties("spring.config.name=test", "server.port=0",
					"spring.cloud.gateway.enabled=false",
					"security.oauth2.resource.userInfoUri:https://example.com",
					"security.oauth2.client.clientId=foo")
			.run();
	RequestContextHolder.setRequestAttributes(
			new ServletRequestAttributes(new MockHttpServletRequest()));
	OAuth2ClientContext client = this.context.getBean(OAuth2ClientContext.class);
	assertThat(client.getAccessToken()).isNull();
	UserInfoTokenServices services = context.getBean(UserInfoTokenServices.class);
	OAuth2RestTemplate template = (OAuth2RestTemplate) ReflectionTestUtils
			.getField(services, "restTemplate");
	MockRestServiceServer server = MockRestServiceServer.createServer(template);
	server.expect(requestTo("https://example.com"))
			.andRespond(withSuccess("{\"id\":\"user\"}", MediaType.APPLICATION_JSON));
	services.loadAuthentication("FOO");
	assertThat(client.getAccessToken().getValue()).isEqualTo("FOO");
	server.verify();
}
 
Example 7
Source File: SampleTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test // SPR-14694
public void repeatedAccessToResponseViaResource() {

	Resource resource = new ClassPathResource("ludwig.json", this.getClass());

	RestTemplate restTemplate = new RestTemplate();
	restTemplate.setInterceptors(Collections.singletonList(new ContentInterceptor(resource)));

	MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate)
			.ignoreExpectOrder(true)
			.bufferContent()  // enable repeated reads of response body
			.build();

	mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
			.andRespond(withSuccess(resource, MediaType.APPLICATION_JSON));

	restTemplate.getForObject("/composers/{id}", Person.class, 42);

	mockServer.verify();
}
 
Example 8
Source File: WiremockMockServerApplicationTests.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@Test
public void contextLoads() throws Exception {
	MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
			.baseUrl("https://example.org") //
			.stubs("classpath:/stubs/resource.json").build();
	assertThat(this.service.go()).isEqualTo("Hello World");
	server.verify();
}
 
Example 9
Source File: DefaultSkipperClientTests.java    From spring-cloud-skipper with Apache License 2.0 5 votes vote down vote up
@Test
public void testLogByReleaseName() {
	RestTemplate restTemplate = new RestTemplate();
	SkipperClient skipperClient = new DefaultSkipperClient("", restTemplate);

	MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate).build();
	mockServer.expect(requestTo("/release/logs/mylog")).andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));

	LogInfo logContent = skipperClient.getLog("mylog");
	mockServer.verify();

	assertThat(logContent).isNotNull();
}
 
Example 10
Source File: DefaultSkipperClientTests.java    From spring-cloud-skipper with Apache License 2.0 5 votes vote down vote up
@Test
public void testLogByReleaseAndAppNames() {
	RestTemplate restTemplate = new RestTemplate();
	SkipperClient skipperClient = new DefaultSkipperClient("", restTemplate);

	MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate).build();
	mockServer.expect(requestTo("/release/logs/mylog/app")).andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));

	LogInfo logContent = skipperClient.getLog("mylog", "app");
	mockServer.verify();

	assertThat(logContent).isNotNull();
}
 
Example 11
Source File: WiremockMockServerApplicationTests.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@Test
public void contextLoads() throws Exception {
	MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
			.baseUrl("https://example.org") //
			.stubs("classpath:/stubs/**/*.json").build();
	assertThat(this.service.go()).isEqualTo("Hello World");
	server.verify();
}
 
Example 12
Source File: WiremockMockServerApplicationTests.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleGet() throws Exception {
	MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
			.baseUrl("https://example.org") //
			.stubs("classpath:/mappings/resource.json").build();
	assertThat(this.restTemplate.getForObject("https://example.org/resource",
			String.class)).isEqualTo("Hello World");
	server.verify();
}
 
Example 13
Source File: WiremockMockServerApplicationTests.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleGetWithBodyFile() throws Exception {
	MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
			.baseUrl("https://example.org") //
			.stubs("classpath:/mappings/resource-with-body-file.json").build();
	assertThat(this.restTemplate.getForObject("https://example.org/resource",
			String.class))
					.isEqualToIgnoringWhitespace("{\"message\":\"Hello World\"}");
	server.verify();
}
 
Example 14
Source File: WiremockMockServerApplicationTests.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleGetWithBodyFileCustomLocation() throws Exception {
	MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
			.baseUrl("https://example.org") //
			.stubs("classpath:/mappings/resource-with-body-file.json")
			.files("classpath:/custom/").build();
	assertThat(this.restTemplate.getForObject("https://example.org/resource",
			String.class))
					.isEqualToIgnoringWhitespace("{\"message\":\"Hello Custom\"}");
	server.verify();
}
 
Example 15
Source File: WiremockMockServerApplicationTests.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleGetWithBodyFileCustomLocationDirectory() throws Exception {
	MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
			.baseUrl("https://example.org") //
			.stubs("classpath:/mappings/resource-with-body-file.json")
			.files("file:src/test/resources/custom").build();
	assertThat(this.restTemplate.getForObject("https://example.org/resource",
			String.class))
					.isEqualToIgnoringWhitespace("{\"message\":\"Hello Custom\"}");
	server.verify();
}
 
Example 16
Source File: WiremockMockServerApplicationTests.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleGetWithEmptyPath() throws Exception {
	MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
			.baseUrl("https://example.org") //
			.stubs("classpath:/mappings/resource-with-empty-path.json").build();
	assertThat(this.restTemplate.getForObject("https://example.org/", String.class))
			.isEqualTo("Hello World");
	server.verify();
}
 
Example 17
Source File: WiremockMockServerApplicationTests.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleGetWithContentType() throws Exception {
	MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
			.baseUrl("https://example.org") //
			.stubs("classpath:/mappings/resource-with-content-type.json").build();
	assertThat(this.restTemplate.getForObject("https://example.org/resource",
			String.class)).isEqualTo("Hello World");
	server.verify();
}
 
Example 18
Source File: WiremockMockServerApplicationTests.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleGetWithoutContentType() throws Exception {
	MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
			.baseUrl("https://example.org") //
			.stubs("classpath:/mappings/resource-without-content-type.json").build();
	assertThat(this.restTemplate.getForObject("https://example.org/resource",
			String.class)).isEqualTo("Hello World");
	server.verify();
}
 
Example 19
Source File: WiremockMockServerApplicationTests.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@Test
public void getWithUrlMatching() throws Exception {
	MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
			.baseUrl("https://example.org") //
			.stubs("classpath:/mappings/url-matches.json").build();
	assertThat(this.restTemplate
			.getForObject("https://example.org/123/hello-url-matcher/", String.class))
					.isEqualTo("Hello Url Matcher");
	server.verify();
}
 
Example 20
Source File: WiremockMockServerApplicationTests.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@Test
public void getWithUrlPathMatching() throws Exception {
	MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //
			.baseUrl("https://example.org") //
			.stubs("classpath:/mappings/url-path-pattern.json").build();
	assertThat(this.restTemplate
			.getForObject("https://example.org/123/url-path-pattern/", String.class))
					.isEqualTo("Hello Url Path Matcher");
	server.verify();
}