[英]Can't suppress DriverManager's static initializer block
I have a unit test that attempts to create a SQLException
to simulate a database error. 我有一个单元测试,尝试创建一个
SQLException
以模拟数据库错误。 In SQLException
's constructor, there is a call to DriverManager
, which has a static initialization block. 在
SQLException
的构造函数中,有一个对DriverManager
的调用,该调用具有一个静态初始化块。 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);
}
}
When I run the test, the static block in DriverManager
is still called. 当我运行测试时,
DriverManager
的静态块仍然被调用。 What am I doing wrong? 我究竟做错了什么?
Clarifications 澄清
DriverManager
runs it's static initialization block, I get this exception: DriverManager
运行它的静态初始化块时,出现此异常: Oct 15, 2013 1:06:24 PM oracle.jdbc.driver.OracleDriver registerMBeans
2013年10月15日,下午1:06:24 oracle.jdbc.driver.OracleDriver registerMBeans
WARNING: Error while registering Oracle JDBC Diagnosability MBean.
警告:注册Oracle JDBC可诊断性MBean时出错。
java.lang.LinkageError: loader constraint violation: when resolving method "java.lang.management.ManagementFactory.getPlatformMBeanServer()Ljavax/management/MBeanServer;"
java.lang.LinkageError:违反加载程序约束:解决方法“ 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;
当前类的oracle / jdbc / driver / OracleDriver的类加载器(例如org / powermock / core / classloader / MockClassLoader的实例)和已解析的类java / lang / management / ManagementFactory的类加载器(的实例)具有javax / management / MBeanServer类型的不同Class对象; used in the signature
用于签名
SQLException
and never instantiate it directly. SQLException
而从不直接实例化它。 I would rather not go that route since it would mean updating 91 different unit tests. 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无法阻止静态初始化程序针对由系统或引导程序类加载器加载的类(例如包含
java.sql
软件包的jre类)运行。
After posting to the Powermock Google Group, I got this response : 发布到Powermock Google网上论坛后,我得到了以下回复 :
You can mock, suppress methods, stub methods etc in these classes since powermock 1.2.5 but you cannot suppress static initializers.
从powermock 1.2.5开始,您可以在这些类中模拟,抑制方法,存根方法等,但是您不能抑制静态初始化器。
See this page: Google Groups PowerMock Group 参见本页: Google网上论坛PowerMock网上论坛
You need to add to the class: @PowerMockIgnore("javax.management.*")
您需要添加到类中:
@PowerMockIgnore("javax.management.*")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.