[BAEL-14088] - Moved code for RequestBody and ResponseBody annotations article

This commit is contained in:
amit2103
2019-04-13 23:16:33 +05:30
parent 9df90567d4
commit 6b0a96291e
9 changed files with 26 additions and 23 deletions

View File

@@ -0,0 +1,58 @@
package com.baeldung.controllers;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.baeldung.SpringBootRestApplication;
import com.baeldung.services.ExampleService;
import com.baeldung.transfer.LoginForm;
import com.baeldung.web.controller.ExamplePostController;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootRestApplication.class)
public class ExamplePostControllerRequestIntegrationTest {
MockMvc mockMvc;
@Mock private ExampleService exampleService;
@InjectMocks private ExamplePostController exampleController;
private final String jsonBody = "{\"username\": \"username\", \"password\": \"password\"}";
private LoginForm lf = new LoginForm();
@Before
public void preTest() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders
.standaloneSetup(exampleController)
.build();
lf.setPassword("password");
lf.setUsername("username");
}
@Test
public void requestBodyTest() {
try {
when(exampleService.fakeAuthenticate(lf)).thenReturn(true);
mockMvc
.perform(post("/post/request")
.content(jsonBody)
.contentType("application/json"))
.andDo(print())
.andExpect(status().isOk());
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}

View File

@@ -0,0 +1,60 @@
package com.baeldung.controllers;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.baeldung.SpringBootRestApplication;
import com.baeldung.services.ExampleService;
import com.baeldung.transfer.LoginForm;
import com.baeldung.web.controller.ExamplePostController;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootRestApplication.class)
public class ExamplePostControllerResponseIntegrationTest {
MockMvc mockMvc;
@Mock private ExampleService exampleService;
@InjectMocks private ExamplePostController exampleController;
private final String jsonBody = "{\"username\": \"username\", \"password\": \"password\"}";
private LoginForm lf = new LoginForm();
@Before
public void preTest() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders
.standaloneSetup(exampleController)
.build();
lf.setPassword("password");
lf.setUsername("username");
}
@Test
public void requestBodyTest() {
try {
when(exampleService.fakeAuthenticate(lf)).thenReturn(true);
mockMvc
.perform(post("/post/response")
.content(jsonBody)
.contentType("application/json"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().json("{\"text\":\"Thanks For Posting!!!\"}"));
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}

View File

@@ -0,0 +1,43 @@
package com.baeldung.resttemplate;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.client.RestTemplate;
import com.baeldung.config.RestClientConfig;
import com.baeldung.transfer.LoginForm;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = RestClientConfig.class)
public class RestTemplateLiveTest {
@Autowired
RestTemplate restTemplate;
@Test
public void givenRestTemplate_whenRequested_thenLogAndModifyResponse() {
LoginForm loginForm = new LoginForm("userName", "password");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<LoginForm> requestEntity = new HttpEntity<LoginForm>(loginForm, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://httpbin.org/post", requestEntity, String.class);
assertThat(responseEntity.getStatusCode(), is(equalTo(HttpStatus.OK)));
assertThat(responseEntity.getHeaders()
.get("Foo")
.get(0), is(equalTo("bar")));
}
}