[英]How to skip authentication for TestRestTemplate in Spring Boot Tests?
[英]How to configure Spring TestRestTemplate
我有一個REST(spring-hateoas)服務器,我想用JUnit測試來測試。 因此我使用自動TestRestTemplate
。
但是,我現在如何為此預先配置的TestRestTemplate添加更多配置? 我需要配置rootURI並添加攔截器。
這是我的JUnit Test類:
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class RestEndpointTests {
private Logger log = LoggerFactory.getLogger(this.getClass());
@LocalServerPort
int localServerPort;
@Value(value = "${spring.data.rest.base-path}") // nice trick to get basePath from application.properties
String basePath;
@Autowired
TestRestTemplate client; // how to configure client?
[... here are my @Test methods that use client ...]
}
文檔說明可以使用靜態@TestConfiguration
類。 但是在靜態類中我無法訪問localServerPort
或basePath
:
@TestConfiguration
static class Config {
@Bean
public RestTemplateBuilder restTemplateBuilder() {
String rootUri = "http://localhost:"+localServerPort+basePath; // <=== DOES NOT WORK
log.trace("Creating and configuring RestTemplate for "+rootUri);
return new RestTemplateBuilder()
.basicAuthorization(TestFixtures.USER1_EMAIL, TestFixtures.USER1_PWD)
.errorHandler(new LiquidoTestErrorHandler())
.requestFactory(new HttpComponentsClientHttpRequestFactory())
.additionalInterceptors(new LogRequestInterceptor())
.rootUri(rootUri);
}
}
我最重要的問題是:為什么不TestRestTemplate
采取spring.data.rest.base-path
從application.properties
考慮擺在首位? 是不是完全預配置的想法,這個包裝類的整個用例?
doc sais
如果您正在使用@SpringBootTest注釋,TestRestTemplate將自動可用,並且可以@Autowired到您的測試中。 如果需要自定義(例如添加其他消息轉換器),請使用RestTemplateBuilder @Bean。
在完整的Java代碼示例中,它看起來如何?
我知道這是一個老問題,你現在可能已經找到了另一個解決方案。 但無論如何,我正在回答其他人像我一樣磕磕絆絆。 我有一個類似的問題,最后在我的測試類中使用@PostConstruct來構造一個配置為我喜歡的TestRestTemplate而不是使用@TestConfiguration。
@RunWith(SpringJUnit4ClassRunner.class)
@EnableAutoConfiguration
@SpringBootTest(classes = {BackendApplication.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MyCookieClientTest {
@LocalServerPort
int localPort;
@Autowired
RestTemplateBuilder restTemplateBuilder;
private TestRestTemplate template;
@PostConstruct
public void initialize() {
RestTemplate customTemplate = restTemplateBuilder
.rootUri("http://localhost:"+localPort)
....
.build();
this.template = new TestRestTemplate(customTemplate,
null, null, //I don't use basic auth, if you do you can set user, pass here
HttpClientOption.ENABLE_COOKIES); // I needed cookie support in this particular test, you may not have this need
}
}
為了配置TestRestTemplate,官方文檔建議您使用TestRestTemplate,如下面的示例所示(例如,添加基本身份驗證):
public class YourEndpointClassTest {
private static final Logger logger = LoggerFactory.getLogger(YourEndpointClassTest.class);
private static final String BASE_URL = "/your/base/url";
@TestConfiguration
static class TestRestTemplateAuthenticationConfiguration {
@Value("${spring.security.user.name}")
private String userName;
@Value("${spring.security.user.password}")
private String password;
@Bean
public RestTemplateBuilder restTemplateBuilder() {
return new RestTemplateBuilder().basicAuthentication(userName, password);
}
}
@Autowired
private TestRestTemplate restTemplate;
//here add your tests...
當我需要在我們的測試環境中使用TestRestTemplate
訪問遠程服務器上的REST端點時,我遇到了這種情況。 因此,測試沒有啟動Spring Boot應用程序,而是僅連接到遠程端點並從那里使用REST服務。 測試的配置更簡單,執行速度更快,因為它沒有構建復雜的Spring(Boot)上下文。 以下是我配置的摘錄:
@RunWith(SpringRunner.class)
public class RemoteRestTestAbstract {
protected TestRestTemplate restTemplate;
private static RestTemplateBuilder restTemplateBuilder;
@BeforeClass
public static void setUpClass() {
restTemplateBuilder = new RestTemplateBuilder()
.rootUri("http://my-remote-test-server.my-domain.com:8080/");
}
@Before
public void init() {
restTemplate = new TestRestTemplate(restTemplateBuilder);
login();
}
//...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.