JAVA-10516: Revisit spring-boot module inside spring-boot-modules folder (#12038)
This commit is contained in:
+71
@@ -0,0 +1,71 @@
|
||||
package com.baeldung.beanvalidation.application;
|
||||
|
||||
import com.baeldung.beanvalidation.application.controllers.UserController;
|
||||
import com.baeldung.beanvalidation.application.repositories.UserRepository;
|
||||
import org.hamcrest.core.Is;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebMvcTest
|
||||
@AutoConfigureMockMvc
|
||||
public class UserControllerIntegrationTest {
|
||||
|
||||
@MockBean
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
UserController userController;
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
public void whenUserControllerInjected_thenNotNull() throws Exception {
|
||||
assertThat(userController).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetRequestToUsers_thenCorrectResponse() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/users")
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPostRequestToUsersAndValidUser_thenCorrectResponse() throws Exception {
|
||||
MediaType textPlainUtf8 = new MediaType(MediaType.TEXT_PLAIN, Charset.forName("UTF-8"));
|
||||
String user = "{\"name\": \"bob\", \"email\" : \"bob@domain.com\"}";
|
||||
mockMvc.perform(MockMvcRequestBuilders.post("/users")
|
||||
.content(user)
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.content().contentType(textPlainUtf8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPostRequestToUsersAndInValidUser_thenCorrectReponse() throws Exception {
|
||||
String user = "{\"name\": \"\", \"email\" : \"bob@domain.com\"}";
|
||||
mockMvc.perform(MockMvcRequestBuilders.post("/users")
|
||||
.content(user)
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(MockMvcResultMatchers.status().isBadRequest())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.name", Is.is("Name is mandatory")))
|
||||
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user