PowerMock注解PowerMockIgnore的使用方法
来源:互联网 发布:淘宝天猫历史价格 编辑:程序博客网 时间:2024/05/29 19:09
故事要从一个异常开始,某天我在开发一个加密、解密特性,算法使用的是3DES,样例代码如下。
package org.jackie.study.powermock;import java.io.UnsupportedEncodingException;import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;/** * @author Jackie * */public class TripleDESAlgorithm { private static final String ALGORITHM = "DESede"; public static byte[] encrypt(String cryptKey, byte[] src) { try { SecretKey deskey = new SecretKeySpec(build3DesKey(cryptKey), ALGORITHM); Cipher c1 = Cipher.getInstance(ALGORITHM); c1.init(Cipher.ENCRYPT_MODE, deskey); return c1.doFinal(src); } catch (Exception e) { e.printStackTrace(); } return null; } public static byte[] decrypt(String cryptKey, byte[] src) { try { SecretKey deskey = new SecretKeySpec(build3DesKey(cryptKey), ALGORITHM); Cipher c1 = Cipher.getInstance(ALGORITHM); c1.init(Cipher.DECRYPT_MODE, deskey); return c1.doFinal(src); } catch (Exception e) { e.printStackTrace(); } return null; } static byte[] build3DesKey(String keyStr) throws UnsupportedEncodingException { byte[] key = new byte[24]; byte[] temp = keyStr.getBytes("UTF-8"); if (key.length > temp.length) { System.arraycopy(temp, 0, key, 0, temp.length); } else { System.arraycopy(temp, 0, key, 0, key.length); } return key; }}用来验证这个算法类的单元测试代码工作正常,运行结果显示上述加密、解密代码逻辑正确,没有错误,测试代码如下。
package org.jackie.study.powermock;import org.junit.Assert;import org.junit.Test;public class TripleDESAlgorithmTest { @Test public void test() throws Exception { String key = String.valueOf(System.currentTimeMillis()) + "jackie"; byte[] encrypted = TripleDESAlgorithm.encrypt(key, "helloworld".getBytes("UTF-8")); byte[] decrypted = TripleDESAlgorithm.decrypt(key, encrypted); Assert.assertEquals("helloworld", new String(decrypted, "UTF-8")); }}但当我使用单元测试代码来验证特性代码功能是否正常时,但恼人的事情发生了,测试代码抛出了诡异的异常,如下。
java.lang.ClassCastException: com.sun.crypto.provider.DESedeCipher cannot be cast to javax.crypto.CipherSpiat javax.crypto.Cipher.chooseProvider(Cipher.java:845)at javax.crypto.Cipher.init(Cipher.java:1213)at javax.crypto.Cipher.init(Cipher.java:1153)at org.jackie.study.powermock.TripleDESAlgorithm.encrypt(TripleDESAlgorithm.java:23)at org.jackie.study.powermock.SubscriberTest.testSayHello(SubscriberTest.java:19)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:601)at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)异常信息比较长,这里仅截取了一部分,但是可以说明问题。
业务代码和测试代码如下。
// 业务代码package org.jackie.study.powermock;public class Subscriber { private String name; // 解密收到的加密信息 public String sayHello(String name, byte[] message) throws Exception { byte[] realMessage = TripleDESAlgorithm.decrypt(name, message); if (realMessage != null) { this.name = name; return new String(realMessage, "UTF-8"); } return null; } public String getName() { return name; }}// 测试代码package org.jackie.study.powermock;import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.powermock.core.classloader.annotations.PrepareForTest;import org.powermock.modules.junit4.PowerMockRunner;@RunWith(PowerMockRunner.class)@PrepareForTestpublic class SubscriberTest {@Testpublic void testSayHello() throws Exception { byte[] encryptMessage = TripleDESAlgorithm.encrypt("jackie", "hello world!".getBytes("UTF-8")); Subscriber subscriber = new Subscriber(); subscriber.sayHello("jackie", encryptMessage); Assert.assertEquals("jackie", subscriber.getName());}}
异常很让人伤脑筋,因为反编译类com.sun.crypto.provider.DESedeCipher可以发现,这个算法实现类的确是javax.crypto.CipherSpi的子类,类转换失败实在是不应该。通常情况下如果出现ClassCastException,除了确实是类型不匹配外,还有一种可能即是两个类的ClassLoader不同,导致JVM认为这两个类没有关联。想到这里,这个问题基本上就有点思路了,因为PowerMock的工作原理即是使用自定义的类加载器来加载被修改过的类,从而达到打桩的目的,这里会不会是这个原因呢?
求助Google大师,发现在官网上有如下一段话。根据提示,我在测试类SubscriberTest前面加上了@PowerMockIgnore({"javax.crypto.*"}),保存之后重新运行测试代码,发现异常不再抛出,问题得到了解决。
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。
- PowerMock注解PowerMockIgnore的使用方法
- java注解的使用方法
- java注解的使用方法
- powermock
- powermock
- powermock
- mybatis的注解及使用方法
- PowerMock与Mockito的使用
- PowerMock处理特殊的单元测试
- 使用spring 的注解 @value使用方法
- Hibernate 常见的注解及使用方法
- java 注解的作用及使用方法详解
- 注解@JsonIgnore的使用方法及其效果
- 注解 java 注解的几大作用及使用方法详解
- java元注解和自定义注解的使用方法
- SpringMvc注解开发,各个注解的含义以及使用方法
- 一个牛x的mock框架--Powermock
- 一个牛x的mock框架--Powermock
- seo中的剩者为王
- Cow Sorting
- 知道这些,seo操作精华也就懂了
- uva 197 Cube(空间几何+回溯)
- 在sql*plus中显示语句执行所用的时间
- PowerMock注解PowerMockIgnore的使用方法
- 站长如何利用百度贴吧做好网络营销
- android之View的启动过程
- 【ACMclub周赛5】Problem E——TSP旅行商问题
- POJ 1459 Power Network
- [3] Android的init进程
- MU Puzzle(推理找规律)(多校第六场))
- seo培训常识
- 映射集合属性之⑤:Map集合属性