BAEL-1085 (#2428)
* 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
This commit is contained in:
committed by
Zeger Hendrikse
parent
b6830cc736
commit
4baaf9f984
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.controllers;
|
||||
|
||||
import com.baeldung.services.ExampleService;
|
||||
import com.baeldung.transfer.LoginForm;
|
||||
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 org.baeldung.web.main.Application;
|
||||
|
||||
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 ExamplePostControllerRequestUnitTest {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.controllers;
|
||||
|
||||
import com.baeldung.services.ExampleService;
|
||||
import com.baeldung.transfer.LoginForm;
|
||||
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.baeldung.web.main.Application;
|
||||
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 ExamplePostControllerResponseUnitTest {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user