{"id":376418,"date":"2024-05-21T06:50:22","date_gmt":"2024-05-21T06:50:22","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=376418"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-05-21T06:50:22","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=376418","title":{"rendered":"<span>\u0422\u0435\u0441\u0442\u044b \u043d\u0430 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u0430\u0445: \u043f\u0438\u0448\u0435\u043c \u043a\u043e\u0434\u0430 \u0432 3 \u0440\u0430\u0437\u0430 \u043c\u0435\u043d\u044c\u0448\u0435. \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u0430\u0446\u0438\u044f AssertJ \u0438 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 Json<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044e \u0441\u0435\u0440\u0438\u044e \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0439 \u043f\u0440\u043e \u043d\u0430\u0448\u0438 Java-\u043e\u043d\u043b\u0430\u0439\u043d \u043a\u0443\u0440\u0441\u044b. \u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u043f\u043e\u0441\u0442\u044b:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/articles\/807047\/\" rel=\"noopener noreferrer nofollow\">\u041a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b \u043d\u0430 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u0430\u0445: \u043f\u0438\u0448\u0435\u043c \u043a\u043e\u0434\u0430 \u0432 3 \u0440\u0430\u0437\u0430 \u043c\u0435\u043d\u044c\u0448\u0435<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/articles\/777824\/\" rel=\"noopener noreferrer nofollow\">\u041c\u0438\u0433\u0440\u0430\u0446\u0438\u044f Java Spring Boot \u043d\u0430 Kotlin<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/articles\/765332\/\" rel=\"noopener noreferrer nofollow\">\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u0432 Java<\/a><\/p>\n<\/li>\n<\/ul>\n<p>\u0421\u0440\u0430\u0437\u0443 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0443: \u0442\u043e\u0447\u043d\u043e \u0442\u0430\u043a\u00a0\u0436\u0435, \u043a\u0430\u043a\u00a0\u0432 <a href=\"https:\/\/habr.com\/ru\/articles\/807047\/\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430\u0445 \u043d\u0430\u00a0\u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u0430\u0445<\/a> \u0441\u0430\u043c\u0438 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b \u043d\u0435\u00a0\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u0438\u0440\u0443\u044e\u0442\u0441\u044f, \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u041d\u0415\u00a0\u0411\u0423\u0414\u0415\u041c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0430\u043c\u0438 \u043a\u043b\u0430\u0441\u0441\u044b \u0442\u0435\u0441\u0442\u043e\u0432. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435\u00a0\u0441\u043f\u0435\u0448\u0438\u0442\u0435 \u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438, \u043d\u0435\u00a0\u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0432 \u0441\u0442\u0430\u0442\u044c\u0438, \u0447\u0442\u043e\u00a0\u044d\u0442\u043e \u00ab<em>Bad practice<\/em>\u00bb. \u041f\u043e\u00a0\u043f\u043e\u0432\u043e\u0434\u0443 \u0443\u0441\u043b\u043e\u0436\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u043e\u0442\u0432\u0435\u0447\u0443 \u0442\u0430\u043a\u00a0\u0436\u0435, \u043a\u0430\u043a\u00a0\u0438 \u0432\u00a0\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u043a\u00a0\u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u0440\u043e\u00a0\u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b\u00a0\u2014 \u043a\u043e\u0434 \u0442\u0435\u0441\u0442\u043e\u0432 \u0438 \u0438\u0445 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u044f\u0442\u0441\u044f \u043f\u0440\u043e\u0449\u0435, \u0437\u0430\u00a0\u0441\u0447\u0435\u0442 \u0443\u0441\u043b\u043e\u0436\u043d\u0435\u043d\u0438\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 (\u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043d\u0430\u00a0\u044d\u0442\u043e\u043c \u0438 \u0441\u0442\u0440\u043e\u0438\u0442\u0441\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u043e\u0432 \u0438 \u041e\u041e\u041f). \u041c\u043e\u0436\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0435 \u0437\u0434\u0435\u0441\u044c \u043f\u043e\u0434\u0445\u043e\u0434\u044b \u0441\u043b\u043e\u0435\u043c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0438, \u043f\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 <a href=\"https:\/\/www.vogella.com\/tutorials\/AssertJ\/article.html\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 AssertJ<\/a> \u043a\u00a0\u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u044e\u0449\u0438\u0439 \u0435\u0433\u043e \u043d\u0430\u00a0\u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 <em>json<\/em> \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432.<\/p>\n<h3>\u0422\u0435\u0441\u0442\u044b \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432\/\u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u0432<\/h3>\n<p>\u0411\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432, \u043a\u0442\u043e \u043f\u0438\u0441\u0430\u043b \u0442\u0435\u0441\u0442\u044b, \u0432\u0438\u0434\u0435\u043b\u0438\/\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438\u00a0\u0442\u0430\u043a\u0438\u0435 \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u0435 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438\u00a0\u043f\u0440\u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432\/\u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u0432:<\/p>\n<pre><code class=\"java\">@Test void getFromService() {     User actual = service.get(USER_ID);     assertEquals(USER_ID, actual.getId());     assertEquals(user.getName(), actual.getName());     assertEquals(user.getEmail(), actual.getEmail());     assertEquals(user.getCaloriesPerDay(), actual.getCaloriesPerDay());     assertEquals(user.getRoles(), actual.getRoles()); }<\/code><\/pre>\n<p>\u0438\u00a0<a href=\"https:\/\/mkyong.com\/spring-boot\/testing-json-in-spring-boot\/\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u0445\u043e\u0436\u0438\u0435 \u043d\u0430 \u043d\u0438\u0445<\/a>\u00a0\u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0445 \u0438\u0437 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 json \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 c \u043f\u043e\u043c\u043e\u0449\u044c\u044e\u00a0<code>MockMvcResultMatchers.jsonPath<\/code> (\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u043a\u043b\u0430\u0441\u0441\u0430 \u0432\u043d\u0438\u0437\u0443 \u0441\u0442\u0430\u0442\u044c\u0438)<\/p>\n<pre><code class=\"java\">@Test void getFromController() throws Exception {     mockMvc.perform(MockMvcRequestBuilders.get(REST_URL + USER_ID)                     .with(httpBasic(admin.getEmail(), admin.getPassword())))             .andDo(print())             .andExpect(status().isOk())             .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))             .andExpect(jsonPath(\"$.id\").value(USER_ID))             .andExpect(jsonPath(\"$.name\").value(user.getName()))             .andExpect(jsonPath(\"$.email\").value(user.getEmail()))             .andExpect(jsonPath(\"$.caloriesPerDay\").value(user.getCaloriesPerDay()))             .andExpect(jsonPath(\"$.roles\", hasSize(1)))             .andExpect(jsonPath(\"$.roles\", contains(Role.USER.name()))); } <\/code><\/pre>\n<p>\u0418 \u044d\u0442\u043e \u0434\u043b\u044f\u00a0<strong>\u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0431\u0435\u0437 \u0432\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0438 \u0441 \u043e\u0434\u043d\u0438\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0432 \u043a\u043e\u043b\u0435\u043b\u043a\u0446\u0438\u0438 \u0440\u043e\u043b\u0435\u0439<\/strong>! \u041f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u0447\u0430\u0441\u0442\u043e \u043d\u0430 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0412\u0421\u0415\u0425 \u043f\u043e\u043b\u0435\u0439 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0437\u0430\u0431\u0438\u0432\u0430\u0435\u0442\u0441\u044f.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0436\u0438\u0437\u043d\u044c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443, \u043f\u0440\u043e\u0439\u0434\u044f \u043f\u0443\u0442\u044c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0443\u0436\u0435 \u043f\u043e\u0447\u0442\u0438 10 \u043b\u0435\u0442 \u043d\u0430 \u043d\u0430\u0448\u0435\u0439 \u0441\u0442\u0430\u0436\u0438\u0440\u043e\u0432\u043a\u0435 TopJava (Maven\/ Spring\/ Security\/ Spring Boot\/ JPA(Hibernate)\/ Swagger\/OpenAPI 3.0)\/ Rest)<\/p>\n<ol>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442 DTO, \u0442\u043e \u0442\u0435\u0441\u0442\u044b \u043c\u043e\u0436\u043d\u043e \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c, \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0432 \u0432 \u043d\u0435\u043c\u00a0<code>equals\/hasCode<\/code>\u00a0(\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0447\u0435\u0440\u0435\u0437 Lombok\u00a0<code>@EqualsAndHashCode(callSuper = true)<\/code>)<\/p>\n<pre><code class=\"java\">@Test void getToFromService() {     UserTo actualTo = service.getTo(USER_ID);     assertEquals(userTo, actualTo); } <\/code><\/pre>\n<\/li>\n<li>\n<p>\u0414\u043b\u044f \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u043e\u0432 \u0432\u0441\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435: \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u0443\u0442\u0438\u043b\u044c\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f Json \u0432 \u043e\u0431\u044a\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0437\u0430\u0442\u0435\u043c \u043c\u043e\u0436\u0435\u043c \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c:<\/p>\n<pre><code class=\"java\">public class JsonUtil {     public static &lt;T> T readValue(String json, Class&lt;T> clazz) throws IOException {         return getObjectMapper().readValue(json, clazz);     }     ... } @Test void getToFromController() throws Exception {     MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get(REST_URL_TO + USER_ID)        .with(httpBasic(admin.getEmail(), admin.getPassword())))        .andExpect(...)        .andReturn();     String json = result.getResponse().getContentAsString();     User actual = JsonUtil.readValue(json, User.class);     assertEquals(user, actual); } <\/code><\/pre>\n<p>\u0418\u043b\u0438 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c:<\/p>\n<pre><code class=\"java\">@Test void getToFromController() throws Exception {     mockMvc.perform(MockMvcRequestBuilders.get(REST_URL_TO + USER_ID)        .with(httpBasic(admin.getEmail(), admin.getPassword())))        .andExpect(...)        .andExpect(result -> assertEquals(user,             JsonUtil.readValue(result.getResponse().getContentAsString(), User.class))); } <\/code><\/pre>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f Entity, \u0438\u00a0<a href=\"https:\/\/stackoverflow.com\/questions\/1638723\/548473\" rel=\"noopener noreferrer nofollow\">\u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c equals\/hasCode\u00a0\u043f\u043e \u0432\u0441\u0435\u043c \u043f\u043e\u043b\u044f\u043c \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c<\/a>, \u043c\u043e\u0436\u043d\u043e \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u043a\u043e\u0434 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e\u00a0<a href=\"https:\/\/www.vogella.com\/tutorials\/AssertJ\/article.html\" rel=\"noopener noreferrer nofollow\">\u043c\u043e\u0449\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 AssertJ<\/a>.<br \/><strong>\u0411\u0443\u0434\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u044b<\/strong>: \u0432 Junit \u043c\u0435\u0442\u043e\u0434\u0430\u0445 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432\u00a0(expected, actual), \u0430 \u0432 \u043c\u0435\u0442\u043e\u0434\u0430\u0445 AssertJ \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442\u00a0(actual,expected).<\/p>\n<pre><code class=\"java\">@Test void getEntityFromService() {     User actual = service.get(USER_ID);     assertThat(actual).usingRecursiveComparison().ignoringFields(\"registered\", \"password\").isEqualTo(user); } <\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f, \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043d\u0441\u0442\u0438 \u0435\u0435 \u0432 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0443 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043e \u0432\u0441\u0435\u0445 \u0442\u0435\u0441\u0442\u0430\u0445:<\/p>\n<pre><code class=\"java\">public static BiConsumer&lt;User, User> USER_MATCHER =        (a, e) -> assertThat(a).usingRecursiveComparison().ignoringFields(\"registered\", \"password\").isEqualTo(e)    @Test void getEntityFromService() {     User actual = service.get(USER_ID);     USER_MATCHER.accept(actual, user); }<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c \u044d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u043a \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430\u043c:<\/p>\n<pre><code class=\"java\">@Test void getEntityFromController() {     mockMvc.perform(MockMvcRequestBuilders.get(REST_URL_TO + USER_ID)        .with(httpBasic(admin.getEmail(), admin.getPassword())))        .andExpect(...)        .andExpect(result -> USER_MATCHER.accept(                 JsonUtil.readValue(result.getResponse().getContentAsString(), User.class), user)); } <\/code><\/pre>\n<\/li>\n<li>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u043d\u0430\u0448 \u043f\u043e\u0434\u0445\u043e\u0434 \u043d\u0430 \u0432\u0441\u0435 \u0441\u043b\u0443\u0447\u0430\u0438:<\/p>\n<ul>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0432\u00a0<code>JsonUtil<\/code>\u00a0\u043c\u0435\u0442\u043e\u0434 \u0447\u0442\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u0438 \u0437\u0430\u043f\u0438\u0441\u0438 \u0432 json (<a href=\"https:\/\/github.com\/JavaOPs\/bootjava\/blob\/patched\/src\/main\/java\/ru\/javaops\/bootjava\/util\/JsonUtil.java\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u0434 JsonUtil<\/a>)<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0431\u0449\u0438\u0439 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0442\u0435\u0441\u0442\u043e\u0432 (<a href=\"https:\/\/github.com\/JavaOPs\/bootjava\/blob\/patched\/src\/test\/java\/ru\/javaops\/bootjava\/web\/AbstractControllerTest.java\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u0434 AbstractControllerTest<\/a>)<\/p>\n<\/li>\n<li>\n<p>\u0421\u0434\u0435\u043b\u0430\u0435\u043c \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c AssertJ (<a href=\"https:\/\/github.com\/JavaOPs\/bootjava\/blob\/patched\/src\/test\/java\/ru\/javaops\/bootjava\/web\/MatcherFactory.java\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u0434 MatcherFactory<\/a>)<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043c\u0430\u0442\u0447\u0435\u0440\u00a0<code>UserTestData.USER_MATCHER<\/code>\u00a0(<a href=\"https:\/\/github.com\/JavaOPs\/bootjava\/blob\/patched\/src\/test\/java\/ru\/javaops\/bootjava\/web\/user\/UserTestData.java\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u0434 UserTestData<\/a>)<\/p>\n<\/li>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439\u00a0<code>USER_MATCHER<\/code>\u00a0\u0432 \u0442\u0435\u0441\u0442\u0430\u0445 (<a href=\"https:\/\/github.com\/JavaOPs\/bootjava\/blob\/patched\/src\/test\/java\/ru\/javaops\/bootjava\/web\/user\/AdminUserControllerTest.java\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u0434 AdminUserControllerTest<\/a>\u00a0\u0438\u00a0<a href=\"https:\/\/github.com\/JavaOPs\/bootjava\/blob\/patched\/src\/test\/java\/ru\/javaops\/bootjava\/web\/user\/ProfileControllerTest.java\" rel=\"noopener noreferrer nofollow\">ProfileControllerTest<\/a>)<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u0412 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u00a0<code>JsonUtil<\/code>\u00a0\u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f json \u0442\u0435\u043b\u0430\u00a0<em>POST<\/em>\u00a0\u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0430 \u043f\u043e\u043b\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u044b \u043a\u0430\u043a\u00a0<code><a class=\"mention\" href=\"\/users\/JsonIgnore\">@JsonIgnore<\/a><\/code>\u00a0\u0438\u043b\u0438\u00a0<code><a class=\"mention\" href=\"\/users\/JsonProperty\">@JsonProperty<\/a>(access = Access.WRITE_ONLY)<\/code>, \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0445\u0430\u043a &#8212; \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u044d\u0442\u0438 \u043f\u043e\u043b\u044f \u0432\u0440\u0443\u0447\u043d\u0443\u044e (<a href=\"https:\/\/github.com\/JavaOPs\/bootjava\/blob\/patched\/src\/main\/java\/ru\/javaops\/bootjava\/util\/JsonUtil.java#L46\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u0434 JsonUtil.writeAdditionProps<\/a>\u00a0\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432\u00a0<a href=\"https:\/\/github.com\/JavaOPs\/bootjava\/blob\/patched\/src\/test\/java\/ru\/javaops\/bootjava\/web\/user\/AdminUserControllerTest.java#L107\" rel=\"noopener noreferrer nofollow\">AdminUserControllerTest.update<\/a>)<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043f\u0440\u043e\u0435\u043a\u0442 \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0438 \u0442\u0435\u0441\u0442\u043e\u0432 \u043c\u043d\u043e\u0433\u043e, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0432\u044b\u0438\u0433\u0440\u044b\u0448 \u0434\u0430\u0436\u0435 \u043d\u0435 3-X, \u0430 \u043d\u0430 \u043f\u043e\u0440\u044f\u0434\u043e\u043a! <br \/>\u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0443\u0445\u043e\u0434\u0438\u0442 \u0432\u0441\u0435 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u043e\u0448\u0438\u0431\u043e\u043a \u0442\u0430\u043a\u0436\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043d\u0430 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043c\u0435\u043d\u044c\u0448\u0435.<\/p>\n<p>\u041d\u0430\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u043a \u0442\u0440\u0430\u0434\u0438\u0446\u0438\u043e\u043d\u043d\u043e: \u043f\u0440\u0438\u044f\u0442\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u043d\u0430 \u043d\u0430\u0448\u0438 \u043a\u0443\u0440\u0441\u044b.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/815151\/\"> https:\/\/habr.com\/ru\/articles\/815151\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044e \u0441\u0435\u0440\u0438\u044e \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0439 \u043f\u0440\u043e \u043d\u0430\u0448\u0438 Java-\u043e\u043d\u043b\u0430\u0439\u043d \u043a\u0443\u0440\u0441\u044b. \u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u043f\u043e\u0441\u0442\u044b:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/articles\/807047\/\" rel=\"noopener noreferrer nofollow\">\u041a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b \u043d\u0430 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u0430\u0445: \u043f\u0438\u0448\u0435\u043c \u043a\u043e\u0434\u0430 \u0432 3 \u0440\u0430\u0437\u0430 \u043c\u0435\u043d\u044c\u0448\u0435<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/articles\/777824\/\" rel=\"noopener noreferrer nofollow\">\u041c\u0438\u0433\u0440\u0430\u0446\u0438\u044f Java Spring Boot \u043d\u0430 Kotlin<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/articles\/765332\/\" rel=\"noopener noreferrer nofollow\">\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u0432 Java<\/a><\/p>\n<\/li>\n<\/ul>\n<p>\u0421\u0440\u0430\u0437\u0443 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0443: \u0442\u043e\u0447\u043d\u043e \u0442\u0430\u043a\u00a0\u0436\u0435, \u043a\u0430\u043a\u00a0\u0432 <a href=\"https:\/\/habr.com\/ru\/articles\/807047\/\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430\u0445 \u043d\u0430\u00a0\u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u0430\u0445<\/a> \u0441\u0430\u043c\u0438 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b \u043d\u0435\u00a0\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u0438\u0440\u0443\u044e\u0442\u0441\u044f, \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u041d\u0415\u00a0\u0411\u0423\u0414\u0415\u041c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0430\u043c\u0438 \u043a\u043b\u0430\u0441\u0441\u044b \u0442\u0435\u0441\u0442\u043e\u0432. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435\u00a0\u0441\u043f\u0435\u0448\u0438\u0442\u0435 \u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438, \u043d\u0435\u00a0\u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0432 \u0441\u0442\u0430\u0442\u044c\u0438, \u0447\u0442\u043e\u00a0\u044d\u0442\u043e \u00ab<em>Bad practice<\/em>\u00bb. \u041f\u043e\u00a0\u043f\u043e\u0432\u043e\u0434\u0443 \u0443\u0441\u043b\u043e\u0436\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u043e\u0442\u0432\u0435\u0447\u0443 \u0442\u0430\u043a\u00a0\u0436\u0435, \u043a\u0430\u043a\u00a0\u0438 \u0432\u00a0\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u043a\u00a0\u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u0440\u043e\u00a0\u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b\u00a0\u2014 \u043a\u043e\u0434 \u0442\u0435\u0441\u0442\u043e\u0432 \u0438 \u0438\u0445 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u044f\u0442\u0441\u044f \u043f\u0440\u043e\u0449\u0435, \u0437\u0430\u00a0\u0441\u0447\u0435\u0442 \u0443\u0441\u043b\u043e\u0436\u043d\u0435\u043d\u0438\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 (\u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043d\u0430\u00a0\u044d\u0442\u043e\u043c \u0438 \u0441\u0442\u0440\u043e\u0438\u0442\u0441\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u043e\u0432 \u0438 \u041e\u041e\u041f). \u041c\u043e\u0436\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0435 \u0437\u0434\u0435\u0441\u044c \u043f\u043e\u0434\u0445\u043e\u0434\u044b \u0441\u043b\u043e\u0435\u043c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0438, \u043f\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 <a href=\"https:\/\/www.vogella.com\/tutorials\/AssertJ\/article.html\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 AssertJ<\/a> \u043a\u00a0\u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u044e\u0449\u0438\u0439 \u0435\u0433\u043e \u043d\u0430\u00a0\u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 <em>json<\/em> \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432.<\/p>\n<h3>\u0422\u0435\u0441\u0442\u044b \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432\/\u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u0432<\/h3>\n<p>\u0411\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432, \u043a\u0442\u043e \u043f\u0438\u0441\u0430\u043b \u0442\u0435\u0441\u0442\u044b, \u0432\u0438\u0434\u0435\u043b\u0438\/\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438\u00a0\u0442\u0430\u043a\u0438\u0435 \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u0435 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438\u00a0\u043f\u0440\u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432\/\u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u0432:<\/p>\n<pre><code class=\"java\">@Test void getFromService() {     User actual = service.get(USER_ID);     assertEquals(USER_ID, actual.getId());     assertEquals(user.getName(), actual.getName());     assertEquals(user.getEmail(), actual.getEmail());     assertEquals(user.getCaloriesPerDay(), actual.getCaloriesPerDay());     assertEquals(user.getRoles(), actual.getRoles()); }<\/code><\/pre>\n<p>\u0438\u00a0<a href=\"https:\/\/mkyong.com\/spring-boot\/testing-json-in-spring-boot\/\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u0445\u043e\u0436\u0438\u0435 \u043d\u0430 \u043d\u0438\u0445<\/a>\u00a0\u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0445 \u0438\u0437 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 json \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 c \u043f\u043e\u043c\u043e\u0449\u044c\u044e\u00a0<code>MockMvcResultMatchers.jsonPath<\/code> (\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u043a\u043b\u0430\u0441\u0441\u0430 \u0432\u043d\u0438\u0437\u0443 \u0441\u0442\u0430\u0442\u044c\u0438)<\/p>\n<pre><code class=\"java\">@Test void getFromController() throws Exception {     mockMvc.perform(MockMvcRequestBuilders.get(REST_URL + USER_ID)                     .with(httpBasic(admin.getEmail(), admin.getPassword())))             .andDo(print())             .andExpect(status().isOk())             .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))             .andExpect(jsonPath(\"$.id\").value(USER_ID))             .andExpect(jsonPath(\"$.name\").value(user.getName()))             .andExpect(jsonPath(\"$.email\").value(user.getEmail()))             .andExpect(jsonPath(\"$.caloriesPerDay\").value(user.getCaloriesPerDay()))             .andExpect(jsonPath(\"$.roles\", hasSize(1)))             .andExpect(jsonPath(\"$.roles\", contains(Role.USER.name()))); } <\/code><\/pre>\n<p>\u0418 \u044d\u0442\u043e \u0434\u043b\u044f\u00a0<strong>\u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0431\u0435\u0437 \u0432\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0438 \u0441 \u043e\u0434\u043d\u0438\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0432 \u043a\u043e\u043b\u0435\u043b\u043a\u0446\u0438\u0438 \u0440\u043e\u043b\u0435\u0439<\/strong>! \u041f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u0447\u0430\u0441\u0442\u043e \u043d\u0430 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0412\u0421\u0415\u0425 \u043f\u043e\u043b\u0435\u0439 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0437\u0430\u0431\u0438\u0432\u0430\u0435\u0442\u0441\u044f.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0436\u0438\u0437\u043d\u044c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443, \u043f\u0440\u043e\u0439\u0434\u044f \u043f\u0443\u0442\u044c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0443\u0436\u0435 \u043f\u043e\u0447\u0442\u0438 10 \u043b\u0435\u0442 \u043d\u0430 \u043d\u0430\u0448\u0435\u0439 \u0441\u0442\u0430\u0436\u0438\u0440\u043e\u0432\u043a\u0435 TopJava (Maven\/ Spring\/ Security\/ Spring Boot\/ JPA(Hibernate)\/ Swagger\/OpenAPI 3.0)\/ Rest)<\/p>\n<ol>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442 DTO, \u0442\u043e \u0442\u0435\u0441\u0442\u044b \u043c\u043e\u0436\u043d\u043e \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c, \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0432 \u0432 \u043d\u0435\u043c\u00a0<code>equals\/hasCode<\/code>\u00a0(\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0447\u0435\u0440\u0435\u0437 Lombok\u00a0<code>@EqualsAndHashCode(callSuper = true)<\/code>)<\/p>\n<pre><code class=\"java\">@Test void getToFromService() {     UserTo actualTo = service.getTo(USER_ID);     assertEquals(userTo, actualTo); } <\/code><\/pre>\n<\/li>\n<li>\n<p>\u0414\u043b\u044f \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u043e\u0432 \u0432\u0441\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435: \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u0443\u0442\u0438\u043b\u044c\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f Json \u0432 \u043e\u0431\u044a\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0437\u0430\u0442\u0435\u043c \u043c\u043e\u0436\u0435\u043c \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c:<\/p>\n<pre><code class=\"java\">public class JsonUtil {     public static &lt;T> T readValue(String json, Class&lt;T> clazz) throws IOException {         return getObjectMapper().readValue(json, clazz);     }     ... } @Test void getToFromController() throws Exception {     MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get(REST_URL_TO + USER_ID)        .with(httpBasic(admin.getEmail(), admin.getPassword())))        .andExpect(...)        .andReturn();     String json = result.getResponse().getContentAsString();     User actual = JsonUtil.readValue(json, User.class);     assertEquals(user, actual); } <\/code><\/pre>\n<p>\u0418\u043b\u0438 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c:<\/p>\n<pre><code class=\"java\">@Test void getToFromController() throws Exception {     mockMvc.perform(MockMvcRequestBuilders.get(REST_URL_TO + USER_ID)        .with(httpBasic(admin.getEmail(), admin.getPassword())))        .andExpect(...)        .andExpect(result -> assertEquals(user,             JsonUtil.readValue(result.getResponse().getContentAsString(), User.class))); } <\/code><\/pre>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f Entity, \u0438\u00a0<a href=\"https:\/\/stackoverflow.com\/questions\/1638723\/548473\" rel=\"noopener noreferrer nofollow\">\u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c equals\/hasCode\u00a0\u043f\u043e \u0432\u0441\u0435\u043c \u043f\u043e\u043b\u044f\u043c \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c<\/a>, \u043c\u043e\u0436\u043d\u043e \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u043a\u043e\u0434 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e\u00a0<a href=\"https:\/\/www.vogella.com\/tutorials\/AssertJ\/article.html\" rel=\"noopener noreferrer nofollow\">\u043c\u043e\u0449\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 AssertJ<\/a>.<br \/><strong>\u0411\u0443\u0434\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u044b<\/strong>: \u0432 Junit \u043c\u0435\u0442\u043e\u0434\u0430\u0445 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432\u00a0(expected, actual), \u0430 \u0432 \u043c\u0435\u0442\u043e\u0434\u0430\u0445 AssertJ \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442\u00a0(actual,expected).<\/p>\n<pre><code class=\"java\">@Test void getEntityFromService() {     User actual = service.get(USER_ID);     assertThat(actual).usingRecursiveComparison().ignoringFields(\"registered\", \"password\").isEqualTo(user); } <\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f, \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043d\u0441\u0442\u0438 \u0435\u0435 \u0432 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0443 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043e \u0432\u0441\u0435\u0445 \u0442\u0435\u0441\u0442\u0430\u0445:<\/p>\n<pre><code class=\"java\">public static BiConsumer&lt;User, User> USER_MATCHER =        (a, e) -> assertThat(a).usingRecursiveComparison().ignoringFields(\"registered\", \"password\").isEqualTo(e)    @Test void getEntityFromService() {     User actual = service.get(USER_ID);     USER_MATCHER.accept(actual, user); }<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c \u044d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u043a \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430\u043c:<\/p>\n<pre><code class=\"java\">@Test void getEntityFromController() {     mockMvc.perform(MockMvcRequestBuilders.get(REST_URL_TO + USER_ID)        .with(httpBasic(admin.getEmail(), admin.getPassword())))        .andExpect(...)        .andExpect(result -> USER_MATCHER.accept(                 JsonUtil.readValue(result.getResponse().getContentAsString(), User.class), user)); } <\/code><\/pre>\n<\/li>\n<li>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u043d\u0430\u0448 \u043f\u043e\u0434\u0445\u043e\u0434 \u043d\u0430 \u0432\u0441\u0435 \u0441\u043b\u0443\u0447\u0430\u0438:<\/p>\n<ul>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0432\u00a0<code>JsonUtil<\/code>\u00a0\u043c\u0435\u0442\u043e\u0434 \u0447\u0442\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u0438 \u0437\u0430\u043f\u0438\u0441\u0438 \u0432 json (<a href=\"https:\/\/github.com\/JavaOPs\/bootjava\/blob\/patched\/src\/main\/java\/ru\/javaops\/bootjava\/util\/JsonUtil.java\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u0434 JsonUtil<\/a>)<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0431\u0449\u0438\u0439 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0442\u0435\u0441\u0442\u043e\u0432 (<a href=\"https:\/\/github.com\/JavaOPs\/bootjava\/blob\/patched\/src\/test\/java\/ru\/javaops\/bootjava\/web\/AbstractControllerTest.java\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u0434 AbstractControllerTest<\/a>)<\/p>\n<\/li>\n<li>\n<p>\u0421\u0434\u0435\u043b\u0430\u0435\u043c \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c AssertJ (<a href=\"https:\/\/github.com\/JavaOPs\/bootjava\/blob\/patched\/src\/test\/java\/ru\/javaops\/bootjava\/web\/MatcherFactory.java\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u0434 MatcherFactory<\/a>)<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043c\u0430\u0442\u0447\u0435\u0440\u00a0<code>UserTestData.USER_MATCHER<\/code>\u00a0(<a href=\"https:\/\/github.com\/JavaOPs\/bootjava\/blob\/patched\/src\/test\/java\/ru\/javaops\/bootjava\/web\/user\/UserTestData.java\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u0434 UserTestData<\/a>)<\/p>\n<\/li>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439\u00a0<code>USER_MATCHER<\/code>\u00a0\u0432 \u0442\u0435\u0441\u0442\u0430\u0445 (<a href=\"https:\/\/github.com\/JavaOPs\/bootjava\/blob\/patched\/src\/test\/java\/ru\/javaops\/bootjava\/web\/user\/AdminUserControllerTest.java\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u0434 AdminUserControllerTest<\/a>\u00a0\u0438\u00a0<a href=\"https:\/\/github.com\/JavaOPs\/bootjava\/blob\/patched\/src\/test\/java\/ru\/javaops\/bootjava\/web\/user\/ProfileControllerTest.java\" rel=\"noopener noreferrer nofollow\">ProfileControllerTest<\/a>)<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u0412 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u00a0<code>JsonUtil<\/code>\u00a0\u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f json \u0442\u0435\u043b\u0430\u00a0<em>POST<\/em>\u00a0\u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0430 \u043f\u043e\u043b\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u044b \u043a\u0430\u043a\u00a0<code><a class=\"mention\" href=\"\/users\/JsonIgnore\">@JsonIgnore<\/a><\/code>\u00a0\u0438\u043b\u0438\u00a0<code><a class=\"mention\" href=\"\/users\/JsonProperty\">@JsonProperty<\/a>(access = Access.WRITE_ONLY)<\/code>, \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0445\u0430\u043a &#8212; \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u044d\u0442\u0438 \u043f\u043e\u043b\u044f \u0432\u0440\u0443\u0447\u043d\u0443\u044e (<a href=\"https:\/\/github.com\/JavaOPs\/bootjava\/blob\/patched\/src\/main\/java\/ru\/javaops\/bootjava\/util\/JsonUtil.java#L46\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u0434 JsonUtil.writeAdditionProps<\/a>\u00a0\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432\u00a0<a href=\"https:\/\/github.com\/JavaOPs\/bootjava\/blob\/patched\/src\/test\/java\/ru\/javaops\/bootjava\/web\/user\/AdminUserControllerTest.java#L107\" rel=\"noopener noreferrer nofollow\">AdminUserControllerTest.update<\/a>)<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043f\u0440\u043e\u0435\u043a\u0442 \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0438 \u0442\u0435\u0441\u0442\u043e\u0432 \u043c\u043d\u043e\u0433\u043e, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0432\u044b\u0438\u0433\u0440\u044b\u0448 \u0434\u0430\u0436\u0435 \u043d\u0435 3-X, \u0430 \u043d\u0430 \u043f\u043e\u0440\u044f\u0434\u043e\u043a! <br \/>\u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0443\u0445\u043e\u0434\u0438\u0442 \u0432\u0441\u0435 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u043e\u0448\u0438\u0431\u043e\u043a \u0442\u0430\u043a\u0436\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043d\u0430 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043c\u0435\u043d\u044c\u0448\u0435.<\/p>\n<p>\u041d\u0430\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u043a \u0442\u0440\u0430\u0434\u0438\u0446\u0438\u043e\u043d\u043d\u043e: \u043f\u0440\u0438\u044f\u0442\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u043d\u0430 \u043d\u0430\u0448\u0438 \u043a\u0443\u0440\u0441\u044b.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/815151\/\"> https:\/\/habr.com\/ru\/articles\/815151\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-376418","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/376418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=376418"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/376418\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=376418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=376418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=376418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}