Spring boot 使用MockMvc和SpringREST文档时重新使用SpringBootErrorAttributes
我正在为一个抛出自定义异常的控制器编写测试(Spring boot 使用MockMvc和SpringREST文档时重新使用SpringBootErrorAttributes,spring-boot,mockito,mockmvc,spring-restdocs,Spring Boot,Mockito,Mockmvc,Spring Restdocs,我正在为一个抛出自定义异常的控制器编写测试(AuthenticationException,在我的例子中),该异常用@ResponseStatus(value=HttpStatus.BAD_REQUEST)注释 使用curl调用引发异常的端点效果很好,我通过下面的示例获得了预期结果: { "timestamp": 1494185397677, "status": 400, "error": "Bad Request", "exception": "com.example.exce
AuthenticationException
,在我的例子中),该异常用@ResponseStatus(value=HttpStatus.BAD_REQUEST)注释
使用curl调用引发异常的端点效果很好,我通过下面的示例获得了预期结果:
{
"timestamp": 1494185397677,
"status": 400,
"error": "Bad Request",
"exception": "com.example.exception.AuthenticationException",
"message": "These are not the credentials you are looking for",
"path": "/status/throw/2"
}
当我用Mockito编写一个测试时,它使用了willThrow()
,我没有得到Spring Boot生成的任何输出,只有在我的exception类中注释的响应代码
这是我的测试:
@Test
public void throwsShouldShowResponseBody() throws Exception {
given(this.service.getAccStatus())
.willThrow(AuthenticationException.class);
this.mvc.perform(get("/status/throw/2"))
.andExpect(status().isBadRequest())
.andDo(document("bad-credentials"));
}
看看类似的问题,似乎这可能是因为MockMvc没有遵循我认为Spring Boot用于推送到/错误的重定向,但我的问题是,是否有任何方法可以使这项工作正常进行,这样我就不必编写类似于Spring Boot的@ControllerAdvice
类提供。我不希望更改Spring Boot在出现错误时生成的输出
谢谢-正如您所指出的,在使用MockMvc
时记录Spring Boot的错误响应有点棘手。这是因为Spring Boot将请求转发到映射到/error
的错误控制器,MockMvc
默认情况下不处理转发
记录错误响应的一种方法是使用适当配置的请求直接调用/error
。有一个:
接下来是描述整个API中使用的错误响应格式。与SpringREST文档有什么联系?您正在尝试记录错误响应吗?如果不是,您实际上只是在测试Spring Boot的错误控制器是否工作,而不是测试您的应用程序。我应该更清楚地说明这一点;是的,我试图记录当我抛出一个异常或者当它返回一个简单的404时,SpringBoots错误控制器将如何输出。基本上,我希望在我的文档中有一个部分可以显示spring boots错误响应,我可以将其用作参考。谢谢-这正是我想要的!还有你参考的样本——如果你没有在这里链接到它们,我从来没有遇到过。如果它们还没有链接到SpringREST文档的参考文档中,那就太好了。
package com.cts.skynews.springboot.controller;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.springframework.web.context.WebApplicationContext;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserControllerTest {
private static final Logger LOGGER = LoggerFactory.getLogger(UserControllerTest.class);
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void addNewUser() throws Exception {
LOGGER.info("START : Inside Spring Boot addUser() method of UserController");
String USER_DATA = "{\"name\":\"Kiran Ravariya\"," + "\"email\":\"kirasln@gmail.com\","
+ "\"password\":\"A123456\"," + "\"status\":\"active\"," + "\"language\":{\"id\":\"1\"},"
+ "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().isOk()).andExpect(jsonPath("$.signedUp").value("true"));
LOGGER.info("END : Spring Boot addUser() method of UserController");
}
@Test
public void checkEmailExists() throws Exception {
LOGGER.info("START : Inside Spring Boot checkEmailExists() method of UserController");
String USER_DATA = "{\"name\":\"Kiran Ravariya\"," + "\"email\":\"ravariakiran@gmail.com\","
+ "\"password\":\"A123456\"," + "\"status\":\"active\"," + "\"language\":{\"id\":\"1\"},"
+ "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().isOk()).andExpect(jsonPath("$.emailExists").value("true"))
.andExpect(jsonPath("$.signedUp").value("false"));
LOGGER.info("END : Spring Boot checkEmailExists() method of UserController");
}
@Test
public void incorrectEmailFormat() throws Exception {
LOGGER.info("START : Inside Spring Boot incorrectEmailFormat() method of UserController");
String USER_DATA = "{\"name\":\"Kiran Ravariya\"," + "\"email\":\"rgmail.com\"," + "\"password\":\"A123456\","
+ "\"status\":\"active\"," + "\"language\":{\"id\":\"1\"}," + "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.errorMessage").value("Input Validation Failed:Email address is invalid"));
LOGGER.info("END : Spring Boot incorrectEmailFormat() method of UserController");
}
@Test
public void nullName() throws Exception {
LOGGER.info("START : Inside Spring Boot nullName() method of UserController");
String USER_DATA = "{\"email\":\"abcdefg@gmail.com\"," + "\"password\":\"A123456\"," + "\"status\":\"active\","
+ "\"language\":{\"id\":\"1\"}," + "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.errorMessage").value("Input Validation Failed:Name cannot be empty"));
LOGGER.info("END : Spring Boot nullName() method of UserController");
}
@Test
public void nullPassword() throws Exception {
LOGGER.info("START : Inside Spring Boot nullPassword() method of UserController");
String USER_DATA = "{\"name\":\"Kiran Ravariya\"," + "\"email\":\"abcdefg@gmail.com\","
+ "\"status\":\"active\"," + "\"language\":{\"id\":\"1\"}," + "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.errorMessage").value("Input Validation Failed:Password cannot be empty"));
LOGGER.info("END : Spring Boot nullPassword() method of UserController");
}
@Test
public void nullEmail() throws Exception {
LOGGER.info("START : Inside Spring Boot nullEmail() method of UserController");
String USER_DATA = "{\"name\":\"Kiran Ravariya\"," + "\"password\":\"A123456\"," + "\"status\":\"active\","
+ "\"language\":{\"id\":\"1\"}," + "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.errorMessage").value("Input Validation Failed:Email cannot be empty"));
LOGGER.info("END : Spring Boot nullEmail() method of UserController");
}
@Test
public void nullStatus() throws Exception {
LOGGER.info("START : Inside Spring Boot nullEmail() method of UserController");
String USER_DATA = "{\"name\":\"Kiran Ravariya\"," + "\"email\":\"abcdefg@gmail.com\","
+ "\"password\":\"A123456\"," + "\"language\":{\"id\":\"1\"}," + "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.errorMessage").value("Input Validation Failed:Status cannot be empty"));
LOGGER.info("END : Spring Boot nullStatus() method of UserController");
}
@Test
public void langugaeNull() throws Exception {
LOGGER.info("START : Inside Spring Boot langugaeNull() method of UserController");
String USER_DATA = "{\"name\":\"Kiran Ravariya\"," + "\"email\":\"abcdefg@gmail.com\","
+ "\"password\":\"A123456\"," + "\"status\":\"active\"," + "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.errorMessage").value("Input Validation Failed:Language cannot be empty"));
LOGGER.info("END : Spring Boot langugaeNull() method of UserController");
}
@Test
public void roleNull() throws Exception {
LOGGER.info("START : Inside Spring Boot roleNull() method of UserController");
String USER_DATA = "{\"name\":\"Kiran Ravariya\"," + "\"email\":\"abcdefg@gmail.com\","
+ "\"password\":\"A123456\"," + "\"status\":\"active\"," + "\"language\":{\"id\":\"1\"}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.errorMessage").value("Input Validation Failed:Role cannot be empty"));
LOGGER.info("END : Spring Boot roleNull() method of UserController");
}
@Test
public void invalidNameLength() throws Exception {
LOGGER.info("START : Inside Spring Boot invalidNameLength() method of UserController");
String USER_DATA = "{\"name\":\"KiranKiranRavariyKiranKiranRavariyaRRavariyaRavariyaRavariyaRavariyaRavariya "
+ "KiranKiranRavariyKiranKiranRavariyaRRavariyaRavariyaRavariyaRavariyaRavariya "
+ "KiranKiranRavariyKiranKiranRavariyaRRavariyaRavariyaRavariyaRavariyaRavariya\","
+ "\"email\":\"abcdefg@gmail.com\"," + "\"password\":\"A123456\"," + "\"status\":\"active\","
+ "\"language\":{\"id\":\"1\"}," + "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.errorMessage").value("Input Validation Failed:Name must be 3 to 80 characters"));
LOGGER.info("END : Spring Boot invalidNameLength() method of UserController");
}
@Test
public void invalidEmailLength() throws Exception {
LOGGER.info("START : Inside Spring Boot invalidEmailLength() method of UserController");
String USER_DATA = "{\"name\":\"Kiran Ravariya\","
+ "\"email\":\"ravariakiran@gmailravariakiran@gmailravariakiran@gmailravariakiran@gmailravariakiran@gmailravariakiran@gmailravariakiran@gmailravariakiran@gmailravariakiran@gmailravariakiran@gmail.com\","
+ "\"password\":\"A123456\"," + "\"status\":\"active\"," + "\"language\":{\"id\":\"1\"},"
+ "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().is4xxClientError()).andExpect(
jsonPath("$.errorMessage").value("Input Validation Failed:Email must be 4 to 80 characters"));
LOGGER.info("END : Spring Boot invalidEmailLength() method of UserController");
}
@Test
public void incorrectPasswordFormat() throws Exception {
LOGGER.info("START : Inside Spring Boot incorrectPasswordFormat() method of UserController");
String USER_DATA = "{\"name\":\"Kiran Ravariya\"," + "\"email\":\"abcdefg@gmail.com\"," + "\"password\":\"12\","
+ "\"status\":\"active\"," + "\"language\":{\"id\":\"1\"}," + "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().is4xxClientError()).andExpect(jsonPath("$.errorMessage")
.value("Input Validation Failed:Password must be 6 to 45 characters"));
LOGGER.info("END : Spring Boot incorrectPasswordFormat() method of UserController");
}
@Test
public void successfullLogin() throws Exception {
LOGGER.info("START : Inside Spring Boot successfullLogin() method of UserController");
String USER_DATA = "{\"email\":\"kiran@gmail.com\",\"password\":\"A123456\"}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/login").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().isOk()).andExpect(jsonPath("$.authenticated").value("true"));
LOGGER.info("END : Spring Boot successfullLogin() method of UserController");
}
@Test
public void invalidEmailForLogin() throws Exception {
LOGGER.info("START : Inside Spring Boot invalidEmailForLogin() method of UserController");
String USER_DATA = "{\"email\":\"kiran123@gmail.com\",\"password\":\"A123456\"}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/login").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().isOk()).andExpect(jsonPath("$.authenticated").value("false"));
LOGGER.info("END : Spring Boot invalidEmailForLogin() method of UserController");
}
@Test
public void invalidPasswordForLogin() throws Exception {
LOGGER.info("START : Inside Spring Boot invalidPasswordForLogin() method of UserController");
String USER_DATA = "{\"email\":\"kiran@gmail.com\",\"password\":\"12345678\"}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/login").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().isOk()).andExpect(jsonPath("$.authenticated").value("false"));;
LOGGER.info("END : Spring Boot invalidPasswordForLogin() method of UserController");
}
}
package com.cts.skynews.springboot.controller;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.springframework.web.context.WebApplicationContext;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserControllerTest {
private static final Logger LOGGER = LoggerFactory.getLogger(UserControllerTest.class);
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void addNewUser() throws Exception {
LOGGER.info("START : Inside Spring Boot addUser() method of UserController");
String USER_DATA = "{\"name\":\"Kiran Ravariya\"," + "\"email\":\"kirasln@gmail.com\","
+ "\"password\":\"A123456\"," + "\"status\":\"active\"," + "\"language\":{\"id\":\"1\"},"
+ "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().isOk()).andExpect(jsonPath("$.signedUp").value("true"));
LOGGER.info("END : Spring Boot addUser() method of UserController");
}
@Test
public void checkEmailExists() throws Exception {
LOGGER.info("START : Inside Spring Boot checkEmailExists() method of UserController");
String USER_DATA = "{\"name\":\"Kiran Ravariya\"," + "\"email\":\"ravariakiran@gmail.com\","
+ "\"password\":\"A123456\"," + "\"status\":\"active\"," + "\"language\":{\"id\":\"1\"},"
+ "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().isOk()).andExpect(jsonPath("$.emailExists").value("true"))
.andExpect(jsonPath("$.signedUp").value("false"));
LOGGER.info("END : Spring Boot checkEmailExists() method of UserController");
}
@Test
public void incorrectEmailFormat() throws Exception {
LOGGER.info("START : Inside Spring Boot incorrectEmailFormat() method of UserController");
String USER_DATA = "{\"name\":\"Kiran Ravariya\"," + "\"email\":\"rgmail.com\"," + "\"password\":\"A123456\","
+ "\"status\":\"active\"," + "\"language\":{\"id\":\"1\"}," + "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.errorMessage").value("Input Validation Failed:Email address is invalid"));
LOGGER.info("END : Spring Boot incorrectEmailFormat() method of UserController");
}
@Test
public void nullName() throws Exception {
LOGGER.info("START : Inside Spring Boot nullName() method of UserController");
String USER_DATA = "{\"email\":\"abcdefg@gmail.com\"," + "\"password\":\"A123456\"," + "\"status\":\"active\","
+ "\"language\":{\"id\":\"1\"}," + "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.errorMessage").value("Input Validation Failed:Name cannot be empty"));
LOGGER.info("END : Spring Boot nullName() method of UserController");
}
@Test
public void nullPassword() throws Exception {
LOGGER.info("START : Inside Spring Boot nullPassword() method of UserController");
String USER_DATA = "{\"name\":\"Kiran Ravariya\"," + "\"email\":\"abcdefg@gmail.com\","
+ "\"status\":\"active\"," + "\"language\":{\"id\":\"1\"}," + "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.errorMessage").value("Input Validation Failed:Password cannot be empty"));
LOGGER.info("END : Spring Boot nullPassword() method of UserController");
}
@Test
public void nullEmail() throws Exception {
LOGGER.info("START : Inside Spring Boot nullEmail() method of UserController");
String USER_DATA = "{\"name\":\"Kiran Ravariya\"," + "\"password\":\"A123456\"," + "\"status\":\"active\","
+ "\"language\":{\"id\":\"1\"}," + "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.errorMessage").value("Input Validation Failed:Email cannot be empty"));
LOGGER.info("END : Spring Boot nullEmail() method of UserController");
}
@Test
public void nullStatus() throws Exception {
LOGGER.info("START : Inside Spring Boot nullEmail() method of UserController");
String USER_DATA = "{\"name\":\"Kiran Ravariya\"," + "\"email\":\"abcdefg@gmail.com\","
+ "\"password\":\"A123456\"," + "\"language\":{\"id\":\"1\"}," + "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.errorMessage").value("Input Validation Failed:Status cannot be empty"));
LOGGER.info("END : Spring Boot nullStatus() method of UserController");
}
@Test
public void langugaeNull() throws Exception {
LOGGER.info("START : Inside Spring Boot langugaeNull() method of UserController");
String USER_DATA = "{\"name\":\"Kiran Ravariya\"," + "\"email\":\"abcdefg@gmail.com\","
+ "\"password\":\"A123456\"," + "\"status\":\"active\"," + "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.errorMessage").value("Input Validation Failed:Language cannot be empty"));
LOGGER.info("END : Spring Boot langugaeNull() method of UserController");
}
@Test
public void roleNull() throws Exception {
LOGGER.info("START : Inside Spring Boot roleNull() method of UserController");
String USER_DATA = "{\"name\":\"Kiran Ravariya\"," + "\"email\":\"abcdefg@gmail.com\","
+ "\"password\":\"A123456\"," + "\"status\":\"active\"," + "\"language\":{\"id\":\"1\"}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.errorMessage").value("Input Validation Failed:Role cannot be empty"));
LOGGER.info("END : Spring Boot roleNull() method of UserController");
}
@Test
public void invalidNameLength() throws Exception {
LOGGER.info("START : Inside Spring Boot invalidNameLength() method of UserController");
String USER_DATA = "{\"name\":\"KiranKiranRavariyKiranKiranRavariyaRRavariyaRavariyaRavariyaRavariyaRavariya "
+ "KiranKiranRavariyKiranKiranRavariyaRRavariyaRavariyaRavariyaRavariyaRavariya "
+ "KiranKiranRavariyKiranKiranRavariyaRRavariyaRavariyaRavariyaRavariyaRavariya\","
+ "\"email\":\"abcdefg@gmail.com\"," + "\"password\":\"A123456\"," + "\"status\":\"active\","
+ "\"language\":{\"id\":\"1\"}," + "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.errorMessage").value("Input Validation Failed:Name must be 3 to 80 characters"));
LOGGER.info("END : Spring Boot invalidNameLength() method of UserController");
}
@Test
public void invalidEmailLength() throws Exception {
LOGGER.info("START : Inside Spring Boot invalidEmailLength() method of UserController");
String USER_DATA = "{\"name\":\"Kiran Ravariya\","
+ "\"email\":\"ravariakiran@gmailravariakiran@gmailravariakiran@gmailravariakiran@gmailravariakiran@gmailravariakiran@gmailravariakiran@gmailravariakiran@gmailravariakiran@gmailravariakiran@gmail.com\","
+ "\"password\":\"A123456\"," + "\"status\":\"active\"," + "\"language\":{\"id\":\"1\"},"
+ "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().is4xxClientError()).andExpect(
jsonPath("$.errorMessage").value("Input Validation Failed:Email must be 4 to 80 characters"));
LOGGER.info("END : Spring Boot invalidEmailLength() method of UserController");
}
@Test
public void incorrectPasswordFormat() throws Exception {
LOGGER.info("START : Inside Spring Boot incorrectPasswordFormat() method of UserController");
String USER_DATA = "{\"name\":\"Kiran Ravariya\"," + "\"email\":\"abcdefg@gmail.com\"," + "\"password\":\"12\","
+ "\"status\":\"active\"," + "\"language\":{\"id\":\"1\"}," + "\"role\":{\"id\":1}}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/signup").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().is4xxClientError()).andExpect(jsonPath("$.errorMessage")
.value("Input Validation Failed:Password must be 6 to 45 characters"));
LOGGER.info("END : Spring Boot incorrectPasswordFormat() method of UserController");
}
@Test
public void successfullLogin() throws Exception {
LOGGER.info("START : Inside Spring Boot successfullLogin() method of UserController");
String USER_DATA = "{\"email\":\"kiran@gmail.com\",\"password\":\"A123456\"}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/login").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().isOk()).andExpect(jsonPath("$.authenticated").value("true"));
LOGGER.info("END : Spring Boot successfullLogin() method of UserController");
}
@Test
public void invalidEmailForLogin() throws Exception {
LOGGER.info("START : Inside Spring Boot invalidEmailForLogin() method of UserController");
String USER_DATA = "{\"email\":\"kiran123@gmail.com\",\"password\":\"A123456\"}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/login").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().isOk()).andExpect(jsonPath("$.authenticated").value("false"));
LOGGER.info("END : Spring Boot invalidEmailForLogin() method of UserController");
}
@Test
public void invalidPasswordForLogin() throws Exception {
LOGGER.info("START : Inside Spring Boot invalidPasswordForLogin() method of UserController");
String USER_DATA = "{\"email\":\"kiran@gmail.com\",\"password\":\"12345678\"}";
LOGGER.debug("JSON Object : {}", USER_DATA);
mockMvc.perform(post("/user/login").content(USER_DATA).contentType("application/json;charset=UTF-8"))
.andExpect(status().isOk()).andExpect(jsonPath("$.authenticated").value("false"));;
LOGGER.info("END : Spring Boot invalidPasswordForLogin() method of UserController");
}
}