使用Eclipse 3.6.1
Java 1.6.0_26
JMockit 0.999.10
更新:我能够创建一个SSCCE,我将在下面发布:
UPDATE2:添加了DaemonHelper。另外,如果我从构造函数中删除DaemonHelper,我会得到一个ClassCircularityError。有关?
当我从类加载器中获取NPE时,我正在尝试对类进行部分模拟,同时模拟类的几个依赖项。 NPE调用堆栈显示在下面
我有Daemon
类,如此:
public class Daemon extends DaemonParent {
public Daemon(ConfigParent config, DaemonHelper helper) {
super(config, helper);
}
@Override
public void execute() {
log("Starting");
if (config.test()) {
log("Testing");
}
log("Ending");
}
}
守护进程有一个像这样的父母:
public abstract class DaemonParent extends Thread {
protected ConfigParent config;
protected DaemonHelper helper;
public DaemonParent(ConfigParent config, DaemonHelper helper) {
this.config = config;
this.helper = helper;
}
public abstract void execute();
public void log(String s) {
System.out.println(s);
}
}
有一个辅助类,如:
public class DaemonHelper extends Thread {
}
Config看起来像:
public class ConfigParent {
protected ConfigHelper helper;
public ConfigParent(ConfigHelper helper) {
this.helper = helper;
}
public boolean test() {
return false;
}
}
Config Parent看起来像:
public class ConfigParent implements Serializable {
protected ConfigHelper helper;
public ConfigParent(ConfigHelper helper) {
this.helper = helper;
}
public boolean test() {
return false;
}
}
测试看起来像:
public class DaemonTest {
@Test
public void testExecute(final ConfigHelper ch, final DaemonHelper dh) {
final Config c = new Config(ch);
final Daemon d = new Daemon(c, dh);
new NonStrictExpectations(d) {
{
}
};
d.execute();
new Verifications() {{
d.log("Starting");
d.log("Ending");
}};
}
}
当我尝试运行此测试时,出现以下错误。请注意,当DaemonParent和Daemon Helper将java.lang.Thread作为超类时,只会弹出此错误:
java.lang.NullPointerException
at java.lang.Thread.interrupted(Unknown Source)
at sun.misc.Resource.getBytes(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.junit.internal.runners.model.EachTestNotifier.addFailure(EachTestNotifier.java:23)
at org.junit.runners.ParentRunner.run(ParentRunner.java:242)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
我尝试评论NonStrictExpectations
块和Verifications
块,我得到了同样的错误。
NPE是从对currentThread()的调用中抛出的......我不知道它是如何失败的。
有什么想法吗?
答案 0 :(得分:1)
事实证明守护进程的根超类是java.lang.Thread
的问题导致JMockit遇到麻烦。解决方案是测试所需方法的静态部分模拟。
public class DaemonTest {
@Test
public void testExecute(final ConfigHelper ch, final DaemonHelper dh) {
new NonStrictExpectations(d) {
@Mocked({"log"}
Daemon d;
@Mocked({"()"})
DaemonHelper dh;
{
}
};
final Config c = new Config(ch);
final Daemon d = new Daemon(c, dh);
d.execute();
new Verifications() {{
d.log("Starting");
d.log("Ending");
}};
}
}