|
| 1 | +/* |
| 2 | + * Copyright (c) 2015 Espark And ©Adarsh Development Services @copyright All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions |
| 6 | + * are met: |
| 7 | + * |
| 8 | + * - Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * - Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in the |
| 13 | + * documentation and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * - Neither the name of Espark nor the names of its |
| 16 | + * contributors may be used to endorse or promote products derived |
| 17 | + * from this software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 20 | + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 21 | + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + */ |
| 31 | +package com.espark.adarsh; |
| 32 | + |
| 33 | +import static org.hamcrest.Matchers.equalTo; |
| 34 | +import static org.junit.Assert.assertThat; |
| 35 | + |
| 36 | +import java.net.URL; |
| 37 | +import java.util.HashMap; |
| 38 | +import java.util.Map; |
| 39 | + |
| 40 | +import com.google.gson.Gson; |
| 41 | +import com.google.gson.reflect.TypeToken; |
| 42 | +import org.junit.Before; |
| 43 | +import org.junit.Test; |
| 44 | +import org.junit.runner.RunWith; |
| 45 | +import org.springframework.beans.factory.annotation.Value; |
| 46 | +import org.springframework.boot.test.IntegrationTest; |
| 47 | +import org.springframework.boot.test.SpringApplicationConfiguration; |
| 48 | +import org.springframework.boot.test.TestRestTemplate; |
| 49 | +import org.springframework.http.ResponseEntity; |
| 50 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 51 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 52 | +import org.springframework.web.client.RestTemplate; |
| 53 | + |
| 54 | + |
| 55 | +/** |
| 56 | + * @author Adarsh Kumar |
| 57 | + * @author $LastChangedBy: Adarsh Kumar$ |
| 58 | + * @version $Revision: 0001 $, $Date:: 1/1/10 0:00 AM#$ |
| 59 | + * @Espark @copyright all right reserve |
| 60 | + */ |
| 61 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 62 | +@SpringApplicationConfiguration(classes = SpringBootController.class) |
| 63 | +@WebAppConfiguration |
| 64 | +@IntegrationTest({"server.port=0"}) |
| 65 | +public class SpringBootControllerRestClientTest { |
| 66 | + |
| 67 | + @Value("${local.server.port}") |
| 68 | + private int port; |
| 69 | + |
| 70 | + @Value("${message}") |
| 71 | + private String message; |
| 72 | + |
| 73 | + private URL base; |
| 74 | + private RestTemplate template; |
| 75 | + |
| 76 | + @Before |
| 77 | + public void setUp() throws Exception { |
| 78 | + this.base = new URL("http://localhost:" + port + "/application/welcome"); |
| 79 | + template = new TestRestTemplate(); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void getHello() throws Exception { |
| 84 | + ResponseEntity<String> response = template.getForEntity(base.toString(), String.class); |
| 85 | + final String jsonString=response.getBody(); |
| 86 | + Map<String, String> retMap = new Gson().fromJson(jsonString, new TypeToken<HashMap<String, String>>() {}.getType()); |
| 87 | + assertThat(retMap.get("name"), equalTo(System.getProperty("user.name"))); |
| 88 | + assertThat(retMap.get("msg"), equalTo("Hello " + System.getProperty("user.name") + " " +message)); |
| 89 | + } |
| 90 | +} |
0 commit comments