简体   繁体   English

使用Powermock模拟静态私有最终变量?

[英]Mocking static private final variable using Powermock?

I have a utility class which is a final class. 我有一个实用程序类,它是最后一堂课。 There i have used injection for injecting LOGGER. 在那里,我使用注入来注入LOGGER。

public final class Utilities {

    @Inject
    private static Logger.ALogger LOGGER;

    private Utilities() {
       //this is the default constructor. so there is no implementation
    }

    public static String convertToURl(string input){
       try{
             //do some job
          }catch(IllegalArgumentException ex){
             LOGGER.error("Invalid Format", ex);
          }
   }

}

While I writing unit testing for this method i have to mock LOGGER otherwise it will throw null pointer exception. 当我为此方法编写单元测试时,我必须模拟LOGGER,否则它将抛出空指针异常。 How can i mock this LOGGER without creating instance of this class. 我如何在不创建此类实例的情况下模拟此LOGGER。 I tried to whitebox the variable. 我试图将变量白名单化。 But it only works with instances? 但这仅适用于实例吗?

This code works fine. 此代码可以正常工作。 To set static field you need to pass a class to org.powermock.reflect.Whitebox.setInternalState . 要设置静态字段,您需要将一个类传递给org.powermock.reflect.Whitebox.setInternalState Please, ensure that you use PowerMock's class from the package org.powermock.reflect because Mockito has the class with the same name. 请确保使用软件包org.powermock.reflect PowerMock的类,因为Mockito的类具有相同的名称。

 @RunWith(PowerMockRunner.class)
 public class UtilitiesTest {

    @Mock
    private Logger.ALogger aLogger;

    @Before
    public void setUp() throws Exception {

        MockitoAnnotations.initMocks(this); // for case them used another runner
        Whitebox.setInternalState(CcpProcessorUtilities.class, "LOGGER", aLogger);
    }

    @Test
    public void testLogger() throws Exception {
        Utilities.convertToURl("");
        verify(aLogger).error(eq("Invalid Format"), any(IllegalArgumentException.class));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Powermock:模拟静态最终记录器 - Powermock: Mocking static final logger 无法使用 PowerMock 覆盖最终类的静态最终变量 - Unable to override static final variable of a final class using PowerMock 使用Powermock的@Mock中的Nullpointer(模拟最终的静态Hashtable) - Nullpointer in a @Mock (mocking final static Hashtable) with Powermock 使用Mockito / Powermock框架模拟最终私有变量 - Mock final private variable using Mockito/Powermock framework 使用Mockito和反射来模拟此私有静态最终变量,我做错了什么? - What am I doing wrong mocking this private static final variable using mockito and reflection? 如何使用JUnit,EasyMock或PowerMock模拟静态最终变量 - How to mock a static final variable using JUnit, EasyMock or PowerMock 我该如何模拟通过Powermock和Mockito通过私有构造函数初始化的私有static final字段? - How do I mock a private static final field initialized via a private constructor using Powermock and Mockito? 使用PowerMock模拟私有方法 - Mocking private method with PowerMock 在最终(实用程序)类中模拟私有静态方法 - Mocking private static method in a final (utility) class 是否可以在 PowerMock 中对私有静态方法使用部分模拟? - Is it possible to use partial mocking for private static methods in PowerMock?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM