28

JNDI呼び出しを行うプライベートメソッドをモックアウトしようとしています。そのメソッドが単体テストから呼び出されると、例外がスローされます^。テスト目的でそのメソッドをモックアウトしたいと思います。別の質問の回答のサンプルコードを使用しましたが、テストに合格しても、基になるメソッドが呼び出されているようです。メソッドにを挿入するSystem.err.println()doTheGamble()、コンソールに出力されます。

興味深いことに、最初にコメントアウトするassertThatと、テストに合格します。?:(

では、プライベートメソッドが呼び出されないように、どのようにモックアウトするのですか?

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
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;

import java.util.Random;

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;

@RunWith(PowerMockRunner.class)
@PrepareForTest(CodeWithPrivateMethod.class)
public class PowerMock_Test {

    static boolean gambleCalled = false; 

    @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);

/* 1 */ assertThat( PowerMock_Test.gambleCalled, is(false) );
        spy.meaningfulPublicApi();
/* 2 */ assertThat( PowerMock_Test.gambleCalled, is(false) );
    }
}


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();

        System.err.println( "\n>>> GAMBLE CALLED <<<\n" );
        PowerMock_Test.gambleCalled = true;

        return gamble;
    }
}   

^当然のことながら、私のワークスペースはJNDIをサポートしていないため、実稼働環境のみがサポートしています。

%すべてのライブラリの最新バージョンであるJUnit 4.10、Mockito 1.8.5、Hamcrest 1.1、Javassist 3.15.0、およびPowerMock1.4.10を使用しています。

4

5 に答える 5

37

PowerMockプライベートメソッドの例から:

@RunWith(PowerMockRunner.class)
// We prepare PartialMockClass for test because it's final or we need to mock private or static methods
@PrepareForTest(PartialMockClass.class)
public class YourTestCase {
@Test
public void privatePartialMockingWithPowerMock() {        
    PartialMockClass classUnderTest = PowerMockito.spy(new PartialMockClass());

    // use PowerMockito to set up your expectation
    PowerMockito.doReturn(value).when(classUnderTest, "methodToMock", "parameter1");

    // execute your test
    classUnderTest.execute();

    // Use PowerMockito.verify() to verify result
    PowerMockito.verifyPrivate(classUnderTest, times(2)).invoke("methodToMock", "parameter1");
}

したがって、これをコードに適用すると、次のようになる可能性があります。

@RunWith(PowerMockRunner.class)
@PrepareForTest(CodeWithPrivateMethod.class)
public class PowerMock_Test {
    @Test(expected = RuntimeException.class)
    public void when_gambling_is_true_then_always_explode() throws Exception {
        CodeWithPrivateMethod spy = PowerMockito.spy(new CodeWithPrivateMethod());

        PowerMockito.doReturn(true).when(spy, "doTheGamble", anyString(), anyInt());


/* 1 */ PowerMockito.verifyPrivate(spy, times(0)).invoke("doTheGamble", anyString(), anyInt());            
        spy.meaningfulPublicApi();
/* 2 */ PowerMockito.verifyPrivate(spy, times(2)).invoke("doTheGamble", anyString(), anyInt());            
    }
}

ここのエディターでコーディングしました。実際に実行されたテストはなく、このコードの作成で害を受けたバグもありません。

于 2011-11-08T22:19:32.160 に答える
3

ArtB、

EclipseIDEで正常に機能する完全なコードを貼り付けるだけです。前回の投稿で言った期待を変えただけです。幸運を。

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.support.membermodification.MemberMatcher.method;

import java.util.Random;

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;

@RunWith(PowerMockRunner.class)
@PrepareForTest(CodeWithPrivateMethod.class)
public class PowerMock_Test {

    static boolean gambleCalled = false; 

    @Test(expected = RuntimeException.class)
    public void when_gambling_is_true_then_always_explode() throws Exception {
        CodeWithPrivateMethod spy = PowerMockito.spy(new CodeWithPrivateMethod());

//        PowerMockito.doReturn(true).when(spy, "doTheGamble", anyString(), anyInt());

        PowerMockito.doReturn(true).when(spy, 
               method(CodeWithPrivateMethod.class, "doTheGamble", String.class, int.class))
                .withArguments(anyString(), anyInt());

        assertThat( PowerMock_Test.gambleCalled, is(false) );
        spy.meaningfulPublicApi();
        assertThat( PowerMock_Test.gambleCalled, is(false) );
    }
}


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();

        System.err.println( "\n>>> GAMBLE CALLED <<<\n" );
        PowerMock_Test.gambleCalled = true;

        return gamble;
    }
}   
于 2011-11-10T16:02:34.193 に答える
1

ArtB、

あなたのコードが機能しないのは確かですか(または)私はここで何かが欠けていますか?私はあなたのメソッドの期待をマイクが提案した方法で次のものに置き換えました、そしてそれはうまくいきます:

PowerMockito.doReturn(true).when(spy, 
               method(CodeWithPrivateMethod.class, "doTheGamble", String.class, int.class))
                .withArguments(anyString(), anyInt());

私はPowermockitoを使用したことはありませんが、以前はMockitoをよく使用していました。

于 2011-11-09T22:44:00.270 に答える
0

モックオブジェクトを作成するために使用spyする場合、それは実際のハーフバックオブジェクトです。私の推測では、を取り除くことですspy純粋なモックオブジェクトで作業します。

于 2011-11-08T21:59:03.397 に答える
0

私のプライベートメソッドが無効だったので、上記のどれも私にはうまくいきませんでした。

次のような設定がありました:

public class MainClass {
    public void publicFunction () {
      this.privateFunction(Arg1.class arg1, Arg2.class arg2);
    }

    private void privateFunction(Arg1.class arg1, Arg2.class arg2){
       // who cares, I don't want any of this to go off
    }

}

うまくいったのは:

@Test
public void whocares() throws Exception {
   mainSpy = spy(mockedMainClassInstance);
   PowerMockito.doNothing().when(mainSpy, "privateFunction", mockedArg1, mockedArg2);
   mainSpy.publicFunction(mockedArg0, mockedArg1, mockedArg2);
   verifyPrivate(mainSpy).invoke("privateFunction", mockedArg1, mockedArg2);

}

プライベート関数のコードが起動を停止したのはそのときです

于 2020-05-18T21:58:18.840 に答える