001/* 002 * Copyright 2012-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.test.spock; 018 019import org.springframework.beans.factory.ObjectProvider; 020import org.springframework.boot.test.context.SpringBootTest; 021import org.springframework.boot.test.web.client.LocalHostUriTemplateHandler; 022import org.springframework.boot.test.web.client.TestRestTemplate; 023import org.springframework.boot.web.client.RestTemplateBuilder; 024import org.springframework.context.annotation.Bean; 025import org.springframework.context.annotation.Configuration; 026import org.springframework.core.env.Environment; 027 028/** 029 * Example configuration for using TestRestTemplate with Spock 1.0 when 030 * {@link SpringBootTest} cannot be used. 031 * 032 * @author Andy Wilkinson 033 */ 034public class SpockTestRestTemplateExample { 035 036 /** 037 * Test configuration for a {@link TestRestTemplate}. 038 */ 039 // tag::test-rest-template-configuration[] 040 @Configuration 041 static class TestRestTemplateConfiguration { 042 043 @Bean 044 public TestRestTemplate testRestTemplate( 045 ObjectProvider<RestTemplateBuilder> builderProvider, 046 Environment environment) { 047 RestTemplateBuilder builder = builderProvider.getIfAvailable(); 048 TestRestTemplate template = builder == null ? new TestRestTemplate() 049 : new TestRestTemplate(builder.build()); 050 template.setUriTemplateHandler(new LocalHostUriTemplateHandler(environment)); 051 return template; 052 } 053 054 } 055 // end::test-rest-template-configuration[] 056 057}