使用PowerMock模拟私有方法,但仍会调用底层方法

编程入门 行业动态 更新时间:2024-10-24 21:28:05
本文介绍了使用PowerMock模拟私有方法,但仍会调用底层方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在试图模拟出一个正在进行JNDI调用的私有方法。当从单元测试调用该方法时,它会抛出异常^。我想嘲笑这种方法用于测试目的。我使用来自其他问题的示例代码回答,当测试通过时,似乎仍然会调用底层方法。我在 doTheGamble()方法中插入了 System.err.println(),然后将其打印到我的控制台。

I am trying to mocking to mock out a private method that is making a JNDI call. When that method gets called from a unit test, it throws an exception^. I would like to mock-out that method for testing purposes. I used the sample code from another questions answer, and while the test passes, it seems that the underlying method still gets called. I inserted a System.err.println() in the doTheGamble() method, and it gets printed out to my console.

有趣的是,如果我注释掉第一个 assertThat ,则测试通过。 ?:(

Interesting enough, if I comment out the first assertThat, the test passes. ?:(

那么,如何模拟私有方法以便不调用它?

So, how do I mock out a private method so that it does not get called?

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,只有生产环境

^ understandably, since my workspace does not support JNDI, only the production environment does

%我使用的是所有库的最新版本,JUnit 4.10,Mockito 1.8.5,Hamcrest 1.1,Javassist 3.15.0和PowerMock 1.4.10。

% I am using the latest versions of all the library, JUnit 4.10, Mockito 1.8.5, Hamcrest 1.1, Javassist 3.15.0, and PowerMock 1.4.10.

推荐答案

来自 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"); }

因此,要将此应用于您的代码,我认为它可能会变成:

So to apply this to your code, I think it might become:

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

我刚刚在编辑器中对此进行了编码。实际上没有运行任何测试,并且在编写此代码时没有任何错误。

I just coded that in the editor here. No tests have actually been run, and no bugs have been harmed in the crafting of this code.

更多推荐

使用PowerMock模拟私有方法,但仍会调用底层方法

本文发布于:2023-10-31 06:34:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545302.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   底层   仍会   PowerMock

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!