Giriş
Açıklaması şöyle.  Yani bu sınıfı istersek TestRestTemplate template = new TestRestTemplate(); şeklinde veya @Autowired şeklinde kullanabiliriz. Bu sınıf gerçek bir servlet ortamında çalışır. 
TestRestTemplate can be instantiated directly in your integration tests ...Alternatively, if you use the @SpringBootTest annotation with WebEnvironment.RANDOM_PORT or WebEnvironment.DEFINED_PORT, you can inject a fully configured TestRestTemplate and start using it. If necessary, additional customizations can be applied through the RestTemplateBuilder bean. Any URLs that do not specify a host and port automatically connect to the embedded server
Kodu şöyle
public class TestRestTemplate {
  private final RestTemplateBuilder builder;
  private final HttpClientOption[] httpClientOptions;
  private final RestTemplate restTemplate;
  ...
}constructor - username + password
Açıklaması şöyle. 
You can get a vanilla template or one that sends Basic HTTP authentication (with a username and password).
Örnek
Şöyle yaparız
TestRestTemplate restTemplate = new  TestRestTemplate("admin", "password", null);
getForObject metodu
ÖrnekŞöyle yaparız
@RunWith(SpringRunner.class)@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)public class MyServerTest {@LocalServerPortprivate int port;@Autowiredprivate TestRestTemplate restTemplate;@Testpublic void testFooGet() throws IOException, InterruptedException {assertTrue(this.restTemplate.getForObject("http:localhost" + port +
"/foo",String.class.contains("...");}}
 
Hiç yorum yok:
Yorum Gönder