无法抑制DriverManager的静态初始化程序块 [英] Can't suppress DriverManager's static initializer block

查看:321
本文介绍了无法抑制DriverManager的静态初始化程序块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单元测试,试图创建一个SQLException以模拟数据库错误.在SQLException的构造函数中,有一个对DriverManager的调用,该调用具有一个静态初始化块.我发现可以使用这种类型的设置来抑制静态块:

I have a unit test that attempts to create a SQLException to simulate a database error. In SQLException's constructor, there is a call to DriverManager, which has a static initialization block. I figured that I could suppress the static block with this type of setup:

@RunWith(PowerMockRunner.class)
@SuppressStaticInitializationFor({"java.sql.DriverManager"})
public class StaticTest 
{
    @Test
    public void testStaticSuppress() throws Exception
    {
        SQLException ex = new SQLException();
        expect(...).andThrow(ex);
    }
}

运行测试时,仍会调用DriverManager中的静态块.我在做什么错了?

When I run the test, the static block in DriverManager is still called. What am I doing wrong?

说明

  • 我正在运行Powermock 1.5-给人的印象是,使用v1.5可以模拟系统类
  • DriverManager运行它的静态初始化块时,出现此异常:
  • I am running Powermock 1.5 - I was under the impression that using v1.5 allows me to mock system classes
  • When DriverManager runs it's static initialization block, I get this exception:

2013年10月15日下午1:06:24 oracle.jdbc.driver.OracleDriver registerMBeans

Oct 15, 2013 1:06:24 PM oracle.jdbc.driver.OracleDriver registerMBeans

警告:注册Oracle JDBC可诊断性MBean时出错.

WARNING: Error while registering Oracle JDBC Diagnosability MBean.

java.lang.LinkageError:违反加载程序约束:解决方法"java.lang.management.ManagementFactory.getPlatformMBeanServer()Ljavax/management/MBeanServer;"时当前类的oracle/jdbc/driver/OracleDriver的类加载器(例如org/powermock/core/classloader/MockClassLoader的实例)和已解析的类java/lang/management/ManagementFactory的类加载器(的实例)具有javax/management/MBeanServer类型的不同Class对象;在签名中使用

java.lang.LinkageError: loader constraint violation: when resolving method "java.lang.management.ManagementFactory.getPlatformMBeanServer()Ljavax/management/MBeanServer;" the class loader (instance of org/powermock/core/classloader/MockClassLoader) of the current class, oracle/jdbc/driver/OracleDriver, and the class loader (instance of ) for resolved class, java/lang/management/ManagementFactory, have different Class objects for the type javax/management/MBeanServer; used in the signature

  • 我意识到我可以对SQLException进行模拟,并且永远不要直接实例化它.我宁愿不走那条路,因为那将意味着更新91个不同的单元测试.我问了这个问题,因为根据PowerMock文档,我的代码看起来应该可以正常工作.
    • I realize that I could make a mock of SQLException and never instantiate it directly. I would rather not go that route since it would mean updating 91 different unit tests. I asked the question because it looked like my code should work just fine according to the PowerMock docs.
    • 推荐答案

      我怀疑(但不确定)Powermock无法阻止静态初始化程序针对由系统或引导程序类加载器加载的类(例如包括软件包java.sql的jre类).

      I suspect (but I am not certain) that Powermock is unable to prevent the static initializer from running for classes that are loaded by the system or bootstrap classloader (like the jre classes including those of the package java.sql are).

      发布到Powermock Google网上论坛后,我得到了此回复 :

      After posting to the Powermock Google Group, I got this response:

      从powermock 1.2.5开始,您可以在这些类中模拟,抑制方法,存根方法等,但是您不能抑制静态初始化器.

      You can mock, suppress methods, stub methods etc in these classes since powermock 1.2.5 but you cannot suppress static initializers.

      这篇关于无法抑制DriverManager的静态初始化程序块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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