BAEL-1085: @RequestBody and @ResponseBody Annotation (#2583)
* Simple Boot REST application and example * BAEL-509 - Removed extra duplicate ant matcher * Example and Unit Tests * documentation * BAEL-1085 - changes per PR code review and document review - altered integration to unit test - all's good * BAEL-1085 - Renamed unit tests and added both to pom.xml * IntelliJ formatter * REVERT - Had removed a duplicate ant matcher from BAEL-509 - this was the incorrect process - reverting now, article has been corrected, but will issue a seperate PR for this * BAEL-509: Removed duplicate ant matcher - does not impact code at runtime * BAEL-1085: Per editor's request, removed Angular client - using CURL POST commands now in article - as such moved the code into a more appropriate module: spring-rest
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
d4d0305f3e
commit
3e192a0707
@@ -1,59 +0,0 @@
|
||||
package com.baeldung.controllers;
|
||||
|
||||
import com.baeldung.services.ExampleService;
|
||||
import com.baeldung.transfer.LoginForm;
|
||||
import org.baeldung.web.main.Application;
|
||||
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 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;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package com.baeldung.controllers;
|
||||
|
||||
import com.baeldung.services.ExampleService;
|
||||
import com.baeldung.transfer.LoginForm;
|
||||
import org.baeldung.web.main.Application;
|
||||
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 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;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
public class ExamplePostControllerResponseIntegrationTest {
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Mock
|
||||
private ExampleService exampleService;
|
||||
|
||||
@InjectMocks
|
||||
private ExamplePostController exampleController;
|
||||
|
||||
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);
|
||||
String jsonBody = "{\"username\": \"username\", \"password\": \"password\"}";
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user