gpt4 book ai didi

java - 使用 PowerMock 模拟私有(private)方法,但仍会调用底层方法

转载 作者:IT老高 更新时间:2023-10-28 21:18:44 25 4
gpt4 key购买 nike

我正在尝试模拟一个进行 JNDI 调用的私有(private)方法。当从单元测试中调用该方法时,它会引发异常^。我想模拟该方法以进行测试。我用了sample code from another questions answer ,并且在测试通过时,似乎仍然调用了底层方法。我在 doTheGamble() 方法中插入了一个 System.err.println(),它被打印到我的控制台。

很有趣,如果我注释掉第一个 assertThat,测试就通过了。 ?:(

那么,我如何模拟一个私有(private)方法以使其不被调用?

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 和 PowerMock 1.4.10。

最佳答案

来自 PowerMock Private Method Example :

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

我只是在此处的编辑器中编写了代码。没有实际运行任何测试,并且在编写此代码时没有任何错误受到损害。

关于java - 使用 PowerMock 模拟私有(private)方法,但仍会调用底层方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8057607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com