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

Posted

技术标签:

【中文标题】使用 PowerMock 模拟私有方法,但仍会调用底层方法【英文标题】:Mocked private method with PowerMock, but underlying method still gets called 【发布时间】:2011-12-24 20:23:24 【问题描述】:

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

很有趣,如果我注释掉第一个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 和 PowerMock 1.4.10。

【问题讨论】:

就我而言,只是忘记了@PrepareForTest 【参考方案1】:

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

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

【讨论】:

@Mike 你知道为什么doReturn(true).when(spy, "doTheGamble", anyString(), anyInt()); 有效但doReturn( true ).when( spy, method( CodeWithPrivateMethod.class, "doTheGamble", String.class, int.class ) ).withArguments( anyString(), anyInt() ) ; 导致IllegalArgumentException: argument type mismatch 吗?后者似乎更符合示例的风格,并且至少等效但不起作用。 我真的不能,对不起。我本人对 Mockito/PowerMock 还比较陌生……我以前从未尝试过以这种风格 (.withArguments(...)) 编写代码。 我完全没有根据的猜测是您正在尝试将intInteger 匹配... 我已经用 .withArguments 和不同的对象尝试过这个,但它似乎不起作用... PowerMockito.doReturn(true).when(spy, "doTheGamble", anyString(), anyInt( ));像魅力一样工作! @Christian - 请您指导***.com/questions/52064126/…。我们如何确保 100% 的代码覆盖率?【参考方案2】:

艺术乙,

只需粘贴在我的 Eclipse IDE 中运行良好的完整代码。我只是改变了我在上一篇文章中所说的期望。祝你好运。

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;
    
   

【讨论】:

【参考方案3】:

艺术乙,

您确定您的代码不起作用(或)我在这里遗漏了什么吗?我只是按照 Mike 建议的方式将您的方法期望替换为以下方法,并且效果很好:

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

我从没用过 Powermockito,但以前用过很多次。

【讨论】:

是的,我确定,一旦我开始工作,我会粘贴一些输出。【参考方案4】:

当您使用spy 构造一个模拟对象时,它是一个真正的半支持对象。我的猜测是摆脱spy。处理 pure 模拟对象。

【讨论】:

我正在尝试测试我正在监视的类的实现,所以这不是一个选项。【参考方案5】:

以上方法都不适合我,因为我的私有方法无效。

有这样的设置:

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

那是私有函数中的代码停止触发的时候

【讨论】:

以上是关于使用 PowerMock 模拟私有方法,但仍会调用底层方法的主要内容,如果未能解决你的问题,请参考以下文章

如何使用PowerMock调用私有方法并获取返回值?

使用PowerMock模拟私有字段和私有方法

无所不能的PowerMock,mock私有方法,静态方法,测试私有方法,final类

PowerMock - 无所不能的PowerMock,mock私有方法,静态方法,测试私有方法,final类

PowerMock - 无所不能的PowerMock,mock私有方法,静态方法,测试私有方法,final类

PowerMock 探索