2626import org .springframework .http .HttpMethod ;
2727import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
2828import org .springframework .web .client .RestTemplate ;
29+ import org .springframework .web .util .UriComponentsBuilder ;
2930
3031import java .util .List ;
3132
@@ -43,24 +44,41 @@ public class DomaBootSampleSimpleApplicationTest {
4344
4445 @ Test
4546 public void test () {
46- Message message1 = restTemplate .getForObject ("http://localhost:" + port
47- + "?text=hello" , Message .class );
47+ Message message1 = restTemplate .getForObject (
48+ UriComponentsBuilder .fromUriString ("http://localhost" ).port (port )
49+ .queryParam ("text" , "hello" ).build ().toUri (), Message .class );
4850 assertThat (message1 .id , is (1 ));
4951 assertThat (message1 .text , is ("hello" ));
50- Message message2 = restTemplate .getForObject ("http://localhost:" + port
51- + "?text=world" , Message .class );
52+ Message message2 = restTemplate .getForObject (
53+ UriComponentsBuilder .fromUriString ("http://localhost" ).port (port )
54+ .queryParam ("text" , "world" ).build ().toUri (), Message .class );
5255 assertThat (message2 .id , is (2 ));
5356 assertThat (message2 .text , is ("world" ));
5457
55- List <Message > messages = restTemplate .exchange ("http://localhost:" + port ,
56- HttpMethod .GET , HttpEntity .EMPTY ,
57- new ParameterizedTypeReference <List <Message >>() {
58- }).getBody ();
59- assertThat (messages .size (), is (2 ));
60- assertThat (messages .get (0 ).id , is (message1 .id ));
61- assertThat (messages .get (0 ).text , is (message1 .text ));
62- assertThat (messages .get (1 ).id , is (message2 .id ));
63- assertThat (messages .get (1 ).text , is (message2 .text ));
58+ {
59+ List <Message > messages = restTemplate .exchange (
60+ UriComponentsBuilder .fromUriString ("http://localhost" ).port (port )
61+ .build ().toUri (), HttpMethod .GET , HttpEntity .EMPTY ,
62+ new ParameterizedTypeReference <List <Message >>() {
63+ }).getBody ();
64+ assertThat (messages .size (), is (2 ));
65+ assertThat (messages .get (0 ).id , is (message1 .id ));
66+ assertThat (messages .get (0 ).text , is (message1 .text ));
67+ assertThat (messages .get (1 ).id , is (message2 .id ));
68+ assertThat (messages .get (1 ).text , is (message2 .text ));
69+ }
70+
71+ {
72+ List <Message > messages = restTemplate .exchange (
73+ UriComponentsBuilder .fromUriString ("http://localhost" ).port (port )
74+ .queryParam ("page" , "1" ).queryParam ("size" , "1" ).build ()
75+ .toUri (), HttpMethod .GET , HttpEntity .EMPTY ,
76+ new ParameterizedTypeReference <List <Message >>() {
77+ }).getBody ();
78+ assertThat (messages .size (), is (1 ));
79+ assertThat (messages .get (0 ).id , is (message2 .id ));
80+ assertThat (messages .get (0 ).text , is (message2 .text ));
81+ }
6482 }
6583
6684}
0 commit comments