PowerMockito 无法模拟私有方法 [英] PowerMockito is not able to mock private method

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

问题描述

@Service公共类 Topic_Service{私有字符串 test_help(){返回test_help";}公共字符串测试(){返回 test_help() + " " + "testing";}}//测试代码@SpringBootTest@RunWith(PowerMockRunner.class)@PrepareForTest(Topic_Service.class)类 Topic_Service_test{@InjectMocksprivate Topic_Service topic_service = PowerMockito.spy(newTopic_Service());@测试void testing_test() 抛出异常{PowerMockito.when(topic_service,"test_help").thenReturn("hello");字符串 s = topic_service.testing();Assertions.assertEquals("你好测试",s);}}//POM.XML 依赖<依赖><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></依赖><依赖><groupId>org.mockito</groupId><artifactId>mockito-core</artifactId><版本>2.8.47<范围>测试</范围></依赖><依赖><groupId>org.powermock</groupId><artifactId>powermock-module-junit4</artifactId><version>1.7.0</version><范围>测试</范围></依赖><依赖><groupId>org.powermock</groupId><artifactId>powermock-api-mockito2</artifactId><version>1.7.0</version><范围>测试</范围></依赖>

我的测试未通过并显示此错误:-

<块引用>

警告:发生了非法的反射访问操作警告:org.powermock.reflect.internal.WhiteboxImpl 的非法反射访问(文件:/home/an.kumar1/.m2/repository/org/powermock/powermock-reflect/1.7.0/powermock-reflect-1.7.0.jar) 到方法 java.lang.Object.finalize()警告:请考虑将此报告给 org.powermock.reflect.internal.WhiteboxImpl 的维护者警告:使用 --illegal-access=warn 启用进一步非法反射访问操作的警告警告:在未来的版本中将拒绝所有非法访问操作

org.mockito.exceptions.misusing.MissingMethodInvocationException:when() 需要一个参数,该参数必须是对模拟的方法调用".例如:when(mock.getArticles()).thenReturn(articles);

此外,出现此错误的原因可能是:1. 你存根:final/private/equals()/hashCode() 方法.那些方法不能被存根/验证.不支持在非公共父类上声明的模拟方法.2. 在 when() 中,您不会在模拟上调用方法,而是在其他对象上调用.

谁能帮帮我?提前致谢.

解决方案

您没有正确地存根私有方法.您需要使用 PowerMockito.stub 来存根私有方法.

以下是如何正确存根私有方法:

PowerMockito.stub(PowerMockito.method(Topic_Service.class,"test_help")).toReturn("hello");

我修改了你的代码如下:

<预><代码>@RunWith(PowerMockRunner.class)@PrepareForTest(Topic_Service.class)公共类 Topic_ServiceTest {@测试void testing_test() 抛出异常 {//设置Topic_Service topic_serviceSpy = PowerMockito.spy(new Topic_Service());PowerMockito.doReturn("Hello").when(topic_serviceSpy, "test_help");//锻炼字符串 s = topic_serviceSpy.testing();//核实Assertions.assertEquals("你好测试",s);}}

然后我通过传递参数 --illegal-access=permit 运行测试.

@Service
public class Topic_Service
{
   private String test_help()
   {
      return "test_help";
   }

   public String testing()
   {
      return test_help() + " " + "testing";
   }
}



//Test Code

@SpringBootTest
@RunWith(PowerMockRunner.class)
@PrepareForTest(Topic_Service.class)
class Topic_Service_test
{

   @InjectMocks
   private Topic_Service topic_service =  PowerMockito.spy(new 
   Topic_Service());

   @Test
   void testing_test() throws Exception
   {

     PowerMockito.when(topic_service,"test_help").thenReturn("hello");
     String s = topic_service.testing();
     Assertions.assertEquals("hello testing",s);
   }
}



//POM.XML Dependencies

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version> 2.8.47</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.7.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito2</artifactId>
        <version>1.7.0</version>
        <scope>test</scope>
    </dependency>

My test is not passing and showing this error:-

WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by org.powermock.reflect.internal.WhiteboxImpl (file:/home/an.kumar1/.m2/repository/org/powermock/powermock-reflect/1.7.0/powermock-reflect-1.7.0.jar) to method java.lang.Object.finalize() WARNING: Please consider reporting this to the maintainers of org.powermock.reflect.internal.WhiteboxImpl WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release

org.mockito.exceptions.misusing.MissingMethodInvocationException: when() requires an argument which has to be 'a method call on a mock'. For example: when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because: 1. you stub either of: final/private/equals()/hashCode() methods. Those methods cannot be stubbed/verified. Mocking methods declared on non-public parent classes is not supported. 2. inside when() you don't call method on mock but on some other object.

Can anyone help me out? Thanks in advance.

解决方案

You are not stubbing correctly a private method. You need to use PowerMockito.stub to stub private methods.

Below is how to stub correctly the private method:

PowerMockito.stub(PowerMockito.method(Topic_Service.class,"test_help")).toReturn("hello");

I modified your code as below:


@RunWith(PowerMockRunner.class)
@PrepareForTest(Topic_Service.class)
public class Topic_ServiceTest {

    @Test
    void testing_test() throws Exception {
        // Setup
        Topic_Service topic_serviceSpy = PowerMockito.spy(new Topic_Service());
        PowerMockito.doReturn("Hello").when(topic_serviceSpy, "test_help");
        // exercise
        String s = topic_serviceSpy.testing();
        // verify
        Assertions.assertEquals("hello testing",s);
    }

}

And I run the test with passing the argument --illegal-access=permit .

这篇关于PowerMockito 无法模拟私有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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