|
1 | 1 | package com.beta.replyservice;
|
2 | 2 |
|
3 |
| -import org.junit.jupiter.api.Test; |
| 3 | +import static org.junit.Assert.assertEquals; |
4 | 4 |
|
| 5 | +import java.io.IOException; |
| 6 | +import java.io.UnsupportedEncodingException; |
| 7 | + |
| 8 | +import org.junit.Before; |
| 9 | +import org.junit.Test; |
| 10 | +import org.junit.runner.RunWith; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
5 | 12 | import org.springframework.boot.test.context.SpringBootTest;
|
| 13 | +import org.springframework.http.MediaType; |
| 14 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 15 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 16 | +import org.springframework.test.web.servlet.MockMvc; |
| 17 | +import org.springframework.test.web.servlet.MvcResult; |
| 18 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 19 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 20 | +import org.springframework.web.context.WebApplicationContext; |
| 21 | + |
| 22 | +import com.fasterxml.jackson.core.JsonParseException; |
| 23 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 24 | +import com.fasterxml.jackson.databind.JsonMappingException; |
| 25 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 26 | + |
| 27 | +import junit.framework.Assert; |
6 | 28 |
|
7 |
| -@SpringBootTest |
| 29 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 30 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 31 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 32 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 33 | + |
| 34 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 35 | +@SpringBootTest(classes = RestServiceApplication.class) |
| 36 | +@WebAppConfiguration |
8 | 37 | public class RestServiceApplicationTest {
|
9 | 38 |
|
| 39 | + @Autowired |
| 40 | + ReplyController repl; |
| 41 | + |
| 42 | + @Autowired |
| 43 | + ReplyHelperService helper; |
| 44 | + |
| 45 | + protected MockMvc mvc; |
| 46 | + |
| 47 | + @Autowired |
| 48 | + WebApplicationContext webApplicationContext; |
| 49 | + |
| 50 | + @Before |
| 51 | + public void setUp() { |
| 52 | + mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); |
| 53 | + } |
| 54 | + |
| 55 | + public String mapToJson(Object obj) throws JsonProcessingException { |
| 56 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 57 | + return objectMapper.writeValueAsString(obj); |
| 58 | + } |
| 59 | + |
| 60 | + public <T> T mapFromJson(String json, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException { |
| 61 | + |
| 62 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 63 | + return objectMapper.readValue(json, clazz); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void replyingTest() { |
| 68 | + String uri = "/reply/{message}"; |
| 69 | + String str = "1-abhi"; |
| 70 | + |
| 71 | + String inputJson = null; |
| 72 | + try { |
| 73 | + inputJson = mapToJson(str); |
| 74 | + } catch (JsonProcessingException e1) { |
| 75 | + // TODO Auto-generated catch block |
| 76 | + e1.printStackTrace(); |
| 77 | + } |
| 78 | + |
| 79 | + MvcResult mvcResult = null; |
| 80 | + try { |
| 81 | + MvcResult grantAuthzResult = mvc.perform(MockMvcRequestBuilders.get(uri, str) |
| 82 | + .contentType(MediaType.APPLICATION_JSON_VALUE).content(inputJson.toString())) |
| 83 | + .andExpect(status().isOk()).andReturn(); |
| 84 | + |
| 85 | + System.out.println(grantAuthzResult.getResponse().getContentAsString()); |
| 86 | + |
| 87 | + } catch (Exception e) { |
| 88 | + System.out.println("Error happened while testing reply api"); |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + |
10 | 93 | @Test
|
11 | 94 | public void contextLoads() {
|
12 | 95 | }
|
|
0 commit comments