PowerMockito.mockStatic(HConnectionManager.class);
when(HConnectionManager.createConnection(configuration)).thenReturn(hConnection)


模拟HBase相关配置文件的方法是什么?当我得到:

java.lang.ExceptionInInitializerError
    at com.mapr.fs.ShimLoader.load(ShimLoader.java:214)
    at org.apache.hadoop.conf.CoreDefaultProperties.<clinit>(CoreDefaultProperties.java:60)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:274)
    at org.apache.hadoop.conf.Configuration.getClassByNameOrNull(Configuration.java:1857)
    at org.apache.hadoop.conf.Configuration.getProperties(Configuration.java:2072)
    at org.apache.hadoop.conf.Configuration.loadResource(Configuration.java:2282)
    at org.apache.hadoop.conf.Configuration.loadResources(Configuration.java:2234)
    at org.apache.hadoop.conf.Configuration.getProps(Configuration.java:2151)
    at org.apache.hadoop.conf.Configuration.get(Configuration.java:871)
    at org.apache.hadoop.conf.Configuration.getTrimmed(Configuration.java:890)
    at org.apache.hadoop.conf.Configuration.getBoolean(Configuration.java:1291)
    at org.apache.hadoop.hbase.HBaseConfiguration.checkDefaultsVersion(HBaseConfiguration.java:71)
    at org.apache.hadoop.hbase.HBaseConfiguration.addHbaseResources(HBaseConfiguration.java:107)
    at org.apache.hadoop.hbase.HBaseConfiguration.create(HBaseConfiguration.java:118)
    at org.apache.hadoop.hbase.client.HConnectionManager.<clinit>(HConnectionManager.java:266)
    at sun.reflect.GeneratedSerializationConstructorAccessor35.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:45)
    at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:73)
    at org.mockito.internal.creation.instance.ObjenesisInstantiator.newInstance(ObjenesisInstantiator.java:14)
    at org.powermock.api.mockito.repackaged.ClassImposterizer.createProxy(ClassImposterizer.java:143)
    at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:58)
    at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:49)
    at org.powermock.api.mockito.repackaged.CglibMockMaker.createMock(CglibMockMaker.java:24)
    at org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:46)
    at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)
    at org.mockito.Mockito.mock(Mockito.java:1285)
    at org.mockito.Mockito.mock(Mockito.java:1163)

最佳答案

这不是一个直接的解决方案,而是一个合理的解决方法:我假设您想使用PowerMock来测试您的代码,必须进行此静态调用,然后使用返回的结果。

因此,您可以围绕此功能创建包装器,而不是“直接”进行此静态调用,例如:

interface ConnectionProvider {
  public HConneciotn createConnection(configuration);


和实现该方法的相应类(实际上是通过进行静态调用)。

现在,您可以更改其他生产代码以与该新界面一起使用。现在,无需使用PowerMokito用户就可以测试所有客户端代码,因为不再需要静态调用。

当然,您可能想使用PowerMokito来确保新包装程序实现完全按照预期的方式运行。

10-07 16:32