2016-11-10 9 views
0

静的メソッドを束ねてユーティリティクラスをテストしようとしています。別の2つの静的メソッドを呼び出すメソッドのテストを書いています。最初の静的メソッド呼び出しでは、mockingはうまくいくように見えますが、2番目の期待を無視して、実際のメソッドを呼び出します。何か案が?私は第2の期待の前に2番目のPowerMock.spy(Utils.class)を提供しようとしましたが、運はありませんでした。どのようにこれを回避するための任意のアイデアですか?PowerMockitoと静的メソッド

ここにサンプルコードがあります。

package com.personal.test; 

import java.io.IOException; 

public class Utils { 

    public static String testUtilOne() { 
     System.out.println("Static method one"); 
     return "methodone"; 
    } 

    public static void testUtilsTwo(String s1, String s2, String s3) throws IOException { 
     throw new IOException("Throw an exception"); 
    } 

    public static void testUtilsThree() throws TestException { 
     try { 
      String s = testUtilOne(); 
      testUtilsTwo("s1", s, "s3"); 
     } catch (IOException ex) { 
      throw new TestException("there was an exception", ex); 
     } 
    } 

    public static class TestException extends Exception { 
     public TestException() { 
      super(); 
     } 
     public TestException(String message, Exception cause) { 
      super(message, cause); 
     } 
    } 
} 


    package com.personal.test; 

import java.io.IOException; 

import org.junit.Assert; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.mockito.Mockito; 
import org.powermock.api.mockito.PowerMockito; 
import org.powermock.core.classloader.annotations.PrepareForTest; 
import org.powermock.modules.junit4.PowerMockRunner; 

import com.personal.test.Utils.TestException; 

@RunWith(PowerMockRunner.class) 
@PrepareForTest({Utils.class}) 
public class UtilsTest { 

    @Test 
    public void testMethodThreeWrapsException() throws Exception { 
     PowerMockito.spy(Utils.class); 
     PowerMockito.when(Utils.class, "testUtilOne").thenReturn("This is coming from test"); 
     PowerMockito.spy(Utils.class); 
     PowerMockito.when(Utils.class, "testUtilsTwo", Mockito.anyString(), Mockito.anyString(), Mockito.anyString()).thenThrow(new IOException("Exception from test")); 
     try { 
      Utils.testUtilsThree(); 
     } catch (TestException ex) { 
      Assert.assertTrue(ex.getCause() instanceof IOException); 
      Assert.assertEquals("Exception from test", ex.getMessage()); 
     } 
    } 
} 
+0

ちょうど記録のために、あなたがする必要がある場合は、静的な模造のための* Power *フレームワークのみを使用してください。ほとんどの場合、人々がPowermockを求めているのは...テストコードが難しい/不可能なコードを作成したためです。彼らはPowermockを使用してそれを修正すると思います。いいえ、それはありません。本当の答えは良いOOテクニックを適用し、Powermockを使用しないことです - 。) – GhostCat

答えて

1

いずれかが興味があれば、一日を過ごした後、私はついにこの問題を回避しました。私はまず、testUtilsThreeの呼び出しのための実際のメソッドを呼び出す擬似スレッドよりも、私のUtilsクラスをstaticMockしました(これはすべての静的メソッドをモックします)。 変更点は次のとおりです。

 package com.personal.test; 

import java.io.IOException; 

import org.junit.Assert; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.mockito.Mockito; 
import org.powermock.api.mockito.PowerMockito; 
import org.powermock.core.classloader.annotations.PrepareForTest; 
import org.powermock.modules.junit4.PowerMockRunner; 

import com.personal.test.Utils.TestException; 

@RunWith(PowerMockRunner.class) 
@PrepareForTest({Utils.class}) 
public class UtilsTest { 

    @Test 
    public void testMethodThreeWrapsException() throws Exception { 
     PowerMockito.mockStatic(Utils.class); 
     PowerMockito.when(Utils.class, "testUtilOne").thenReturn("This is coming from test"); 
     PowerMockito.when(Utils.class, "testUtilsTwo", Mockito.anyString(), Mockito.anyString(), Mockito.anyString()).thenThrow(new IOException("Exception from test")); 
     PowerMockito.doCallRealMethod().when(Utils.class, "testUtilsThree"); 
     try { 
      Utils.testUtilsThree(); 
     } catch (TestException ex) { 
      Assert.assertTrue(ex.getCause() instanceof IOException); 
      Assert.assertEquals("Exception from test", ex.getMessage()); 
     } 
    } 
} 
+0

私は[ExpectedException](http://junit.org/junit4/javadoc/4.12/org/junit/rules/)の使用を提案したいと思います。 ExpectedException.html)代わりにcatchブロックを使用して例外をアサートしてください。 –

+0

ありがとうアーサー、私はダウンストリームコードは私のサンプルコードが表示されないカスタム例外にラップされている3つの異なる例外をスローするため、try catchを使用しました。だからGIVENダウンストリームコードに応じて別の例外がスローされます。 – ringadingding

関連する問題