差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
study:java:powermock:mocking [2020/05/12 01:13] – [Mock Protected Parent Method] bananastudy: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> </code>
  
 +子クラスの例を次に示す。
 <code java> <code java>
 package child; package child;
行 166: 行 168:
 public class Child extends Parent { public class Child extends Parent {
  
-    public String boo() {+    public String bar() {
         //call parent method         //call parent method
         this.foo();         this.foo();
行 175: 行 177:
 </code> </code>
  
 +テストクラスの例を次に示す。
 <code java> <code java>
 package child; package child;
  
 import parent.Parent; import parent.Parent;
 +import static org.mockito.Matchers.anyObject;
 +import static org.powermock.api.mockito.PowerMockito.doNothing;
 +import static org.powermock.api.mockito.PowerMockito.spy;
 +import static org.powermock.api.mockito.PowerMockito.when;
 +import org.powermock.core.classloader.annotations.PrepareForTest;
  
-@PrepareForTest({ Parent.class, Child.class })+@PrepareForTest({Parent.class, Child.class})
 public class ChildTest { public class ChildTest {
     //Class Under Test     //Class Under Test
行 187: 行 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());
  
行 195: 行 203:
  
     @Test     @Test
-    public void testBoo() {+    public void testBar() {
         //call test method         //call test method
         cut.bar();         cut.bar();
行 203: 行 211:
  
 </code> </code>
 +ここでは、親クラスのfooメソッドが呼ばれた際、何もしない(doNothing)を実装している。
  

QR Code
QR Code study:java:powermock:mocking (generated for current page)