{"id":185834,"date":"2013-07-07T23:33:02","date_gmt":"2013-07-07T19:33:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=185834"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=185834","title":{"rendered":"<span class=\"post_title\">PowerMock(+Mockito) +TestNG \u0438 \u043c\u043e\u043a\u0438\u043d\u0433 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432\/\u043c\u0435\u0442\u043e\u0434\u043e\u0432<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u041d\u0430 \u0445\u0430\u0431\u0440\u0435 \u0443\u0436\u0435 \u0431\u044b\u043b\u0430 <a href=\"\/post\/172239\/\">\u0441\u0442\u0430\u0442\u044c\u044f<\/a> \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f PowerMock, \u043d\u043e \u0432 \u043d\u0435\u0439 \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u0435\u0442 \u0442\u0430\u043a\u043e\u0433\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f, \u043a\u0430\u043a \u043c\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043a\u0430\u043a \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u00ab\u0435\u0434\u0438\u043d\u0438\u0446\u00bb \u0432 \u043a\u043b\u0430\u0441\u0441\u0435, \u0442\u0430\u043a \u0438 \u0432 \u0433\u0438\u0431\u0440\u0438\u0434\u043d\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438, \u043a\u043e\u0433\u0434\u0430 \u0447\u0430\u0441\u0442\u044c \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0443 \u043a\u043b\u0430\u0441\u0441\u0430 \u043c\u043e\u043a\u0438\u0440\u0443\u044e\u0442\u0441\u044f, \u0430 \u0447\u0430\u0441\u0442\u044c \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0440\u0435\u0430\u043b\u044c\u043d\u043e. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u044e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u043d\u0438\u0448\u0443.<\/p>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0441\u043e \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 (<a href=\"https:\/\/github.com\/BorzdeG\/ru.itbasis.demo.mock.staticMethod\/tree\/8e57fa82e47b57920b0b3f573dde2d82017d9f63\">\u043a\u043e\u043c\u043c\u0438\u0442<\/a>):<\/p>\n<pre><code class=\"java\">public class ClassStatic { \tstatic String getValue() { \t\treturn &quot;value&quot;; \t}  \tstatic String getValue(final String s) { \t\treturn getValue() + s; \t} } <\/code><\/pre>\n<p>  <a name=\"habracut\"><\/a><br \/>  \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0430\u0442\u0438\u0447\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 ClassStaticTest (<a href=\"https:\/\/github.com\/BorzdeG\/ru.itbasis.demo.mock.staticMethod\/tree\/262cdc58b7798759148dd12f8b362437909d00f3\">\u043a\u043e\u043c\u043c\u0438\u0442<\/a>):  <\/p>\n<pre><code class=\"java\">import org.testng.Assert; import org.testng.annotations.DataProvider; import org.testng.annotations.Test;  @Test(groups = {&quot;static&quot;, &quot;noMock&quot;}) public class ClassStaticTest { \t@DataProvider \tpublic Object[][] data() throws Exception { \t\treturn new Object[][]{{&quot;&quot;, &quot;value&quot;}, {&quot;test&quot;, &quot;valuetest&quot;}}; \t}  \t@Test \tpublic void testGetValueVoid() throws Exception { \t\tAssert.assertEquals(ClassStatic.getValue(), &quot;value&quot;); \t}  \t@Test(dataProvider = &quot;data&quot;, dependsOnMethods = {&quot;testGetValueVoid&quot;}) \tpublic void testGetValueAttribute(final String v, final String r) throws Exception { \t\tAssert.assertEquals(ClassStatic.getValue(v), r); \t} } <\/code><\/pre>\n<p>  \u0414\u043e \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u043c\u044b \u043d\u0438 \u0440\u0430\u0437\u0443 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 PowerMock, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u043d \u043d\u0430\u043c \u043d\u0435 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u0441\u044f. \u0422\u0435\u043f\u0435\u0440\u044c \u0436\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043c\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u043b\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u00ab\u043a\u043e\u0441\u0442\u044f\u043a\u00bb:  <\/p>\n<pre><code class=\"java\">import org.powermock.modules.testng.PowerMockObjectFactory; import org.testng.IObjectFactory; import org.testng.annotations.ObjectFactory; import org.testng.annotations.Test;  @Test(groups = {&quot;static&quot;, &quot;mock&quot;}) public class ClassStaticMockTest { \t@ObjectFactory \tpublic IObjectFactory setObjectFactory() { \t\treturn new PowerMockObjectFactory(); \t} } <\/code><\/pre>\n<p>\u041c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438 \u043a\u043b\u0430\u0441\u0441 ClassStaticMockTest, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043b\u0438 Object Factory \u0434\u043b\u044f TestNG (<a href=\"http:\/\/code.google.com\/p\/powermock\/wiki\/TestNG_usage#Programmatically\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f<\/a>)<\/p>\n<p>  \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0442\u0435\u0441\u0442 \u0441 \u043c\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430:  <\/p>\n<pre><code class=\"java\">import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.testng.PowerMockObjectFactory; import org.testng.Assert; import org.testng.IObjectFactory; import org.testng.annotations.DataProvider; import org.testng.annotations.ObjectFactory; import org.testng.annotations.Test;  import static org.mockito.MockitoAnnotations.Mock; import static org.powermock.api.mockito.PowerMockito.mockStatic; import static org.powermock.api.mockito.PowerMockito.when;  @Test(groups = {&quot;static&quot;, &quot;mock&quot;}) @PrepareForTest({ClassStatic.class}) public class ClassStaticMockTest {  \t@Mock \tpublic ClassStatic classStatic;  \t@DataProvider \tpublic Object[][] data() throws Exception { \t\treturn new Object[][]{{&quot;&quot;, &quot;newValue&quot;}, {&quot;test&quot;, &quot;newValuetest&quot;}}; \t}  \t@ObjectFactory \tpublic IObjectFactory setObjectFactory() { \t\treturn new PowerMockObjectFactory(); \t}  \t@Test(dependsOnGroups = {&quot;noMock&quot;}) \tpublic void mockGetValueVoid() throws Exception { \t\tmockStatic(ClassStatic.class); \t\twhen(ClassStatic.getValue()).thenReturn(&quot;newValue&quot;); \t\tAssert.assertEquals(ClassStatic.getValue(), &quot;newValue&quot;); \t} } <\/code><\/pre>\n<p><i>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435: \u043d\u0435 \u0437\u043d\u0430\u044e \u043f\u043e\u0447\u0435\u043c\u0443 (\u0433\u0440\u0443\u0441\u0442\u044c), \u043d\u043e \u0431\u0435\u0437 \u0441\u0442\u0440\u043e\u043a\u0438 <code>@Mock public ClassStatic classStatic;<\/code> \u0442\u0435\u0441\u0442 \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f.<\/i><\/p>\n<p>  \u041e\u0441\u0442\u0430\u043b\u0441\u044f \u0437\u0430\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u00ab\u0430\u043a\u043a\u043e\u0440\u0434\u00bb \u2014 \u0433\u0438\u0431\u0440\u0438\u0434\u043d\u044b\u0439 \u0442\u0435\u0441\u0442, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u0434\u0438\u043d \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u043c\u0435\u0442\u043e\u0434 \u0437\u0430\u043c\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u043d, \u0430 \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 (\u0441\u0443\u043c\u043c\u0430\u0440\u043d\u044b\u0439 <a href=\"https:\/\/github.com\/BorzdeG\/ru.itbasis.demo.mock.staticMethod\/tree\/3ae2fea05c70ae8b644010cd9f48bd05951ceaba\">\u043a\u043e\u043c\u043c\u0438\u0442<\/a>):   <\/p>\n<pre><code class=\"java\">import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.testng.PowerMockObjectFactory; import org.testng.Assert; import org.testng.IObjectFactory; import org.testng.annotations.DataProvider; import org.testng.annotations.ObjectFactory; import org.testng.annotations.Test;  import static org.mockito.MockitoAnnotations.Mock; import static org.powermock.api.mockito.PowerMockito.mockStatic; import static org.powermock.api.mockito.PowerMockito.when;  \/** Created by borz on 06.07.13. *\/ @Test(groups = {&quot;static&quot;, &quot;mock&quot;}) @PrepareForTest({ClassStatic.class}) public class ClassStaticMockTest {  \t@Mock \tpublic ClassStatic classStatic;  \t@DataProvider \tpublic Object[][] data() throws Exception { \t\treturn new Object[][]{{&quot;&quot;, &quot;newValue&quot;}, {&quot;test&quot;, &quot;newValuetest&quot;}}; \t}  \t@ObjectFactory \tpublic IObjectFactory setObjectFactory() { \t\treturn new PowerMockObjectFactory(); \t}  \t@Test(dependsOnGroups = {&quot;noMock&quot;}) \tpublic void mockGetValueVoid() throws Exception { \t\tmockStatic(ClassStatic.class); \t\twhen(ClassStatic.getValue()).thenReturn(&quot;newValue&quot;); \t\tAssert.assertEquals(ClassStatic.getValue(), &quot;newValue&quot;); \t}  \t@Test(dataProvider = &quot;data&quot;, dependsOnMethods = {&quot;mockGetValueVoid&quot;}) \tpublic void testGetValueAttribute(final String v, final String r) throws Exception { \t\tmockStatic(ClassStatic.class); \t\twhen(ClassStatic.getValue()).thenReturn(&quot;newValue&quot;); \t\twhen(ClassStatic.getValue(v)).thenCallRealMethod(); \t\tAssert.assertEquals(ClassStatic.getValue(v), r); \t} } <\/code><\/pre>\n<p>\u041c\u044b \u043f\u0435\u0440\u0435\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430 <code>getValue()<\/code> \u0438 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c, \u0447\u0442\u043e \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <code>getValue(String value)<\/code> \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u043d \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0443 \u043a\u043b\u0430\u0441\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0436\u0435 \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u0435\u0431\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 <code>getValue()<\/code>.<\/p>\n<p>  PS: \u0412\u0441\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043d\u0430 <a href=\"https:\/\/github.com\/BorzdeG\/ru.itbasis.demo.mock.staticMethod\/commits\/master\">GitHub<\/a>    \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\/185834\/\"> http:\/\/habrahabr.ru\/post\/185834\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u041d\u0430 \u0445\u0430\u0431\u0440\u0435 \u0443\u0436\u0435 \u0431\u044b\u043b\u0430 <a href=\"\/post\/172239\/\">\u0441\u0442\u0430\u0442\u044c\u044f<\/a> \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f PowerMock, \u043d\u043e \u0432 \u043d\u0435\u0439 \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u0435\u0442 \u0442\u0430\u043a\u043e\u0433\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f, \u043a\u0430\u043a \u043c\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043a\u0430\u043a \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u00ab\u0435\u0434\u0438\u043d\u0438\u0446\u00bb \u0432 \u043a\u043b\u0430\u0441\u0441\u0435, \u0442\u0430\u043a \u0438 \u0432 \u0433\u0438\u0431\u0440\u0438\u0434\u043d\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438, \u043a\u043e\u0433\u0434\u0430 \u0447\u0430\u0441\u0442\u044c \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0443 \u043a\u043b\u0430\u0441\u0441\u0430 \u043c\u043e\u043a\u0438\u0440\u0443\u044e\u0442\u0441\u044f, \u0430 \u0447\u0430\u0441\u0442\u044c \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0440\u0435\u0430\u043b\u044c\u043d\u043e. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u044e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u043d\u0438\u0448\u0443.<\/p>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0441\u043e \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 (<a href=\"https:\/\/github.com\/BorzdeG\/ru.itbasis.demo.mock.staticMethod\/tree\/8e57fa82e47b57920b0b3f573dde2d82017d9f63\">\u043a\u043e\u043c\u043c\u0438\u0442<\/a>):<\/p>\n<pre><code class=\"java\">public class ClassStatic { \tstatic String getValue() { \t\treturn &quot;value&quot;; \t}  \tstatic String getValue(final String s) { \t\treturn getValue() + s; \t} } <\/code><\/pre>\n<p>  <\/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-185834","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/185834","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=185834"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/185834\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=185834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=185834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=185834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}