{"id":171911,"date":"2013-03-12T13:39:04","date_gmt":"2013-03-12T09:39:04","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=171911"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=171911","title":{"rendered":"<span class=\"post_title\">\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u043e\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e MockMvc<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442.<\/p>\n<p>  \u041f\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0432\u0448\u0438\u0441\u044c \u0441 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439 MockMvc, \u044f \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u043b &quot;<i>\u043f\u043e\u043b\u043d\u043e\u0435 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f<\/i>&quot; \u0435\u0451 \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u043d\u0438\u0439 \u043d\u0430 \u0425\u0430\u0431\u0440\u0435. \u041f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u0432\u043e\u0441\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u0440\u043e\u0431\u0435\u043b, \u0442\u0435\u043c \u0431\u043e\u043b\u0435\u0435, \u0447\u0442\u043e \u043d\u0430\u0448\u0435 <a href=\"http:\/\/habrahabr.ru\/post\/145158\">\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 ContactManager<\/a> \u043a\u0430\u043a \u0440\u0430\u0437 \u043d\u0443\u0436\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u0418\u0442\u0430\u043a, \u043e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0442\u0435\u043c\u0430 \u0443\u0440\u043e\u043a\u0430 \u2014 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0442\u0435\u0441\u0442\u044b \u0434\u043b\u044f \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u043e\u0432. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0431\u043e\u043d\u0443\u0441\u0430 \u043c\u044b \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u044d\u0442\u043e \u043f\u043e \u043c\u043e\u0434\u043d\u043e\u0439, <a href=\"http:\/\/habrahabr.ru\/post\/112488\">\u00ab\u0431\u0435\u0437-xml-\u043d\u043e\u0439\u00bb<\/a> \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438. <\/p>\n<h5>\u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u043f\u0440\u043e\u0435\u043a\u0442\u0430<\/h5>\n<p>  \u0412\u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0431\u043d\u043e\u0432\u0438\u043c \u0432\u0435\u0440\u0441\u0438\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a. SpringFramework \u043a \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u043c\u0443 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u043b\u0441\u044f \u0443\u0436\u0435 \u0434\u043e \u0432\u0435\u0440\u0441\u0438\u0438 3.2.1, \u0438 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u044f \u0432\u043e\u0436\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 MockMvc, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u0430\u043d\u043d\u043e\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e. Spring Security \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043e\u0442\u0441\u0442\u0430\u0435\u0442, \u043d\u043e \u044d\u0442\u043e (\u043f\u043e\u0447\u0442\u0438) \u043d\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430. \u0412\u0435\u0440\u0441\u0438\u044f Hibernate \u0442\u043e\u0436\u0435 \u043f\u043e\u0434\u0440\u043e\u0441\u043b\u0430 \u0434\u043e 4.1.9.Final. \u041f\u043e\u043b\u043d\u044b\u0439 \u0444\u0430\u0439\u043b \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0432\u044b \u043d\u0430\u0439\u0434\u0435\u0442\u0435 <a href=\"https:\/\/github.com\/monzdrpower\/ContactManager-MockMvc\/blob\/master\/pom.xml\">\u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438<\/a> (\u0441\u0441\u044b\u043b\u043a\u0430 \u0432 \u043a\u043e\u043d\u0446\u0435 \u0441\u0442\u0430\u0442\u044c\u0438).<\/p>\n<p>  \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u043d\u0430 4 \u0432\u0435\u0440\u0441\u0438\u044e Hibernate \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0444\u0430\u0439\u043b\u0430 <code>data.xml<\/code>. \u041d\u0443\u0436\u043d\u043e \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c 3 \u043d\u0430 4 \u0432 \u0438\u043c\u0435\u043d\u0438 \u043f\u0430\u043a\u0435\u0442\u0430 <code>org.springframework.orm.hibernate4<\/code>, \u0432 \u0431\u0438\u043d\u0435 <code>sessionFactory<\/code> \u0443\u0431\u0440\u0430\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b <code>configLocation<\/code> \u0438 <code>configurationClass<\/code> \u0438 \u0432\u043c\u0435\u0441\u0442\u043e \u043d\u0438\u0445 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>packagesToScan<\/code>, \u043a\u0443\u0434\u0430 \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0430\u043a\u0435\u0442\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0441 Hibernate-\u043c\u0430\u043f\u043f\u0438\u043d\u0433\u043e\u043c \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 <code>hibernate.cfg.xml<\/code>. \u0421\u0430\u043c \u044d\u0442\u043e\u0442 \u0444\u0430\u0439\u043b \u043c\u043e\u0436\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c, \u043e\u043d \u043d\u0430\u043c \u0443\u0436\u0435 \u043d\u0435 \u043d\u0443\u0436\u0435\u043d. \u0412 \u0438\u0442\u043e\u0433\u0435 \u0431\u0438\u043d <code>sessionfactory<\/code> \u043f\u0440\u0438\u043c\u0435\u0442 \u0432\u0438\u0434:  <\/p>\n<pre><code class=\"xml\">\t&lt;bean id=&quot;sessionFactory&quot; \t\tclass=&quot;org.springframework.orm.hibernate4.LocalSessionFactoryBean&quot;&gt; \t\t&lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot; \/&gt; \t\t&lt;property name=&quot;packagesToScan&quot;&gt; \t\t\t&lt;list&gt; \t\t\t\t&lt;value&gt;net.schastny.contactmanager.domain&lt;\/value&gt; \t\t\t\t&lt;value&gt;com.acme.contactmanager.domain&lt;\/value&gt; \t\t\t&lt;\/list&gt; \t\t&lt;\/property&gt;  \t\t&lt;property name=&quot;hibernateProperties&quot;&gt; \t\t\t&lt;props&gt; \t\t\t\t&lt;prop key=&quot;hibernate.show_sql&quot;&gt;true&lt;\/prop&gt; \t\t\t\t&lt;prop key=&quot;hibernate.hbm2ddl.auto&quot;&gt;create-drop&lt;\/prop&gt; \t\t\t\t&lt;prop key=&quot;hibernate.dialect&quot;&gt;${jdbc.dialect}&lt;\/prop&gt; \t\t\t\t&lt;prop key=&quot;hibernate.connection.charSet&quot;&gt;UTF-8&lt;\/prop&gt; \t\t\t&lt;\/props&gt; \t\t&lt;\/property&gt; \t&lt;\/bean&gt; <\/code><\/pre>\n<p>  \u0422\u0430\u043a\u0436\u0435 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u044f\u0442\u0441\u044f \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u0430\u0442\u0430\u043b\u043e\u0433 <code>src\/test\/resources<\/code>, \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u0432 \u043d\u0435\u0433\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 <code>security.xml<\/code>, \u0441\u043e\u0437\u0434\u0430\u0435\u043c <code>testdb.properties<\/code> \u2014 \u0444\u0430\u0439\u043b \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u0434\u043b\u044f \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445. \u0411\u0435\u0437 \u043d\u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u043e\u0439\u0442\u0438\u0441\u044c, \u043d\u043e \u043e\u043f\u044f\u0442\u044c \u0442\u0430\u043a\u0438 \u0432 \u0443\u0447\u0435\u0431\u043d\u044b\u0445 \u0446\u0435\u043b\u044f\u0445 \u043c\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0432 \u0431\u0438\u043d\u044b \u0438\u0437\u0432\u043d\u0435. \u0421\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0444\u0430\u0439\u043b\u0430  <\/p>\n<pre><code class=\"nginx\">db.user.name=sa db.user.pass= <\/code><\/pre>\n<p>  \u041d\u0435 \u0431\u043e\u0433 \u0432\u0435\u0441\u0442\u044c \u0447\u0442\u043e, \u043d\u043e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442. \u0414\u0435\u043b\u0430\u0435\u043c \u043a\u043e\u043f\u0438\u044e <code>log4j.xml<\/code> \u0438 \u0441 \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u043c\u0438 \u043d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u0435. \u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u043c \u043a\u043e\u0434\u0430\u043c. <\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u0430\u0442\u0430\u043b\u043e\u0433 <code>src\/test\/groovy<\/code>, \u043f\u0430\u043a\u0435\u0442 <code>com.acme.contactmanager.test<\/code>   <\/p>\n<blockquote><p>\u0427\u0442\u043e\u0431\u044b \u043c\u0430\u0432\u0435\u043d \u043f\u0440\u0438 \u0441\u0431\u043e\u0440\u043a\u0435 \u0438\u0437 \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u043d\u0430\u0448\u0435\u043b \u043d\u0430\u0448\u0438 \u0433\u0440\u0443\u0432\u0438-\u0442\u0435\u0441\u0442\u044b, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432 pom.xml build-helper-maven-plugin<\/p><\/blockquote>\n<p>  <\/p>\n<h5>Spring-\u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u043e\u0432<\/h5>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0430\u0439\u043b \u0441\u043f\u0440\u0438\u043d\u0433\u043e\u0432\u043e\u0439 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 <code>TestConfig.groovy<\/code>  <\/p>\n<pre><code class=\"java\">@Configuration @ComponentScan(['net.schastny.contactmanager.dao', 'com.acme.contactmanager.dao', 'net.schastny.contactmanager.web', 'net.schastny.contactmanager.service']) @PropertySource('classpath:testdb.properties') @ImportResource('classpath:security.xml') @EnableTransactionManagement class TestConfig { \/\/ ... }<\/code><\/pre>\n<p>  \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0439 \u0433\u043e\u0432\u043e\u0440\u044f\u0442 \u0441\u0430\u043c\u0438 \u0437\u0430 \u0441\u0435\u0431\u044f:  <\/p>\n<ul>\n<li>\u044d\u0442\u043e \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f<\/li>\n<li>\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043a\u0430\u0442\u044c \u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0445 \u043f\u0430\u043a\u0435\u0442\u0430\u0445 (\u043d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u044e: \u044d\u0442\u043e \u0433\u0440\u0443\u0432\u0438 \u0438 \u043c\u0430\u0441\u0441\u0438\u0432 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u043d\u044b\u0435 \u0441\u043a\u043e\u0431\u043a\u0438)<\/li>\n<li>\u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0444\u0430\u0439\u043b \u043f\u0440\u043e\u043f\u0435\u0440\u0442\u0435\u0439 <code>testdb.properties<\/code><\/li>\n<li>\u043d\u0435 \u0437\u0430\u0431\u044b\u0442\u044c \u043f\u0440\u043e <code>security.xml<\/code><\/li>\n<li>\u0438 \u0434\u0430, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u044b \u0431\u0443\u0434\u0443\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438<\/li>\n<\/ul>\n<p>  <\/p>\n<blockquote><p>\u041a\u0441\u0442\u0430\u0442\u0438, \u043f\u043e \u043f\u043e\u0432\u043e\u0434\u0443 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439. \u0423 \u043d\u0430\u0441 \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u043e\u0432 DAO \u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u044f\u0432\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u044b \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u043d\u043e \u0432\u043e\u043e\u0431\u0449\u0435 \u043c\u043e\u0436\u043d\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c\u0441\u044f \u043e\u0434\u043d\u0438\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u0430\u0441\u0441\u043e\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u0412 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u043d\u0430\u0434\u043e \u0431\u0443\u0434\u0435\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e\u0431\u044b proxyTarget \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u043b\u0438\u0441\u044c \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u0438\u043d\u0430\u0447\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b: <code>@EnableTransactionManagement(proxyTargetClass = true)<\/code><\/p><\/blockquote>\n<p>  \u0414\u0430\u043b\u0435\u0435. \u041f\u0440\u0438\u0432\u044f\u0436\u0435\u043c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0438\u0437 <code>testdb.properties<\/code> \u043a \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430\u043c \u043a\u043b\u0430\u0441\u0441\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>@Value<\/code>  <\/p>\n<pre><code class=\"java\">class TestConfig {  \t@Value('${db.user.name}') \tString userName  \t@Value('${db.user.pass}') \tString userPass  \t\/\/ ... } <\/code><\/pre>\n<p>  \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0431\u0438\u043d <code>LocalSessionFactoryBean<\/code>, \u0433\u0434\u0435 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430. \u0417\u0434\u0435\u0441\u044c \u0432\u0438\u0434\u0438\u043c \u0443\u0436\u0435 \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0439 \u043d\u0430\u043c <code>packagesToScan<\/code>  <\/p>\n<pre><code class=\"java\">\t@Bean \tpublic LocalSessionFactoryBean sessionFactory() {  \t\tLocalSessionFactoryBean bean = new LocalSessionFactoryBean()  \t\tbean.packagesToScan = [ \t\t\t'com.acme.contactmanager.domain', \t\t\t'net.schastny.contactmanager.domain'] as String[]  \t\tProperties props = new Properties() \t\tprops.&quot;hibernate.connection.driver_class&quot; = &quot;org.h2.Driver&quot; \t\tprops.&quot;hibernate.connection.url&quot; = &quot;jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE;DB_CLOSE_ON_EXIT=FALSE&quot; \t\tprops.&quot;hibernate.connection.username&quot; = userName \t\tprops.&quot;hibernate.connection.password&quot; = userPass \t\tprops.&quot;hibernate.dialect&quot; = &quot;org.hibernate.dialect.H2Dialect&quot; \t\tprops.&quot;hibernate.hbm2ddl.auto&quot; = &quot;create-drop&quot; \t\tprops.&quot;hibernate.temp.use_jdbc_metadata_defaults&quot; = &quot;false&quot;  \t\tbean.hibernateProperties = props \t\t \t\tbean \t}<\/code><\/pre>\n<p>  <\/p>\n<blockquote><p>\u0412\u043e\u0442 \u044d\u0442\u0430 \u0448\u0442\u0443\u043a\u0430 <code>hibernate.temp.use_jdbc_metadata_defaults = false<\/code> \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 <a href=\"http:\/\/stackoverflow.com\/questions\/10075081\/hibernate-slow-to-acquire-postgres-connection\">\u0442\u043e\u0440\u043c\u043e\u0437\u0438\u0442 \u0441\u0442\u0430\u0440\u0442 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430<\/a> \u043d\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u0411\u0414 <\/p><\/blockquote>\n<p>  \u0418 \u043d\u0430\u043a\u043e\u043d\u0435\u0446 \u00ab\u0432\u0438\u0448\u0435\u043d\u043a\u043e\u0439 \u043d\u0430 \u0442\u043e\u0440\u0442\u0435\u00bb \u043d\u0430\u0448\u0435\u0439 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 \u0431\u0443\u0434\u0435\u0442 <code>HibernateTransactionManager<\/code>   <\/p>\n<pre><code class=\"java\">\t@Bean \tpublic HibernateTransactionManager transactionManager() { \t\tHibernateTransactionManager txManager = new HibernateTransactionManager() \t\ttxManager.autodetectDataSource = false \t\ttxManager.sessionFactory = sessionFactory().object \t\ttxManager \t}<\/code><\/pre>\n<p>  \u041f\u043e\u0432\u0442\u043e\u0440\u044e\u0441\u044c \u2014 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043e\u0441\u0442\u0430\u043b\u0430\u0441\u044c \u0441\u0442\u0430\u0440\u043e\u0439, \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 xml. \u042d\u0442\u0430 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0442\u0435\u0441\u0442\u044b.<\/p>\n<h5>\u041d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/h5>\n<p>  \u041d\u043e \u044d\u0442\u043e \u0432\u0441\u0435 \u0431\u044b\u043b\u0430 \u043f\u0440\u0438\u0441\u043a\u0430\u0437\u043a\u0430, \u043d\u0430\u0441\u0442\u0430\u043b\u043e \u0432\u0440\u0435\u043c\u044f \u0441\u043a\u0430\u0437\u043a\u0438. \u041b\u043e\u0433\u0438\u0447\u043d\u043e \u043e\u0436\u0438\u0434\u0430\u0442\u044c, \u0447\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 MockMvc \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 3 \u0448\u0430\u0433\u043e\u0432: \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043c\u043e\u043a-\u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0430 HTTP-\u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0443 \u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0430\u043d\u0430\u043b\u0438\u0437 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432. \u0414\u043b\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0448\u0430\u0433\u0430 \u2014 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u043c\u043e\u043a-\u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u2014 \u043c\u044b \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0431\u0438\u043b\u0434\u0435\u0440\u043e\u043c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 WebApplicationContext. <\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u0441 \u043c\u043d\u0435\u043c\u043e\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c <code>MockMvcTest.groovy<\/code>  <\/p>\n<pre><code class=\"java\">@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(classes = [ TestConfig.class ] ) class MockMvcTest {  \t@Autowired \tWebApplicationContext wac  \tMockMvc mockMvc \t \t@Before \tpublic void setup() { \t\tthis.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).dispatchOptions(true).build() \t} } <\/code><\/pre>\n<p>  \u0412\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u043e \u0431\u0435\u0437\u043e\u0431\u0440\u0430\u0437\u0438\u044f. \u041c\u043e\u0436\u043d\u043e \u0443\u0436\u0435 \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u0417\u0430\u0433\u043b\u044f\u043d\u0435\u043c \u0432 \u043d\u0430\u0448 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0438 \u0443\u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0445\u043e\u0440\u043e\u0448\u0438\u043c \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u043c \u043d\u0430 \u043f\u0440\u043e\u0431\u043d\u044b\u0439 \u0442\u0435\u0441\u0442 \u0441\u043b\u0443\u0436\u0438\u0442 \u043c\u0435\u0442\u043e\u0434 <code>home()<\/code>, \u0433\u0434\u0435 \u0441\u0442\u043e\u0438\u0442 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0440\u0435\u0434\u0438\u0440\u0435\u043a\u0442  <\/p>\n<pre><code class=\"java\">\t@RequestMapping(&quot;\/&quot;) \tpublic String home() { \t\treturn &quot;redirect:\/index&quot;; \t} <\/code><\/pre>\n<p>  \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0442\u0430\u043a \u0438 \u043f\u0438\u0448\u0435\u043c  <\/p>\n<pre><code class=\"java\">\t@Test \tpublic void home() {         MockHttpServletRequestBuilder request = MockMvcRequestBuilders.get(&quot;\/&quot;) \t\tResultActions result = mockMvc.perform(request) \t\tresult.andExpect(MockMvcResultMatchers.redirectedUrl(&quot;\/index&quot;)) \t} <\/code><\/pre>\n<p>  \u041f\u043e\u044f\u0441\u043d\u0435\u043d\u0438\u0439 <a href=\"http:\/\/izvestia.ru\/news\/544796\">\u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438<\/a> \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f:  <\/p>\n<ul>\n<li>\u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e MockMvcRequestBuilders.get(&quot;\/&quot;) \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043e\u0431\u0435\u0440\u0442\u043a\u0443 \u0434\u043b\u044f GET-\u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0430 \u0443\u0440\u043b &quot;\/&quot;<\/li>\n<li>mockMvc.perform(request) \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/li>\n<li>\u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0447\u0442\u043e \u0432\u0435\u0440\u043d\u0443\u043b\u0441\u044f \u0440\u0435\u0434\u0438\u0440\u0435\u043a\u0442 \u043d\u0430 \u043d\u0443\u0436\u043d\u044b\u0439 \u0443\u0440\u043b<\/li>\n<\/ul>\n<p>  <\/p>\n<blockquote><p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>andExpect()<\/code> \u0434\u0430\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430, \u0432\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0438\u0437 Javadoc, \u0434\u0430\u044e\u0449\u0438\u0435 \u043e\u0431\u0449\u0435\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e \u0435\u0451 \u0440\u0430\u0431\u043e\u0442\u0435:  <\/p>\n<pre><code class=\"java\">mockMvc.perform(get(&quot;\/person\/1&quot;))    .andExpect(status.isOk())    .andExpect(content().mimeType(MediaType.APPLICATION_JSON))    .andExpect(jsonPath(&quot;$.person.name&quot;).equalTo(&quot;Jason&quot;));   mockMvc.perform(post(&quot;\/form&quot;))    .andExpect(status.isOk())    .andExpect(redirectedUrl(&quot;\/person\/1&quot;))    .andExpect(model().size(1))    .andExpect(model().attributeExists(&quot;person&quot;))    .andExpect(flash().attributeCount(1))    .andExpect(flash().attribute(&quot;message&quot;, &quot;success!&quot;)); <\/code><\/pre>\n<\/blockquote>\n<p>  \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c, \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u0423\u0440\u0430, \u043f\u0435\u0440\u0432\u044b\u0439 \u0442\u0435\u0441\u0442 \u0433\u043e\u0442\u043e\u0432. \u0427\u0442\u043e \u0442\u0430\u043c \u0434\u0430\u043b\u044c\u0448\u0435? \u0421\u043f\u0438\u0441\u043e\u043a \u043a\u043e\u043d\u0442\u0430\u043a\u0442\u043e\u0432.  <\/p>\n<pre><code class=\"java\">\t@RequestMapping(&quot;\/index&quot;) \tpublic String listContacts(Map&lt;String, Object&gt; map) {  \t\tmap.put(&quot;contact&quot;, new Contact()); \t\tmap.put(&quot;contactList&quot;, contactService.listContact()); \t\tmap.put(&quot;contactTypeList&quot;, contactService.listContactType());  \t\treturn &quot;contact&quot;; \t} <\/code><\/pre>\n<p>  \u0412\u0441\u0435 \u0442\u043e\u0442 \u0436\u0435 GET-\u0437\u0430\u043f\u0440\u043e\u0441, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043c\u0430\u043f\u0443 \u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u0445, \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0435\u0451 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0438\u043c\u044f view. \u041e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c GET-\u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043c\u044b \u0443\u0436\u0435 \u0443\u043c\u0435\u0435\u043c, \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430. \u041f\u0438\u0448\u0435\u043c \u0432\u0442\u043e\u0440\u043e\u0439 \u0442\u0435\u0441\u0442.  <\/p>\n<pre><code class=\"java\">\t@Test \tpublic void index() {  \t\tResultActions result = mockMvc.perform(MockMvcRequestBuilders.get(&quot;\/index&quot;))  \t\tresult.andExpect(MockMvcResultMatchers.view().name(&quot;contact&quot;)) \t\t\t.andExpect(MockMvcResultMatchers.model().attributeExists(&quot;contact&quot;)) \t\t\t.andExpect(MockMvcResultMatchers.model().attributeExists(&quot;contactList&quot;)) \t\t\t.andExpect(MockMvcResultMatchers.model().attributeExists(&quot;contactTypeList&quot;)) \t} <\/code><\/pre>\n<p>  \u0412\u043d\u043e\u0432\u044c \u043d\u0438 \u043e\u0434\u043d\u043e\u0439 \u043b\u0438\u0448\u043d\u0435\u0439 \u0441\u0442\u0440\u043e\u0447\u043a\u0438. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0432 \u0437\u0430\u043f\u0440\u043e\u0441, \u043c\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043b\u0438 \u0438\u043c\u044f view, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043e\u043d \u043d\u0430\u043c \u0432\u0435\u0440\u043d\u0443\u043b, \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043b\u0438, \u0447\u0442\u043e \u0432\u0441\u0435 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b \u043c\u043e\u0434\u0435\u043b\u0438 \u043d\u0430 \u043c\u0435\u0441\u0442\u0435. \u0414\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u044d\u0442\u0438\u0445 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 MvcResult \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 andReturn()  <\/p>\n<pre><code class=\"java\">MvcResult mvcResult  = result.andReturn() assert mvcResult.modelAndView.model.contactTypeList.size() == 3 <\/code><\/pre>\n<p>  \u041f\u043e\u043a\u0430 \u0432\u0441\u0435 \u0438\u0434\u0435\u0442 \u043d\u0435\u043f\u043b\u043e\u0445\u043e, \u043d\u043e \u0432\u043f\u0435\u0440\u0435\u0434\u0438 \u0435\u0449\u0451 \u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0431\u043e\u0442\u044b. \u041f\u043e\u0440\u0430 \u0443\u0436\u0435 \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u043d\u0430\u0448 \u0441\u043f\u0438\u0441\u043e\u043a. \u041c\u0435\u0442\u043e\u0434 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"java\">\t@RequestMapping(value = &quot;\/add&quot;, method = RequestMethod.POST) \tpublic String addContact(@ModelAttribute(&quot;contact&quot;) Contact contact, BindingResult result) {  \t\tcontactService.addContact(contact);  \t\treturn &quot;redirect:\/index&quot;; \t} <\/code><\/pre>\n<p>  \u041d\u0430\u043a\u043e\u043d\u0435\u0446-\u0442\u043e POST-\u043c\u0435\u0442\u043e\u0434 \u0434\u0430 \u043f\u043b\u044e\u0441 \u0435\u0449\u0451 \u0443\u0441\u0442\u0440\u0430\u0448\u0430\u044e\u0449\u0435\u0433\u043e \u0432\u0438\u0434\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>@ModelAttribute Contact contact<\/code>. \u041d\u043e \u043d\u0435 \u0432\u0441\u0435 \u0442\u0430\u043a \u043f\u043b\u043e\u0445\u043e. \u0411\u0435\u0433\u043b\u043e\u0435 \u0433\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u043f\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u00abmockmvc ModelAttribute\u00bb \u0442\u0443\u0442\u0436\u0435 \u0434\u0430\u0435\u0442 <a href=\"http:\/\/forum.springsource.org\/archive\/index.php\/t-124040.html\">\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/a>. \u041f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u043c\u0430\u043f\u043f\u0438\u043d\u0433 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430\u0431\u043e\u0440\u043e\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0432 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u043f\u043e\u043b\u043d\u0435 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a: <code>param(Stirng name, String... values)<\/code>. \u041f\u0438\u0448\u0435\u043c  <\/p>\n<pre><code class=\"java\">\t@Autowired \tContactService contactService  \t@Test \tpublic void add() {  \t\t\/\/ \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u0438\u0437 \u0411\u0414 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0447\u0442\u043e \u043e\u043d \u043f\u0443\u0441\u0442\u043e\u0439 \t\tdef contacts = contactService.listContact() \t\tassert !contacts  \t\t\/\/ \u0434\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0430\u043a\u0442\u0430 \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u0435\u0433\u043e \u0442\u0438\u043f \t\tdef contactTypes = contactService.listContactType() \t\tassert contactTypes  \t\t\/\/  \u0441\u043e\u0437\u0434\u0430\u0435\u043c POST-\u0437\u0430\u043f\u0440\u043e\u0441, \u043d\u0430\u0431\u0438\u0432\u0430\u0435\u043c \u0435\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \t\tmockMvc.perform(MockMvcRequestBuilders.post(&quot;\/add&quot;) \t\t\t\t.param(&quot;firstname&quot;,'firstname') \t\t\t\t.param(&quot;lastname&quot;,'lastname') \t\t\t\t.param(&quot;email&quot;,'firstname.lastname@gmail.com') \t\t\t\t.param(&quot;telephone&quot;,'555-1234') \t\t\t\t.param(&quot;contacttype.id&quot;, contactTypes[0].id.toString()) \t\t.andExpect(MockMvcResultMatchers.redirectedUrl(&quot;\/index&quot;))  \t\t\/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0411\u0414 \t\tcontacts = contactService.listContact()  \t\t\/\/ \u0441\u043f\u0438\u0441\u043e\u043a \u043d\u0435 \u043f\u0443\u0441\u0442\u043e\u0439 \u0438 \u0443 \u043a\u043e\u043d\u0442\u0430\u043a\u0442\u0430 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 id \t\tassert contacts \t\tassert contacts[0].id  \t\t\/\/ \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0430\u043a\u0442, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044f \u0411\u0414 \u0432 \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \t\tcontactService.removeContact(contacts[0].id) } <\/code><\/pre>\n<p>  \u041d\u0443 \u0438 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 \u2014 delete().   <\/p>\n<pre><code class=\"java\">\t@RequestMapping(&quot;\/delete\/{contactId}&quot;) \tpublic String deleteContact(@PathVariable(&quot;contactId&quot;) Integer contactId) {  \t\tcontactService.removeContact(contactId);  \t\treturn &quot;redirect:\/index&quot;; \t} <\/code><\/pre>\n<p>  \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 <code>@PathVariable<\/code> \u0442\u043e\u0436\u0435 \u043d\u0435 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0435\u0451 \u0432 URL.  <\/p>\n<pre><code class=\"java\">\t@Test \tpublic void delete() {  \t\t\/\/ \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0430\u043a\u0442 \u0447\u0435\u0440\u0435\u0437 \u0441\u0435\u0440\u0432\u0438\u0441 \t\tdef contactTypes = contactService.listContactType() \t\tassert contactTypes  \t\tContact contact = new Contact( \t\t\t\tfirstname : 'firstname', \t\t\t\tlastname : 'lastname', \t\t\t\temail : 'firstname.lastname@gmail.com', \t\t\t\ttelephone : '555-1234', \t\t\t\tcontacttype : contactTypes[0] \t\t\t) \t\t \t\tcontactService.addContact(contact) \t\tassert contact.id  \t\tdef contacts = contactService.listContact() \t\t\/\/ \u0432 \u0433\u0440\u0443\u0432\u0438 contacts.id \u0434\u0430\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 id \u043a\u043e\u043d\u0442\u0430\u043a\u0442\u043e\u0432 \t\tassert contact.id in contacts.id  \t\t\/\/ \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c POST-\u0437\u0430\u043f\u0440\u043e\u0441 , \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044f \u0432 URL id \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0430\u043a\u0442\u0430  \t\t\/\/ ${contact.id} - \u044d\u0442\u043e \u043d\u0435 \u0441\u043f\u0440\u0438\u043d\u0433\u043e\u0432\u044b\u0439 placeholder, \u044d\u0442\u043e GString! \t\tmockMvc.perform(MockMvcRequestBuilders.get(&quot;\/delete\/${contact.id}&quot;) \t\t\t.andExpect(MockMvcResultMatchers.redirectedUrl(&quot;\/index&quot;)) \t\t \t\t\/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0447\u0442\u043e \u043a\u043e\u043d\u0442\u0430\u043a\u0442 \u0443\u0434\u0430\u043b\u0438\u043b\u0441\u044f \t\tdef contacts = contactService.listContact() \t\tassert !(contact.id in contacts.id) \t} <\/code><\/pre>\n<p>  \u0412\u043e\u0442 \u0438 \u0432\u0441\u0435, \u0432\u0441\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043f\u043e\u043a\u0440\u044b\u0442\u044b \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u043c\u0438 \u0442\u0435\u0441\u0442\u0430\u043c\u0438, \u0443\u0440\u0430! \u0418\u043b\u0438 \u043d\u0435 \u0443\u0440\u0430? \u0412\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044c \u0441\u043f\u0440\u043e\u0441\u0438\u0442 \u2014 \u0430 \u043a\u0430\u043a \u0436\u0435 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c?! \u0417\u0430\u0447\u0435\u043c \u043c\u044b \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u043b\u0438 <code>security.xml<\/code>, \u0435\u0441\u043b\u0438 \u0432 \u0442\u0435\u0441\u0442\u0430\u0445 \u043d\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u043d\u0438\u044f \u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u0445 \u0438 \u0440\u043e\u043b\u044f\u0445?! \u0418 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0430\u0432. \u0412 \u0442\u0440\u0435\u0442\u044c\u0435\u0439 \u0447\u0430\u0441\u0442\u0438 \u0443\u0440\u043e\u043a\u0430 \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u043e SpringSecurity.<\/p>\n<h5>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e<\/h5>\n<p>  \u041b\u043e\u0433\u0438\u0447\u043d\u043e \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c, \u0447\u0442\u043e MockMvc \u0434\u043e\u043b\u0436\u0435\u043d \u0438\u043c\u0435\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u043c\u0438. \u0412 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435, \u0432 \u0431\u0438\u043b\u0434\u0435\u0440\u0435 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 <code>addFilter()<\/code>, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 <code>springSecurityFilterChain<\/code>. \u0418\u0437\u043c\u0435\u043d\u0438\u043c \u043d\u0430\u0448 \u0442\u0435\u0441\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:  <\/p>\n<pre><code class=\"java\">\t@Autowired \tFilterChainProxy springSecurityFilterChain \t \t@Before \tpublic void setup() { \t\tthis.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac) \t\t\t\t.addFilter(springSecurityFilterChain) \/\/ \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0444\u0438\u043b\u044c\u0442\u0440 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \t\t\t\t.dispatchOptions(true).build() \t}\t <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043f\u0440\u0430\u0432 \u043f\u0440\u0438 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0438 \u043a \u0443\u0440\u043b\u0430\u043c, \u043d\u043e \u043d\u0443\u0436\u043d\u043e \u043a\u0430\u043a\u0438\u043c-\u0442\u043e \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0435. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u00ab\u0444\u0438\u043d\u0442 \u0443\u0448\u0430\u043c\u0438\u00bb \u0438 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 SecirityContextHolder.  <\/p>\n<pre><code class=\"java\">\t\tList&lt;GrantedAuthority&gt; authorities = AuthorityUtils.createAuthorityList(&quot;ROLE_USER&quot;); \t\tAuthentication auth = new UsernamePasswordAuthenticationToken(&quot;user1&quot;, &quot;1111&quot;, authorities); \t\tSecurityContextHolder.getContext().setAuthentication(auth); <\/code><\/pre>\n<p>  \u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u0440\u0430\u0432\u0434\u043e\u043f\u043e\u0434\u043e\u0431\u043d\u043e, \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043c\u0435\u0442\u043e\u0434 add(), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043d\u0430\u043b\u0438\u0447\u0438\u044f \u043f\u0440\u0438\u0432\u0438\u043b\u0435\u0433\u0438\u0438 ROLE_USER. \u0411\u0430\u0431\u0430\u0445! \u041d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c  <\/p>\n<pre><code class=\"nginx\">DEBUG: org.springframework.security.web.access.intercept.FilterSecurityInterceptor - Secure object: FilterInvocation: URL: \/add; Attributes: [ROLE_USER] DEBUG: org.springframework.security.web.access.intercept.FilterSecurityInterceptor - Previously Authenticated: org.springframework.security.authentication. AnonymousAuthenticationToken@d4551ca6: Principal: guest; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication. WebAuthenticationDetails@957e: RemoteIpAddress: 127.0.0.1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS DEBUG: org.springframework.security.web.access.ExceptionTranslationFilter - Access is denied (user is anonymous); redirecting to authentication entry point <\/code><\/pre>\n<p>  <code>Granted Authorities: ROLE_ANONYMOUS<\/code> \u043a\u0430\u043a \u0431\u044b \u043d\u0430\u043c\u0435\u043a\u0430\u0435\u0442 \u043d\u0430\u043c, \u0447\u0442\u043e \u043d\u0430\u0448 \u0444\u0438\u043d\u0442 \u0443\u0448\u0430\u043c\u0438 \u043d\u0435 \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u043b. \u041d\u043e \u0441\u043f\u0435\u0448\u0443 \u0443\u0441\u043f\u043e\u043a\u043e\u0438\u0442\u044c \u2014 \u043d\u0430\u0448\u0435\u0439 \u0432\u0438\u043d\u044b \u0442\u0443\u0442 \u043d\u0435\u0442, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0430 Security \u0432 \u0442\u0435\u0441\u0442\u0430\u0445 <a href=\"https:\/\/jira.springsource.org\/browse\/SEC-2015\">\u0435\u0449\u0451 \u043f\u043e\u043a\u0430 \u043d\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430<\/a>. \u0418\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b, \u0447\u0442\u043e \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0441\u043e SpringSecurity \u00ab\u043f\u043e\u0447\u0442\u0438 \u043d\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430\u00bb. \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0442\u0430\u043a\u0438 \u0435\u0441\u0442\u044c, \u043d\u043e \u043e\u043d\u0430 \u0440\u0435\u0448\u0430\u0435\u043c\u0430. <\/p>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u043d\u0430\u043c \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043a\u043b\u0430\u0441\u0441 <a href=\"https:\/\/github.com\/SpringSource\/spring-test-mvc\/blob\/master\/src\/test\/java\/org\/springframework\/test\/web\/server\/samples\/context\/SecurityRequestPostProcessors.java\">SecurityRequestPostProcessors.java<\/a>. \u042f \u043d\u0435 \u0431\u0443\u0434\u0443 \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0435\u0433\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u043c, \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043a\u043e\u043f\u0438\u0440\u0443\u044e \u0435\u0433\u043e \u0432 \u043f\u0430\u043f\u043a\u0443 <code>src\/test\/java<\/code> \u0438 \u043f\u043e\u043a\u0430\u0436\u0443, \u043a\u0430\u043a \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0430\u0448\u0438\u0445 \u043d\u0443\u0436\u0434\u0430\u0445. <\/p>\n<p>  \u0423\u0431\u0438\u0440\u0430\u0435\u043c \u043e\u043a\u0430\u0437\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u0431\u0435\u0441\u043f\u043e\u043b\u0435\u0437\u043d\u044b\u043c \u0432\u044b\u0437\u043e\u0432 <code>setAuthentication(auth)<\/code>, \u0430 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 add() \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u0434\u043d\u0443 \u0441\u0442\u0440\u043e\u0447\u043a\u0443:  <\/p>\n<pre><code class=\"java\">\t\t\/\/...  \t\tmockMvc.perform(MockMvcRequestBuilders.post(&quot;\/add&quot;) \t\t\t\t.param(&quot;firstname&quot;,'firstname') \t\t\t\t.param(&quot;lastname&quot;,'lastname') \t\t\t\t.param(&quot;email&quot;,'firstname.lastname@gmail.com') \t\t\t\t.param(&quot;telephone&quot;,'555-1234') \t\t\t\t.param(&quot;contacttype.id&quot;, contactTypes[0].id.toString()) \t\t\t\t.with(SecurityRequestPostProcessors.userDetailsService(&quot;user1&quot;))) \/\/ \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 Security  \t\t\t\t.andExpect(MockMvcResultMatchers.redirectedUrl(&quot;\/index&quot;)) \t\t\/\/ ... <\/code><\/pre>\n<p>  \u0422\u043e \u0435\u0441\u0442\u044c \u043f\u043e \u0441\u0443\u0442\u0438 \u043c\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u043e\u0442 \u0438\u043c\u0435\u043d\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f user1, \u0441\u043e \u0432\u0441\u0435\u043c\u0438 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u0430\u043c\u0438. \u0418 \u043e\u043d \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442! \u0412 \u043b\u043e\u0433\u0435 \u0432\u0438\u0434\u0438\u043c \u0438\u0441\u043a\u043e\u043c\u043e\u0435 <code>Granted Authorities: ROLE_USER<\/code><\/p>\n<p>  \u041d\u043e \u043d\u0435 \u0441\u043f\u0435\u0448\u0438\u0442\u0435 \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u0441\u0442\u0430\u0440\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0442\u0435\u0441\u0442\u043e\u0432, \u043e\u043d\u0438 \u0435\u0449\u0451 \u043d\u0430\u043c \u043f\u0440\u0438\u0433\u043e\u0434\u044f\u0442\u0441\u044f. \u0412\u0435\u0434\u044c \u0432 \u0442\u0430\u043a\u043e\u043c \u0432\u0438\u0434\u0435 \u043e\u043d\u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u044e\u0442 \u043d\u0438 \u0447\u0442\u043e \u0438\u043d\u043e\u0435, \u043a\u0430\u043a \u043d\u0435\u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043d\u0430\u0448\u0435\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435. \u0418 \u0442\u0435 \u0436\u0435 \u043c\u0435\u0442\u043e\u0434\u044b home() \u0438 index() \u0434\u043e\u043b\u0436\u043d\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u044d\u0442\u0438 \u0443\u0440\u043b\u044b \u043d\u0435 \u043d\u0430\u043b\u0430\u0433\u0430\u044e\u0442 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u043d\u0430 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e. \u0418 \u043e\u043d\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442! <\/p>\n<p>  \u0412\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u043a \u043c\u0435\u0442\u043e\u0434\u0443 add(). \u0427\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043a\u043e\u043d\u0442\u0430\u043a\u0442, \u0431\u0443\u0434\u0443\u0447\u0438 \u043d\u0435\u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u043c\u0438? \u041f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430\u043c \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u043b\u043e\u0433\u0438\u043d\u0430! \u0412 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u0445 \u0440\u0435\u0434\u0438\u0440\u0435\u043a\u0442\u0430 \u044d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0440\u0435\u0434\u0438\u0440\u0435\u043a\u0442 \u043d\u0430 \/login.jsp<br \/>  \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u043d\u0435\u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0430 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0430\u043a\u0442\u0430 \u043c\u044b \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u043d\u0430 \u0434\u0440\u0443\u0433\u0443\u044e:  <\/p>\n<pre><code class=\"java\">result.andExpect(MockMvcResultMatchers.redirectedUrl(&quot;http:\/\/localhost\/login.jsp&quot;)) <\/code><\/pre>\n<p>  \u0422\u0430\u043a\u0438\u043c \u0436\u0435 \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043d\u0430 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043e\u043b\u0436\u0435\u043d \u0440\u0435\u0430\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u043c\u0435\u0442\u043e\u0434 delete(). \u0410 \u043e\u0442 \u0438\u043c\u0435\u043d\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u00abadmin\u00bb \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0438 \u044d\u0442\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e.<\/p>\n<p>  \u0418 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0449\u0451 \u043e\u0434\u0438\u043d \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u2014 \u043a\u043e\u0433\u0434\u0430 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0437\u0430\u043f\u0438\u0441\u044c \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0441 \u043f\u0440\u0430\u0432\u0430\u043c\u0438 ROLE_USER. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u043d \u0434\u043e\u043b\u0436\u0435\u043d \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443 403, \u0430 \u0442\u043e\u0447\u043d\u0435\u0435 \u2014 \u0444\u043e\u0440\u0432\u0430\u0440\u0434 \u043d\u0430 \/error403.jsp. \u0422\u0435\u043b\u043e \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a (id \u043a\u043e\u043d\u0442\u0430\u043a\u0442\u0430 \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435 \u0438\u0433\u0440\u0430\u0435\u0442 \u0440\u043e\u043b\u0438, \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u043c \/1):   <\/p>\n<pre><code class=\"java\">\t\tmockMvc.perform(MockMvcRequestBuilders.get(&quot;\/delete\/1&quot;) \t\t\t\t.with(SecurityRequestPostProcessors.userDetailsService(&quot;user1&quot;))) \t\t\t\t.andExpect(MockMvcResultMatchers.forwardedUrl(&quot;\/error403.jsp&quot;)) <\/code><\/pre>\n<p>  \u0412\u043e\u0442 \u0438 \u0432\u0441\u0435. \u0412 \u0438\u0442\u043e\u0433\u0435 \u0443 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c 12 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u043f\u043e 3 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 4 \u0443\u0440\u043b\u043e\u0432. \u041e\u043d\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e\u0442 \u043d\u0435\u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f, \u0434\u043e\u0441\u0442\u0443\u043f \u0441 \u043f\u0440\u0430\u0432\u0430\u043c\u0438 ROLE_USER \u0438 ROLE_ADMIN. \u041f\u043e\u043f\u0443\u0442\u043d\u043e \u0441 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430\u043c\u0438 \u043c\u044b \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u0438 \u0414\u0410\u041e. <\/p>\n<p>  <a href=\"https:\/\/github.com\/monzdrpower\/ContactManager-MockMvc\">\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043d\u0430 GitHub<\/a>    \t \t\t   \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/171911\/\"> http:\/\/habrahabr.ru\/post\/171911\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442.<\/p>\n<p>  \u041f\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0432\u0448\u0438\u0441\u044c \u0441 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439 MockMvc, \u044f \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u043b &quot;<i>\u043f\u043e\u043b\u043d\u043e\u0435 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f<\/i>&quot; \u0435\u0451 \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u043d\u0438\u0439 \u043d\u0430 \u0425\u0430\u0431\u0440\u0435. \u041f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u0432\u043e\u0441\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u0440\u043e\u0431\u0435\u043b, \u0442\u0435\u043c \u0431\u043e\u043b\u0435\u0435, \u0447\u0442\u043e \u043d\u0430\u0448\u0435 <a href=\"http:\/\/habrahabr.ru\/post\/145158\">\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 ContactManager<\/a> \u043a\u0430\u043a \u0440\u0430\u0437 \u043d\u0443\u0436\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438.  <\/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-171911","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/171911","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=171911"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/171911\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=171911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=171911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=171911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}