jMockit异常

hw19900825 hw19900825 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
纯新人刚学jMockit,写了个测试方法想Mock目标方法中new的一个类。
目标代码
public void writeByteToFile(final String destination, final byte[] fileContents) throws IOException {
//这里省略针对文件正确性的测试,排除简单,可手动模拟的错误
FileOutputStream opStream = null;
try {
opStream = new FileOutputStream(destination);
opStream.write(fileContents);
opStream.flush();
opStream.close();
} catch (IOException e) {
throw new IOException(destination , e.getCause());
}
}
想用JMockit 去Mock FileOutputStream 的构造方法,返回一个Mock的FileOutputStream类,然后Mock write方法抛出IO异常
测试代码
@Mocked
FileOutputStream fileOutputStream;
@Test
public void testCreateFile008() {
String strFileName="//testfile//CreateFile.txt";
final byte[] byteDate={0x17,0x18,0x19,0x20,0x21};
try{
new Expectations(FileOutputStream.class) {
{new FileOutputStream(withInstanceOf(String.class));
result = fileOutputStream;
fileOutputStream.write(byteDate);;
result = new IOException();}
};
FileUtils fileUtils=new FileUtils();
fileUtils.writeByteToFile(strFileName, byteDate);
}catch (Exception e){
}
}
结果执行到 new Expectations(FileOutputStream.class) {这就报错
java.lang.ClassCircularityError: org/junit/internal/AssumptionViolatedException
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at jp.co.dgic.eclipse.jdt.internal.junit.runner.DJUnitRunner.main(DJUnitRunner.java:49)
System class loader does not support adding JAR file to system class path during the live phase!
Unable to add C:\WorkSpace\MSAD\lib\jmockit-1.8.jar to system class path - not supported by system class loader or configuration error!
用的Junit4.11,jMockit1.8的Jar包,后面好像说什么JMockit的包不能再运行阶段读取,不理解。

求大神指教
...全文
评论
收藏 已收藏
分享
返回顶部
hw19900825
Java 51,407 成员 86,128 内容