当新功能不生效时,PowerMockito模拟 [英] PowerMockito Mocking whenNew not taking effect

查看:122
本文介绍了当新功能不生效时,PowerMockito模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说明:

我似乎无法让我的存根或嘲笑在我所测的课程中起作用。我正在尝试使用whenNew动作,以便可以模拟返回对象,然后使用返回值模拟对该对象的操作。

I cannot seem to have my stubs or mocks take affect in the class I have under test. I am trying to use the whenNew action so I can mock a return object and then mock a operation on that object with a returned value.

我想它很简单,我想念但看不到。

I imagine its something simple I am missing but not seeing it.

解决方案:最初,我使用的是 MockitoRunner.class ,它需要更改为 PowerMockRunner.class 。下面的代码反映了解决方案。

SOLUTION: Originally I was running with MockitoRunner.class and it required being changed to PowerMockRunner.class. Code below reflects the solution.

类路径上的瓶子:


  • powermock-mockito-1.4.11- full.jar

  • mockoito-all-1.9.0.jar

  • javassist-3.15.0-GA.jar

  • junit-4.8.2.jaf

  • objensis-1.2.jar

  • cglib-nodep-2.2.2.jar

  • powermock-mockito-1.4.11-full.jar
  • mockoito-all-1.9.0.jar
  • javassist-3.15.0-GA.jar
  • junit-4.8.2.jaf
  • objensis-1.2.jar
  • cglib-nodep-2.2.2.jar

测试等级

   import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.powermock.api.mockito.PowerMockito;
    import static org.powermock.api.mockito.PowerMockito.*;
    import org.powermock.core.classloader.annotations.PrepareForTest;
    import org.powermock.modules.junit4.PowerMockRunner;
    import static org.mockito.Matchers.any;
    @RunWith(PowerMockRunner.class)
    @PrepareForTest(ClassA.class)
    public class ClassATest {

        @Test
        public void test() throws Exception
        {
                String[] returnSomeValue = {"PowerMockTest"};
                String[] inputValue = {"Test1"};
                ClassB mockedClassB = mock(ClassB.class);
                whenNew( ClassB.class).withNoArguments().thenReturn( mockedClassB );
                when( mockedClassB, "getResult", any(String[].class) ).thenReturn(returnSomeValue);       

                IClassA classUnderTest = new ClassA();
                String[] expectedValue = classUnderTest.runTest(inputValue);      
        }

    }

A类实施

public class ClassA implements IClassA {

    @Override
    public String[] runTest(String[] inputValues) {

        String[] result;
        IClassB classB = new ClassB();
        result = classB.getResult(inputValues);

        return result;
    }

} 


推荐答案

由于您正在使用powermock功能( @PrepareForTest PowerMockito.whenNew 等),因此必须运行

Since you are using powermock features (@PrepareForTest, PowerMockito.whenNew etc.), you have to run your test with the PowerMockRunner.

@RunWith(PowerMockRunner.class)

由于ClassB#geResult不是私有的,因此您也可以简化代码并替换

Because ClassB#geResult is not private, you may also simplify your code and replace

when( mockedClassB, "getResult", any(String[].class) ).thenReturn(someValue); 

by

when(mockedClassB.getResult(any(String[].class))).thenReturn(someValue);

这篇关于当新功能不生效时,PowerMockito模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆