{"id":350222,"date":"2023-07-13T15:00:50","date_gmt":"2023-07-13T15:00:50","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=350222"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=350222","title":{"rendered":"<span>\u0424\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 SpringBoot<\/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<div class=\"persona\" persona=\"true\"><img decoding=\"async\" persona=\"true\" class=\"image persona__image\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/c5b\/204\/3b8\/c5b2043b898c0ab77bffc6f9f47836eb.jpg\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c5b\/204\/3b8\/c5b2043b898c0ab77bffc6f9f47836eb.jpg\" data-blurred=\"true\"\/><\/p>\n<h5 class=\"persona__heading\" persona=\"true\">\u0410\u0432\u0442\u043e\u0440 \u0441\u0442\u0430\u0442\u044c\u0438: \u0420\u0443\u0441\u0442\u0435\u043c \u0413\u0430\u043b\u0438\u0435\u0432<\/h5>\n<p>IBM Senior DevOps Engineer &amp; Integration Architect. \u041e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 DevOps \u043c\u0435\u043d\u0442\u043e\u0440 \u0438 \u043a\u043e\u0443\u0447 \u0432 IBM  <\/p>\n<\/div>\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442, \u0432 <a href=\"https:\/\/habr.com\/ru\/companies\/otus\/articles\/746414\/\">\u043f\u0440\u043e\u0448\u043b\u044b\u0439 \u0440\u0430\u0437 \u043c\u044b \u0433\u043e\u0432\u043e\u0440\u0438\u043b\u0438 \u043f\u0440\u043e \u044e\u043d\u0438\u0442 \u0442\u0435\u0441\u0442\u044b \u0441 MockMVC<\/a>, \u0441\u0435\u0433\u043e\u0434\u043d\u044f \u0436\u0435 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0442\u0435\u0441\u0442\u044b \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0433\u043e \u043a\u043e\u0434\u0430.<\/p>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c. \u0418\u0437 \u043f\u0440\u0435\u0440\u0435\u043a\u0432\u0438\u0437\u0438\u0442\u043e\u0432 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043a\u043e\u0434 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f <code>BookController.java<\/code>.<\/p>\n<pre><code class=\"java\">package books;  import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*;  import java.util.List;  @RestController @RequestMapping(\"\/books\") public class BookController {  private final BookService bookService;  public BookController(BookService bookService) {     this.bookService = bookService; }  @GetMapping public List&lt;Book> findAll() {     return bookService.findAll(); }  @GetMapping(\"\/{id}\") public Book findOne(@PathVariable int id) {     return bookService.findOne(id); }  @PostMapping @ResponseStatus(HttpStatus.CREATED) public Book create(@RequestBody Book book) {     return bookService.create(book); }  } <\/code><\/pre>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043e\u0441\u0440\u0435\u0434\u043e\u0442\u043e\u0447\u0438\u043c\u0441\u044f \u043d\u0430 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0438 \u043a\u043b\u0430\u0441\u0441\u0430. \u042d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u043f\u043e\u043c\u0435\u0447\u0435\u043d \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0435\u0439 <code>@RestController<\/code>, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u044d\u0442\u043e \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u044b. \u041a\u043b\u0430\u0441\u0441 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441 \u043f\u0443\u0442\u0435\u043c <code>\/books<\/code> \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 <code>@RequestMapping<\/code>  :<\/p>\n<pre><code class=\"java\">@RestController @RequestMapping(\"\/books\") public class BookController {     \/\/ ... }<\/code><\/pre>\n<p>\u042d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 BookService \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f Spring \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430. <code>BookService.java<\/code> :<\/p>\n<pre><code class=\"java\">package books;  import org.springframework.stereotype.Service;  import javax.annotation.PostConstruct; import java.util.ArrayList; import java.util.Arrays; import java.util.List;  @Service public class BookService {  private List&lt;Book> books = new ArrayList&lt;>();  public List&lt;Book> findAll() {     return books; }  public Book findOne(int id) {     return books.stream().filter(book -> book.getId() == id).findFirst().orElseThrow(BookNotFoundException::new); }  public Book create(Book book) {     books.add(book);     return book; }  \/**  * This method will be called once after the bean was initialized and add some seed data to the books list.  *\/ @PostConstruct private void loadBooks() {     Book one = new Book(1,             \"97 Things Every Java Programmer Should Know\",             \"Kevlin Henney, Trisha Gee\",             \"OReilly Media, Inc.\",             \"May 2020\",             \"9781491952696\",             \"Java\");     Book two = new Book(2,             \"Spring Boot: Up and Running\",             \"Mark Heckler\",             \"OReilly Media, Inc.\",             \"February 2021\",             \"9781492076919\",             \"Spring\");     Book three = new Book(3,             \"Hacking with Spring Boot 2.3: Reactive Edition\",             \"Greg L. Turnquist\",             \"Amazon.com Services LLC\",             \"May 2020\",             \"B086722L4L\",             \"Spring\");      books.addAll(Arrays.asList(one,two,three)); }  }<\/code><\/pre>\n<p>\u041a\u043b\u0430\u0441\u0441 BookService \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0442\u0440\u0438 \u043a\u043d\u0438\u0433\u0438 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0438\u0445 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435. \u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 <code>findAll()<\/code>, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0438\u0439 \u0432\u0441\u0435 \u043a\u043d\u0438\u0433\u0438 \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438, \u043c\u0435\u0442\u043e\u0434 <code>findOne()<\/code>, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0438\u0439 \u043e\u0434\u043d\u0443 \u043a\u043d\u0438\u0433\u0443, \u0438 \u043c\u0435\u0442\u043e\u0434 <code>create()<\/code>, \u0441\u043e\u0437\u0434\u0430\u044e\u0449\u0438\u0439 \u043d\u043e\u0432\u0443\u044e \u043a\u043d\u0438\u0433\u0443 \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u0435\u0435 \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e:<\/p>\n<pre><code class=\"java\">private final BookService bookService;  public BookController(BookService bookService) {     this.bookService = bookService; }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c <code>book.java<\/code><\/p>\n<pre><code class=\"java\">package com.oreilly.functionalbooks;  public class Book {  private int id; private String title; private String author; private String publisher; private String releaseDate; private String isbn; private String topic;  public Book() { }  public Book(int id, String title, String author, String publisher, String releaseDate, String isbn, String topic) {     this.id = id;     this.title = title;     this.author = author;     this.publisher = publisher;     this.releaseDate = releaseDate;     this.isbn = isbn;     this.topic = topic; }  public int getId() {     return id; }  public void setId(int id) {     this.id = id; }  public String getTitle() {     return title; }  public void setTitle(String title) {     this.title = title; }  public String getAuthor() {     return author; }  public void setAuthor(String author) {     this.author = author; }  public String getPublisher() {     return publisher; }  public void setPublisher(String publisher) {     this.publisher = publisher; }  public String getReleaseDate() {     return releaseDate; }  public void setReleaseDate(String releaseDate) {     this.releaseDate = releaseDate; }  public String getIsbn() {     return isbn; }  public void setIsbn(String isbn) {     this.isbn = isbn; }  public String getTopic() {     return topic; }  public void setTopic(String topic) {     this.topic = topic; }  @Override public String toString() {     return \"Book{\" +             \"id=\" + id +             \", title='\" + title + '\\'' +             \", author='\" + author + '\\'' +             '}'; }  }<\/code><\/pre>\n<p>\u043c\u043e\u0434\u0435\u043b\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043a\u043d\u0438\u0433\u0438:<\/p>\n<pre><code class=\"java\">public class Book {      private int id;     private String title;     private String author;     private String publisher;     private String releaseDate;     private String isbn;     private String topic;      \/\/ constructors     \/\/ getters and setters }<\/code><\/pre>\n<p>\u0421\u043f\u0435\u0440\u0432\u0430 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043a\u043e\u0434, \u0447\u0442\u043e\u0431\u044b \u0443\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f \u0447\u0442\u043e \u0432\u0441\u0435 \u0440\u0430\u0431\u043e\u0442f\u0435\u0442<\/p>\n<p><code>mvn spring-boot:run<\/code><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e9c\/04e\/35b\/e9c04e35b2271c8e9063424d1df311c2.png\" width=\"1243\" height=\"193\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e9c\/04e\/35b\/e9c04e35b2271c8e9063424d1df311c2.png\"\/><\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/152\/459\/ee7\/152459ee728d9c40bcb054392d8c8244.png\" width=\"1250\" height=\"112\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/152\/459\/ee7\/152459ee728d9c40bcb054392d8c8244.png\"\/><\/figure>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u043e\u0432\u0443\u044e \u043a\u043d\u0438\u0433\u0443 \u0438 \u0432\u0435\u0440\u043d\u0435\u043c \u0435\u0451:<\/p>\n<pre><code class=\"java\">curl --location --request POST 'http:\/\/localhost:8080\/books' \\ --header 'Content-Type: application\/json' \\ --data-raw '{ \"id\": 4, \"title\": \"Fundamentals of Software Architecture: An Engineering Approach\", \"author\": \"Mark Richards, Neal Ford\", \"publisher\": \"Upfront Books\", \"releaseDate\": \"Feb 2021\", \"isbn\": \"B08X8H15BW\", \"topic\": \"Architecture\" }'<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/01a\/02d\/a81\/01a02da81cc9f7d1c99ff36b24ea5d9a.png\" width=\"1272\" height=\"350\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/01a\/02d\/a81\/01a02da81cc9f7d1c99ff36b24ea5d9a.png\"\/><\/figure>\n<p>\u0412\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u0442\u0435\u0441\u0442\u043e\u0432:<\/p>\n<p>\u0410\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e <code>@SpringBootTest<\/code> \u043c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0432 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0435\u043c \u0442\u0435\u0441\u0442\u044b \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 Spring Boot. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u044d\u0442\u043e\u0439 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043f\u043e\u043c\u0438\u043c\u043e \u043e\u0431\u044b\u0447\u043d\u043e\u0439 Spring TestContext Framework. <code>@SpringBootTest<\/code>:<\/p>\n<ul>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 <code>SpringBootContextLoader<\/code> \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0437\u0430\u0433\u0440\u0443\u0437\u0447\u0438\u043a\u0430 <code>ContextLoader<\/code> \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0430 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0430\u044f <code>@ContextConfiguration(loader=...)<\/code> :<\/p>\n<\/li>\n<li>\n<p>\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0438\u0449\u0435\u0442 <code>@SpringBootConfiguration<\/code>, \u043a\u043e\u0433\u0434\u0430 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u0430\u044f <code>@Configuration<\/code> \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0438 \u043d\u0435 \u0443\u043a\u0430\u0437\u0430\u043d\u044b \u044f\u0432\u043d\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b.<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0441\u0440\u0435\u0434\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430 <code>properties<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430 <code>args<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u041e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0440\u0435\u0436\u0438\u043c\u043e\u0432 <code>webEnvironment<\/code>, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0435\u0433\u043e \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u044e\u0449\u0435\u0433\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u0438\u043b\u0438 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u043f\u043e\u0440\u0442.<\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 bean-\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 <code>TestRestTemplate<\/code> \u0438\/\u0438\u043b\u0438 <code>WebTestClient<\/code> \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u0432\u0435\u0431-\u0442\u0435\u0441\u0442\u0430\u0445, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0445 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0438\u0439 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440.<\/p>\n<\/li>\n<\/ul>\n<p>\u041c\u044b \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c\u0441\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e <code>@SpringBootTest<\/code> \u043a \u043a\u043b\u0430\u0441\u0441\u0443 <code>BookControllerTest<\/code> \u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u044c Spring \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0438\u0439 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0440\u0442\u0443.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0430\u0439\u043b <code>BookControllerTest.java<\/code> \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043a\u043e\u0434\u043e\u043c:<\/p>\n<pre><code class=\"java\">package com.oreilly.functionalbooks;  import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.*;  import java.util.List;  import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue;  \/\/ spring-boot-test public class BookControllerTest {  \/\/ test-rest-template \/\/ test-find-all \/\/ test-find-all-exchange \/\/ test-find-one \/\/ test-find-one-invalid \/\/ test-create  }<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043a\u043b\u0430\u0441\u0441 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e:<\/p>\n<p><code>@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)<\/code><\/p>\n<p>\u042d\u0442\u0430 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f \u0434\u0430\u0441\u0442 \u043d\u0430\u043c \u043c\u043d\u043e\u0433\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439 \u043f\u043e\u043c\u0438\u043c\u043e \u043e\u0431\u044b\u0447\u043d\u043e\u0439 Spring TestContext Framework. \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442 <code>webEnvironment<\/code> \u043a\u0430\u043a <code>RANDOM_PORT<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442 Spring \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0438\u0439 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0430 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u043e\u0440\u0442\u0443.<\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 <code>@SpringBootTest<\/code> \u043a \u043a\u043b\u0430\u0441\u0441\u0443 \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 bean-\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 <code>TestRestTemplate<\/code> \u0438 <code>@Autowired<\/code> \u0432 \u0432\u0430\u0448 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441. \u041a\u043b\u0430\u0441\u0441 <code>TestRestTemplate<\/code> \u2014 \u0443\u0434\u043e\u0431\u043d\u0430\u044f \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0430 RestTemplate, \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0430\u044f \u0434\u043b\u044f \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0442\u0435\u0441\u0442\u043e\u0432.<\/p>\n<p>\u00a0\u0415\u0441\u043b\u0438 \u0432\u044b \u043d\u043e\u0432\u0438\u0447\u043e\u043a \u0432 RestTemplate, \u0437\u043d\u0430\u0439\u0442\u0435, \u0447\u0442\u043e \u044d\u0442\u043e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043b\u0438\u0435\u043d\u0442 \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f HTTP-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0441 \u043f\u0440\u043e\u0441\u0442\u044b\u043c API, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0442\u0440\u043e\u0438\u0442\u0441\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0438\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a HTTP, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a JDK HttpURLConnection, Apache HttpComponents \u0438 \u0434\u0440\u0443\u0433\u0438\u0435.<\/p>\n<p>\u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>TestRestTemplate<\/code>, \u0443\u0434\u0435\u043b\u0438\u0442\u0435 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044e API. \u0412\u043e\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0445 API, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b \u0443\u0432\u0438\u0434\u0438\u0442\u0435 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u044d\u0442\u0430\u043f\u0430\u0445 \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0437\u0430\u043d\u044f\u0442\u0438\u044f.<\/p>\n<ul>\n<li>\n<p><code>getForEntity<\/code><\/p>\n<\/li>\n<li>\n<p><code>getForObject<\/code><\/p>\n<\/li>\n<li>\n<p><code>postForEntity<\/code><\/p>\n<\/li>\n<li>\n<p><code>exchange<\/code><\/p>\n<\/li>\n<\/ul>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 <code>TestRestTemplate<\/code>, \u043d\u0430\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u0442\u044c \u0435\u0433\u043e \u0443 Spring, \u043e\u0431\u044a\u044f\u0432\u0438\u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e <code>@Autowired<\/code>. \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043a\u043e\u0434 \u0432 <code>BookControllerTest<\/code>:<\/p>\n<pre><code class=\"java\">@Autowired     TestRestTemplate template;<\/code><\/pre>\n<p>\u0418\u043c\u044f \u044d\u0442\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u2014 <code>template<\/code>, \u0438 \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u043d\u0435\u0439 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0448\u0430\u0433\u0430\u0445.<\/p>\n<p>\u041f\u0440\u0438\u0448\u043b\u043e \u0432\u0440\u0435\u043c\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430\u0448 \u043f\u0435\u0440\u0432\u044b\u0439 \u0442\u0435\u0441\u0442, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0432\u044b \u0431\u0443\u0434\u0435\u0442\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c API <code>findAll()<\/code> \u0432 <code>BookController<\/code>:<\/p>\n<pre><code class=\"java\">@Test     void shouldReturnAllBooks() {         ResponseEntity&lt;Book[]> entity = template.getForEntity(\"\/books\", Book[].class);          assertEquals(HttpStatus.OK,entity.getStatusCode());         assertEquals(MediaType.APPLICATION_JSON,entity.getHeaders().getContentType());          Book[] books = entity.getBody();         assertTrue(books.length >= 3);         assertEquals(\"97 Things Every Java Programmer Should Know\",books[0].getTitle());         assertEquals(\"Spring Boot: Up and Running\",books[1].getTitle());         assertEquals(\"Hacking with Spring Boot 2.3: Reactive Edition\",books[2].getTitle());     } <\/code><\/pre>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u044d\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>getForEntity()<\/code> <code>TestRestTemplate<\/code>, \u0447\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 GET \u043a \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u0435 <code>\/books<\/code> \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u043a\u043d\u0438\u0433:<\/p>\n<pre><code class=\"java\">ResponseEntity&lt;Book[]> entity = template.getForEntity(\"\/books\", Book[].class);<\/code><\/pre>\n<p>\u041a\u043e\u0433\u0434\u0430 \u044f \u0432\u043f\u0435\u0440\u0432\u044b\u0435 \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0441 \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439, \u043f\u043e\u0434\u0443\u043c\u0430\u043b: \u00ab\u041c\u0435\u0442\u043e\u0434 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code>List&lt;Book><\/code>. \u041f\u043e\u0447\u0435\u043c\u0443 \u043c\u044b \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0442\u0438\u043f <code>Book[]<\/code>?\u00bb \u041a\u043e\u0440\u043e\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u0442\u0438\u043f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <code>List<\/code>. \u0412\u0441\u0435, \u0447\u0442\u043e Spring \u0437\u043d\u0430\u0435\u0442, \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u0441\u043f\u0438\u0441\u043e\u043a, \u043d\u043e \u0443 \u043d\u0435\u0433\u043e \u043d\u0435\u0442 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u043d\u0430 \u0442\u0438\u043f:<\/p>\n<pre><code class=\"java\">\/\/ This will not work ResponseEntity&lt;List&lt;Book>> entity = template.getForEntity(\"\/books\", List.class);<\/code><\/pre>\n<p>\u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e Spring \u0434\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 JSON \u0432 <code>LinkedHashMap<\/code>, \u0438 \u044d\u0442\u043e \u043d\u0435 \u0434\u0430\u0435\u0442 \u043d\u0430\u043c \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043c\u044b \u0445\u043e\u0442\u0438\u043c. \u041c\u044b \u0443\u0437\u043d\u0430\u043b\u0438 \u043e \u043c\u0435\u0442\u043e\u0434\u0435 <code>exchange()<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0442\u0438\u043f, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u043d\u0430\u043c \u0432\u0435\u0440\u043d\u0443\u0442\u044c <code>List&lt;Book><\/code>:<\/p>\n<pre><code class=\"java\">@Test     void shouldReturnAllBooksUsingExchange() {         HttpHeaders headers = new HttpHeaders();         headers.setContentType(MediaType.APPLICATION_JSON);         ResponseEntity&lt;List&lt;Book>> entity = template.exchange(\"\/books\", HttpMethod.GET, new HttpEntity&lt;>(headers), new ParameterizedTypeReference&lt;List&lt;Book>>() {});          assertEquals(HttpStatus.OK,entity.getStatusCode());         assertEquals(MediaType.APPLICATION_JSON,entity.getHeaders().getContentType());          List&lt;Book> books = entity.getBody();         assertTrue(books.size() >= 3);     }<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u0442\u0435\u0441\u0442<\/p>\n<p><code>mvn test<\/code><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e51\/4e8\/a70\/e514e8a70409764635f22e75a5120607.png\" width=\"1268\" height=\"311\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e51\/4e8\/a70\/e514e8a70409764635f22e75a5120607.png\"\/><\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/db9\/d0d\/207\/db9d0d2074c6a92b5d046a8604d4d16c.png\" width=\"789\" height=\"323\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/db9\/d0d\/207\/db9d0d2074c6a92b5d046a8604d4d16c.png\"\/><\/figure>\n<p>\u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u043c\u0435\u0442\u043e\u0434 <code>getForEntity()<\/code> <code>TestRestTemplate<\/code> \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043a\u043d\u0438\u0433. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>getForObject()<\/code> \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0439 \u043a\u043d\u0438\u0433\u0438.<\/p>\n<pre><code class=\"java\">@Test     void shouldReturnAValidBook() {         Book book = template.getForObject(\"\/books\/1\", Book.class);         assertEquals(1,book.getId());         assertEquals(\"97 Things Every Java Programmer Should Know\",book.getTitle());         assertEquals(\"Kevlin Henney, Trisha Gee\",book.getAuthor());         assertEquals(\"OReilly Media, Inc.\",book.getPublisher());         assertEquals(\"May 2020\", book.getReleaseDate());         assertEquals(\"9781491952696\",book.getIsbn());         assertEquals(\"Java\",book.getTopic());     }<\/code><\/pre>\n<p><code>getForObject()<\/code> \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443, \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u044f URI \u0438 ResponseType \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044f \u044d\u0442\u043e\u0442 \u043e\u0431\u044a\u0435\u043a\u0442. \u0412 \u043a\u0430\u0436\u0434\u043e\u043c \u0438\u0437 \u0443\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u0439 \u043c\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0447\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f\u043c.<\/p>\n<p>\u041c\u044b \u0442\u0430\u043a\u0436\u0435 \u0445\u043e\u0442\u0438\u043c \u0443\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u043a\u043e\u0433\u0434\u0430 \u0437\u0430\u043f\u0440\u043e\u0441 GET \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043d\u0435\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u043a\u043d\u0438\u0433\u0438, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434 \u043e\u0442\u0432\u0435\u0442\u0430, 404 Not Found:<\/p>\n<pre><code class=\"java\">@Test     void invalidBookIdShouldReturn404() {         ResponseEntity&lt;Book> entity = template.getForEntity(\"\/books\/99\", Book.class);         assertEquals(HttpStatus.NOT_FOUND, entity.getStatusCode());     }<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0442\u0435\u0441\u0442<\/p>\n<p><code>mvn test<\/code><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/f7b\/752\/478\/f7b752478f994f6966433499a23e16a6.png\" width=\"1084\" height=\"319\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f7b\/752\/478\/f7b752478f994f6966433499a23e16a6.png\"\/><\/figure>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0448\u0430\u0433\u0435 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0435\u0441\u0442, \u0447\u0442\u043e\u0431\u044b \u0443\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u0443\u044e \u043a\u043d\u0438\u0433\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0430 \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e:<\/p>\n<pre><code class=\"java\">@Test     void shouldCreateNewBook() {         Book book = new Book(4,                 \"Fundamentals of Software Architecture: An Engineering Approach\",                 \"Mark Richards, Neal Ford\",                 \"Upfront Books\",                 \"Feb 2021\",                 \"B08X8H15BW\",                 \"Architecture\");         ResponseEntity&lt;Book> entity = template.postForEntity(\"\/books\", book, Book.class);         assertEquals(HttpStatus.CREATED,entity.getStatusCode());          Book created = entity.getBody();         assertEquals(4,created.getId());         assertEquals(\"Fundamentals of Software Architecture: An Engineering Approach\",created.getTitle());         assertEquals(\"Mark Richards, Neal Ford\",created.getAuthor());         assertEquals(\"Upfront Books\",created.getPublisher());         assertEquals(\"Feb 2021\", created.getReleaseDate());         assertEquals(\"B08X8H15BW\",created.getIsbn());         assertEquals(\"Architecture\",created.getTopic());         assertEquals(4,template.getForObject(\"\/books\", List.class).stream().count());     } <\/code><\/pre>\n<p><code>PostForEntity()<\/code> <code>TestRestTemplate<\/code> \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c URI, \u043e\u0431\u044a\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0442\u0435\u043b\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0438 ResponseType. \u041f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043d\u043e\u0432\u043e\u0433\u043e \u0440\u0435\u0441\u0443\u0440\u0441\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f 201 Created, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e:<\/p>\n<pre><code class=\"java\">assertEquals(HttpStatus.CREATED,entity.getStatusCode());<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u043a\u0443\u0447\u0430 \u0443\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u0439 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0432\u043d\u043e\u0432\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0439 \u043a\u043d\u0438\u0433\u0438. \u0412 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u044d\u0442\u043e \u0431\u044b\u043b\u043e \u043d\u0435 \u0442\u0430\u043a \u0443\u0436 \u043f\u043e\u043b\u0435\u0437\u043d\u043e, \u043d\u043e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0441\u0435\u0431\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u0443, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u043a\u043d\u0438\u0433\u0438 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0430 \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442\u0441\u044f.<\/p>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0432\u044b\u0437\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043a\u043d\u0438\u0433\u0438, \u0447\u0442\u043e\u0431\u044b \u0443\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u043d\u0430 \u043a\u043d\u0438\u0433\u0430:<\/p>\n<pre><code class=\"python\">assertEquals(4,template.getForObject(\"\/books\", List.class).stream().count());<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u0442\u0435\u0441\u0442<\/p>\n<p><code>mvn test<\/code><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/8c6\/d05\/e28\/8c6d05e285704301eaeede0e4a7cadb0.png\" width=\"1200\" height=\"317\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8c6\/d05\/e28\/8c6d05e285704301eaeede0e4a7cadb0.png\"\/><\/figure>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u043d\u0430\u0448 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b \u043f\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0445 \u0442\u0435\u0441\u0442\u043e\u0432 \u0432 Spring Boot \u043f\u043e\u0434\u043e\u0448\u0435\u043b \u043a \u043a\u043e\u043d\u0446\u0443. \u041c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d \u0432 \u043f\u0440\u0435\u0434\u0434\u0432\u0435\u0440\u0438\u0438 \u0441\u0442\u0430\u0440\u0442\u0430 \u043a\u0443\u0440\u0441\u0430 &#171;<a href=\"https:\/\/otus.pw\/o9Gq\/\">\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0447\u0438\u043a \u043d\u0430 Spring Framework<\/a>&#171;.<\/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\/companies\/otus\/articles\/747852\/\"> https:\/\/habr.com\/ru\/companies\/otus\/articles\/747852\/<\/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<div class=\"persona\" persona=\"true\">\n<h5 class=\"persona__heading\" persona=\"true\">\u0410\u0432\u0442\u043e\u0440 \u0441\u0442\u0430\u0442\u044c\u0438: \u0420\u0443\u0441\u0442\u0435\u043c \u0413\u0430\u043b\u0438\u0435\u0432<\/h5>\n<p>IBM Senior DevOps Engineer &amp; Integration Architect. \u041e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 DevOps \u043c\u0435\u043d\u0442\u043e\u0440 \u0438 \u043a\u043e\u0443\u0447 \u0432 IBM  <\/p>\n<\/div>\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442, \u0432 <a href=\"https:\/\/habr.com\/ru\/companies\/otus\/articles\/746414\/\">\u043f\u0440\u043e\u0448\u043b\u044b\u0439 \u0440\u0430\u0437 \u043c\u044b \u0433\u043e\u0432\u043e\u0440\u0438\u043b\u0438 \u043f\u0440\u043e \u044e\u043d\u0438\u0442 \u0442\u0435\u0441\u0442\u044b \u0441 MockMVC<\/a>, \u0441\u0435\u0433\u043e\u0434\u043d\u044f \u0436\u0435 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0442\u0435\u0441\u0442\u044b \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0433\u043e \u043a\u043e\u0434\u0430.<\/p>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c. \u0418\u0437 \u043f\u0440\u0435\u0440\u0435\u043a\u0432\u0438\u0437\u0438\u0442\u043e\u0432 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043a\u043e\u0434 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f <code>BookController.java<\/code>.<\/p>\n<pre><code class=\"java\">package books;  import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*;  import java.util.List;  @RestController @RequestMapping(\"\/books\") public class BookController {  private final BookService bookService;  public BookController(BookService bookService) {     this.bookService = bookService; }  @GetMapping public List&lt;Book> findAll() {     return bookService.findAll(); }  @GetMapping(\"\/{id}\") public Book findOne(@PathVariable int id) {     return bookService.findOne(id); }  @PostMapping @ResponseStatus(HttpStatus.CREATED) public Book create(@RequestBody Book book) {     return bookService.create(book); }  } <\/code><\/pre>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043e\u0441\u0440\u0435\u0434\u043e\u0442\u043e\u0447\u0438\u043c\u0441\u044f \u043d\u0430 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0438 \u043a\u043b\u0430\u0441\u0441\u0430. \u042d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u043f\u043e\u043c\u0435\u0447\u0435\u043d \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0435\u0439 <code>@RestController<\/code>, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u044d\u0442\u043e \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u044b. \u041a\u043b\u0430\u0441\u0441 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441 \u043f\u0443\u0442\u0435\u043c <code>\/books<\/code> \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 <code>@RequestMapping<\/code>  :<\/p>\n<pre><code class=\"java\">@RestController @RequestMapping(\"\/books\") public class BookController {     \/\/ ... }<\/code><\/pre>\n<p>\u042d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 BookService \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f Spring \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430. <code>BookService.java<\/code> :<\/p>\n<pre><code class=\"java\">package books;  import org.springframework.stereotype.Service;  import javax.annotation.PostConstruct; import java.util.ArrayList; import java.util.Arrays; import java.util.List;  @Service public class BookService {  private List&lt;Book> books = new ArrayList&lt;>();  public List&lt;Book> findAll() {     return books; }  public Book findOne(int id) {     return books.stream().filter(book -> book.getId() == id).findFirst().orElseThrow(BookNotFoundException::new); }  public Book create(Book book) {     books.add(book);     return book; }  \/**  * This method will be called once after the bean was initialized and add some seed data to the books list.  *\/ @PostConstruct private void loadBooks() {     Book one = new Book(1,             \"97 Things Every Java Programmer Should Know\",             \"Kevlin Henney, Trisha Gee\",             \"OReilly Media, Inc.\",             \"May 2020\",             \"9781491952696\",             \"Java\");     Book two = new Book(2,             \"Spring Boot: Up and Running\",             \"Mark Heckler\",             \"OReilly Media, Inc.\",             \"February 2021\",             \"9781492076919\",             \"Spring\");     Book three = new Book(3,             \"Hacking with Spring Boot 2.3: Reactive Edition\",             \"Greg L. Turnquist\",             \"Amazon.com Services LLC\",             \"May 2020\",             \"B086722L4L\",             \"Spring\");      books.addAll(Arrays.asList(one,two,three)); }  }<\/code><\/pre>\n<p>\u041a\u043b\u0430\u0441\u0441 BookService \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0442\u0440\u0438 \u043a\u043d\u0438\u0433\u0438 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0438\u0445 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435. \u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 <code>findAll()<\/code>, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0438\u0439 \u0432\u0441\u0435 \u043a\u043d\u0438\u0433\u0438 \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438, \u043c\u0435\u0442\u043e\u0434 <code>findOne()<\/code>, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0438\u0439 \u043e\u0434\u043d\u0443 \u043a\u043d\u0438\u0433\u0443, \u0438 \u043c\u0435\u0442\u043e\u0434 <code>create()<\/code>, \u0441\u043e\u0437\u0434\u0430\u044e\u0449\u0438\u0439 \u043d\u043e\u0432\u0443\u044e \u043a\u043d\u0438\u0433\u0443 \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u0435\u0435 \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e:<\/p>\n<pre><code class=\"java\">private final BookService bookService;  public BookController(BookService bookService) {     this.bookService = bookService; }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c <code>book.java<\/code><\/p>\n<pre><code class=\"java\">package com.oreilly.functionalbooks;  public class Book {  private int id; private String title; private String author; private String publisher; private String releaseDate; private String isbn; private String topic;  public Book() { }  public Book(int id, String title, String author, String publisher, String releaseDate, String isbn, String topic) {     this.id = id;     this.title = title;     this.author = author;     this.publisher = publisher;     this.releaseDate = releaseDate;     this.isbn = isbn;     this.topic = topic; }  public int getId() {     return id; }  public void setId(int id) {     this.id = id; }  public String getTitle() {     return title; }  public void setTitle(String title) {     this.title = title; }  public String getAuthor() {     return author; }  public void setAuthor(String author) {     this.author = author; }  public String getPublisher() {     return publisher; }  public void setPublisher(String publisher) {     this.publisher = publisher; }  public String getReleaseDate() {     return releaseDate; }  public void setReleaseDate(String releaseDate) {     this.releaseDate = releaseDate; }  public String getIsbn() {     return isbn; }  public void setIsbn(String isbn) {     this.isbn = isbn; }  public String getTopic() {     return topic; }  public void setTopic(String topic) {     this.topic = topic; }  @Override public String toString() {     return \"Book{\" +             \"id=\" + id +             \", title='\" + title + '\\'' +             \", author='\" + author + '\\'' +             '}'; }  }<\/code><\/pre>\n<p>\u043c\u043e\u0434\u0435\u043b\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043a\u043d\u0438\u0433\u0438:<\/p>\n<pre><code class=\"java\">public class Book {      private int id;     private String title;     private String author;     private String publisher;     private String releaseDate;     private String isbn;     private String topic;      \/\/ constructors     \/\/ getters and setters }<\/code><\/pre>\n<p>\u0421\u043f\u0435\u0440\u0432\u0430 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043a\u043e\u0434, \u0447\u0442\u043e\u0431\u044b \u0443\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f \u0447\u0442\u043e \u0432\u0441\u0435 \u0440\u0430\u0431\u043e\u0442f\u0435\u0442<\/p>\n<p><code>mvn spring-boot:run<\/code><\/p>\n<figure class=\"full-width\"><\/figure>\n<figure class=\"full-width\"><\/figure>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u043e\u0432\u0443\u044e \u043a\u043d\u0438\u0433\u0443 \u0438 \u0432\u0435\u0440\u043d\u0435\u043c \u0435\u0451:<\/p>\n<pre><code class=\"java\">curl --location --request POST 'http:\/\/localhost:8080\/books' \\ --header 'Content-Type: application\/json' \\ --data-raw '{ \"id\": 4, \"title\": \"Fundamentals of Software Architecture: An Engineering Approach\", \"author\": \"Mark Richards, Neal Ford\", \"publisher\": \"Upfront Books\", \"releaseDate\": \"Feb 2021\", \"isbn\": \"B08X8H15BW\", \"topic\": \"Architecture\" }'<\/code><\/pre>\n<figure class=\"full-width\"><\/figure>\n<p>\u0412\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u0442\u0435\u0441\u0442\u043e\u0432:<\/p>\n<p>\u0410\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e <code>@SpringBootTest<\/code> \u043c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0432 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0435\u043c \u0442\u0435\u0441\u0442\u044b \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 Spring Boot. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u044d\u0442\u043e\u0439 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043f\u043e\u043c\u0438\u043c\u043e \u043e\u0431\u044b\u0447\u043d\u043e\u0439 Spring TestContext Framework. <code>@SpringBootTest<\/code>:<\/p>\n<ul>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 <code>SpringBootContextLoader<\/code> \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0437\u0430\u0433\u0440\u0443\u0437\u0447\u0438\u043a\u0430 <code>ContextLoader<\/code> \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0430 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0430\u044f <code>@ContextConfiguration(loader=...)<\/code> :<\/p>\n<\/li>\n<li>\n<p>\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0438\u0449\u0435\u0442 <code>@SpringBootConfiguration<\/code>, \u043a\u043e\u0433\u0434\u0430 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u0430\u044f <code>@Configuration<\/code> \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0438 \u043d\u0435 \u0443\u043a\u0430\u0437\u0430\u043d\u044b \u044f\u0432\u043d\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b.<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0441\u0440\u0435\u0434\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430 <code>properties<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430 <code>args<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u041e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0440\u0435\u0436\u0438\u043c\u043e\u0432 <code>webEnvironment<\/code>, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0435\u0433\u043e \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u043f\u0440\u043e\u0441\u043b\u0443\u0448\u0438\u0432\u0430\u044e\u0449\u0435\u0433\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u0438\u043b\u0438 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u043f\u043e\u0440\u0442.<\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 bean-\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 <code>TestRestTemplate<\/code> \u0438\/\u0438\u043b\u0438 <code>WebTestClient<\/code> \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u0432\u0435\u0431-\u0442\u0435\u0441\u0442\u0430\u0445, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0445 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0438\u0439 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440.<\/p>\n<\/li>\n<\/ul>\n<p>\u041c\u044b \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c\u0441\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e <code>@SpringBootTest<\/code> \u043a \u043a\u043b\u0430\u0441\u0441\u0443 <code>BookControllerTest<\/code> \u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u044c Spring \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0438\u0439 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0440\u0442\u0443.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0430\u0439\u043b <code>BookControllerTest.java<\/code> \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043a\u043e\u0434\u043e\u043c:<\/p>\n<pre><code class=\"java\">package com.oreilly.functionalbooks;  import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.*;  import java.util.List;  import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue;  \/\/ spring-boot-test public class BookControllerTest {  \/\/ test-rest-template \/\/ test-find-all \/\/ test-find-all-exchange \/\/ test-find-one \/\/ test-find-one-invalid \/\/ test-create  }<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043a\u043b\u0430\u0441\u0441 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e:<\/p>\n<p><code>@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)<\/code><\/p>\n<p>\u042d\u0442\u0430 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f \u0434\u0430\u0441\u0442 \u043d\u0430\u043c \u043c\u043d\u043e\u0433\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439 \u043f\u043e\u043c\u0438\u043c\u043e \u043e\u0431\u044b\u0447\u043d\u043e\u0439 Spring TestContext Framework. \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442 <code>webEnvironment<\/code> \u043a\u0430\u043a <code>RANDOM_PORT<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442 Spring \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0438\u0439 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0430 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u043e\u0440\u0442\u0443.<\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 <code>@SpringBootTest<\/code> \u043a \u043a\u043b\u0430\u0441\u0441\u0443 \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 bean-\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 <code>TestRestTemplate<\/code> \u0438 <code>@Autowired<\/code> \u0432 \u0432\u0430\u0448 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441. \u041a\u043b\u0430\u0441\u0441 <code>TestRestTemplate<\/code> \u2014 \u0443\u0434\u043e\u0431\u043d\u0430\u044f \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0430 RestTemplate, \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0430\u044f \u0434\u043b\u044f \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0442\u0435\u0441\u0442\u043e\u0432.<\/p>\n<p>\u00a0\u0415\u0441\u043b\u0438 \u0432\u044b \u043d\u043e\u0432\u0438\u0447\u043e\u043a \u0432 RestTemplate, \u0437\u043d\u0430\u0439\u0442\u0435, \u0447\u0442\u043e \u044d\u0442\u043e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043b\u0438\u0435\u043d\u0442 \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f HTTP-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0441 \u043f\u0440\u043e\u0441\u0442\u044b\u043c API, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0442\u0440\u043e\u0438\u0442\u0441\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0438\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a HTTP, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a JDK HttpURLConnection, Apache HttpComponents \u0438 \u0434\u0440\u0443\u0433\u0438\u0435.<\/p>\n<p>\u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>TestRestTemplate<\/code>, \u0443\u0434\u0435\u043b\u0438\u0442\u0435 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044e API. \u0412\u043e\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0445 API, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b \u0443\u0432\u0438\u0434\u0438\u0442\u0435 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u044d\u0442\u0430\u043f\u0430\u0445 \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0437\u0430\u043d\u044f\u0442\u0438\u044f.<\/p>\n<ul>\n<li>\n<p><code>getForEntity<\/code><\/p>\n<\/li>\n<li>\n<p><code>getForObject<\/code><\/p>\n<\/li>\n<li>\n<p><code>postForEntity<\/code><\/p>\n<\/li>\n<li>\n<p><code>exchange<\/code><\/p>\n<\/li>\n<\/ul>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 <code>TestRestTemplate<\/code>, \u043d\u0430\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u0442\u044c \u0435\u0433\u043e \u0443 Spring, \u043e\u0431\u044a\u044f\u0432\u0438\u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e <code>@Autowired<\/code>. \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043a\u043e\u0434 \u0432 <code>BookControllerTest<\/code>:<\/p>\n<pre><code class=\"java\">@Autowired     TestRestTemplate template;<\/code><\/pre>\n<p>\u0418\u043c\u044f \u044d\u0442\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u2014 <code>template<\/code>, \u0438 \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u043d\u0435\u0439 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0448\u0430\u0433\u0430\u0445.<\/p>\n<p>\u041f\u0440\u0438\u0448\u043b\u043e \u0432\u0440\u0435\u043c\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430\u0448 \u043f\u0435\u0440\u0432\u044b\u0439 \u0442\u0435\u0441\u0442, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0432\u044b \u0431\u0443\u0434\u0435\u0442\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c API <code>findAll()<\/code> \u0432 <code>BookController<\/code>:<\/p>\n<pre><code class=\"java\">@Test     void shouldReturnAllBooks() {         ResponseEntity&lt;Book[]> entity = template.getForEntity(\"\/books\", Book[].class);          assertEquals(HttpStatus.OK,entity.getStatusCode());         assertEquals(MediaType.APPLICATION_JSON,entity.getHeaders().getContentType());          Book[] books = entity.getBody();         assertTrue(books.length >= 3);         assertEquals(\"97 Things Every Java Programmer Should Know\",books[0].getTitle());         assertEquals(\"Spring Boot: Up and Running\",books[1].getTitle());         assertEquals(\"Hacking with Spring Boot 2.3: Reactive Edition\",books[2].getTitle());     } <\/code><\/pre>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u044d\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>getForEntity()<\/code> <code>TestRestTemplate<\/code>, \u0447\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 GET \u043a \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u0435 <code>\/books<\/code> \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u043a\u043d\u0438\u0433:<\/p>\n<pre><code class=\"java\">ResponseEntity&lt;Book[]> entity = template.getForEntity(\"\/books\", Book[].class);<\/code><\/pre>\n<p>\u041a\u043e\u0433\u0434\u0430 \u044f \u0432\u043f\u0435\u0440\u0432\u044b\u0435 \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0441 \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439, \u043f\u043e\u0434\u0443\u043c\u0430\u043b: \u00ab\u041c\u0435\u0442\u043e\u0434 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code>List&lt;Book><\/code>. \u041f\u043e\u0447\u0435\u043c\u0443 \u043c\u044b \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0442\u0438\u043f <code>Book[]<\/code>?\u00bb \u041a\u043e\u0440\u043e\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u0442\u0438\u043f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <code>List<\/code>. \u0412\u0441\u0435, \u0447\u0442\u043e Spring \u0437\u043d\u0430\u0435\u0442,<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\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-350222","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/350222","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=350222"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/350222\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=350222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=350222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=350222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}