差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン | |||
| study:java:powermock:mocking [2020/05/12 01:15] – [Mock Protected Parent Method] banana | study:java:powermock:mocking [2020/05/12 01:19] (現在) – [Mock Protected Parent Method] banana | ||
|---|---|---|---|
| 行 148: | 行 148: | ||
| Parent classにあるprotected methodの振る舞いを定義する例を紹介する。 | Parent classにあるprotected methodの振る舞いを定義する例を紹介する。 | ||
| + | 親クラスの例を次に示す。 | ||
| <code java> | <code java> | ||
| package parent; | package parent; | ||
| 行 154: | 行 155: | ||
| protected void foo(String arg1, String arg2) { | protected void foo(String arg1, String arg2) { | ||
| - | //Logic here | + | //some logic here |
| } | } | ||
| } | } | ||
| </ | </ | ||
| + | 子クラスの例を次に示す。 | ||
| <code java> | <code java> | ||
| package child; | package child; | ||
| 行 166: | 行 168: | ||
| public class Child extends Parent { | public class Child extends Parent { | ||
| - | public String | + | public String |
| //call parent method | //call parent method | ||
| this.foo(); | this.foo(); | ||
| 行 175: | 行 177: | ||
| </ | </ | ||
| + | テストクラスの例を次に示す。 | ||
| <code java> | <code java> | ||
| package child; | package child; | ||
| 行 185: | 行 188: | ||
| import org.powermock.core.classloader.annotations.PrepareForTest; | import org.powermock.core.classloader.annotations.PrepareForTest; | ||
| - | @PrepareForTest({ Parent.class, | + | @PrepareForTest({Parent.class, |
| public class ChildTest { | public class ChildTest { | ||
| //Class Under Test | //Class Under Test | ||
| 行 192: | 行 195: | ||
| @Before | @Before | ||
| public void setUp() throws Exception { | public void setUp() throws Exception { | ||
| - | // Partial mock to mock methods in parent class | + | //Partial mock to mock methods in parent class |
| cut = spy(new Child()); | cut = spy(new Child()); | ||
| 行 200: | 行 203: | ||
| @Test | @Test | ||
| - | public void testBoo() { | + | public void testBar() { |
| //call test method | //call test method | ||
| cut.bar(); | cut.bar(); | ||
| 行 208: | 行 211: | ||
| </ | </ | ||
| + | ここでは、親クラスのfooメソッドが呼ばれた際、何もしない(doNothing)を実装している。 | ||