PowerMockito使用PowerMockIgnore注解消除类加载器引入的ClassCastException
Posted nizuimeiabc1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PowerMockito使用PowerMockIgnore注解消除类加载器引入的ClassCastException相关的知识,希望对你有一定的参考价值。
The reason is that the XML framework tries to instantiate classes using reflection and does this from the thread context classloader (PowerMock‘s classloader) but then tries to assign the created object to a field not loaded by the same classloader. When this happens you need to make use of the @PowerMockIgnore annotation to tell PowerMock to defer the loading of a certain package to the system classloader. What you need to ignore is case specific but usually it‘s the XML framework or some packages that interact with it. E.g. @PowerMockIgnore({"org.xml.*", "javax.xml.*"}).
从上述描述中可以得到的信息是,假如待测试类中使用到了XML解析相关的包和类,那么测试类前同样需要增加@PowerMockIgnore({"org.xml.*", "javax.xml.*"}),消除类加载器引入的ClassCastException。
以上是关于PowerMockito使用PowerMockIgnore注解消除类加载器引入的ClassCastException的主要内容,如果未能解决你的问题,请参考以下文章
即使使用 Scala 测试的 @PrepareForTest,PowerMockito 也会抛出 ClassNotPreparedException