PowerMockを使用してテストするためのプライベートメソッドを模倣する方法は?
質問
プライベートな方法を呼び出す公開方法でテストしたいクラスがあります。プライベートメソッドは正しく機能すると仮定したいと思います。たとえば、ようなものが欲しいです doReturn....when...
. 。あることがわかりました PowerMockを使用した可能なソリューション, 、しかし、このソリューションは私にとってはうまくいきません。それはどのようにできますか?誰かがこの問題を抱えていましたか?
解決
ここでは問題はありません。 Mockito APIを使用して次のコードを使用して、私はそれをどうやってそれを行うことができました:
public class CodeWithPrivateMethod {
public void meaningfulPublicApi() {
if (doTheGamble("Whatever", 1 << 3)) {
throw new RuntimeException("boom");
}
}
private boolean doTheGamble(String whatever, int binary) {
Random random = new Random(System.nanoTime());
boolean gamble = random.nextBoolean();
return gamble;
}
}
そして、これがJunitテストです:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.support.membermodification.MemberMatcher.method;
@RunWith(PowerMockRunner.class)
@PrepareForTest(CodeWithPrivateMethod.class)
public class CodeWithPrivateMethodTest {
@Test(expected = RuntimeException.class)
public void when_gambling_is_true_then_always_explode() throws Exception {
CodeWithPrivateMethod spy = PowerMockito.spy(new CodeWithPrivateMethod());
when(spy, method(CodeWithPrivateMethod.class, "doTheGamble", String.class, int.class))
.withArguments(anyString(), anyInt())
.thenReturn(true);
spy.meaningfulPublicApi();
}
}
他のヒント
あらゆるテストフレームワークで動作する一般的なソリューション(もしも あなたのクラスは非ですfinal
)独自のモックを手動で作成することです。
- プライベート方法を保護してください。
- テストクラスでクラスを拡張します
- 以前のプライベート方法をオーバーライドして、必要な定数を返す
これはフレームワークを使用しないため、エレガントではありませんが、常に機能します。PowerMockがなくても。または、ステップ#1を既に行った場合は、Mockitoを使用して手順#2&#3を実行できます。
プライベートメソッドを直接ock笑するには、に示すようにPowerMockを使用する必要があります その他の答え.
私はあなたがプライベートな機能と呼ぶことができる方法を知っていますockitoでテストする
@Test
public void commandEndHandlerTest() throws Exception
{
Method retryClientDetail_privateMethod =yourclass.class.getDeclaredMethod("Your_function_name",null);
retryClientDetail_privateMethod.setAccessible(true);
retryClientDetail_privateMethod.invoke(yourclass.class, null);
}
何らかの理由で、ブライスの答えは私のためには機能していません。私はそれをうまく操作するためにそれを少し操作することができました。 PowerMockの新しいバージョンがあるからかもしれません。 1.6.5を使用しています。
import java.util.Random;
public class CodeWithPrivateMethod {
public void meaningfulPublicApi() {
if (doTheGamble("Whatever", 1 << 3)) {
throw new RuntimeException("boom");
}
}
private boolean doTheGamble(String whatever, int binary) {
Random random = new Random(System.nanoTime());
boolean gamble = random.nextBoolean();
return gamble;
}
}
テストクラスは次のように見えます:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.doReturn;
@RunWith(PowerMockRunner.class)
@PrepareForTest(CodeWithPrivateMethod.class)
public class CodeWithPrivateMethodTest {
private CodeWithPrivateMethod classToTest;
@Test(expected = RuntimeException.class)
public void when_gambling_is_true_then_always_explode() throws Exception {
classToTest = PowerMockito.spy(classToTest);
doReturn(true).when(classToTest, "doTheGamble", anyString(), anyInt());
classToTest.meaningfulPublicApi();
}
}
議論なし:
ourObject = PowerMockito.spy(new OurClass());
when(ourObject , "ourPrivateMethodName").thenReturn("mocked result");
と String
口論:
ourObject = PowerMockito.spy(new OurClass());
when(ourObject, method(OurClass.class, "ourPrivateMethodName", String.class))
.withArguments(anyString()).thenReturn("mocked result");
所属していません StackOverflow