我正在尝试让 Thread Weaver 工作,但无论我尝试什么,我总是遇到下面的异常。
我创建了一个干净的项目,其中仅包含 EasyMock、JUnit 和 Thread Weaver,代码是 Thread Weaver 文档中的示例(我使用 EasyMock 4.2,并使用 JDK 15 和 JDK 9 以及 JUnit 4 和 JUnit 进行了尝试5).
有人知道吗...?
测试类:
import java.util.ArrayList;
public class MyList extends ArrayList {
synchronized public boolean putIfAbsent(Object o) {
boolean absent = !super.contains(o);
if (absent) {
super.add(o);
}
return absent;
}
}
测试:
import com.google.testing.threadtester.*;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class MyListTest {
private MyList list;
@Test
public void testThreading() {
AnnotatedTestRunner runner = new AnnotatedTestRunner();
// Run all Weaver tests in this class, using MyList as the Class Under Test.
runner.runTests(this.getClass(), MyList.class);
}
@ThreadedBefore
public void before() {
list = new MyList();
}
@ThreadedMain
public void mainThread() {
list.putIfAbsent("A");
}
@ThreadedSecondary
public void secondThread() {
list.putIfAbsent("A");
}
@ThreadedAfter
public void after() {
assertEquals(1, list.size());
}
}
例外:
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.google.testing.threadtester.MethodCaller.invoke(MethodCaller.java:71)
at com.google.testing.threadtester.BaseThreadedTestRunner.runTests(BaseThreadedTestRunner.java:210)
at com.google.testing.threadtester.BaseThreadedTestRunner.runTests(BaseThreadedTestRunner.java:174)
at MyListTest.testThreading(MyListTest.java:14)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at com.google.testing.threadtester.MethodCaller.invoke(MethodCaller.java:68)
... 24 more
Caused by: java.lang.RuntimeException: Cannot instrument class
at com.google.testing.threadtester.TestInstrumenter.instrument(TestInstrumenter.java:446)
at com.google.testing.instrumentation.InstrumentedClassLoader.findClass(InstrumentedClassLoader.java:138)
at com.google.testing.instrumentation.InstrumentedClassLoader.loadClass(InstrumentedClassLoader.java:114)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:292)
at com.google.testing.threadtester.AnnotatedTestWrapper.runTests(AnnotatedTestWrapper.java:272)
... 29 more
Caused by: javassist.CannotCompileException: [source error] no such class: com.google.testing.threadtester.CallLogger
at javassist.CtField.make(CtField.java:167)
at com.google.testing.threadtester.TestInstrumenter.addDeclaration(TestInstrumenter.java:501)
at com.google.testing.threadtester.TestInstrumenter.processClass(TestInstrumenter.java:470)
at com.google.testing.threadtester.TestInstrumenter.instrument(TestInstrumenter.java:444)
... 35 more
Caused by: compile error: no such class: com.google.testing.threadtester.CallLogger
at javassist.compiler.MemberResolver.searchImports(MemberResolver.java:468)
at javassist.compiler.MemberResolver.lookupClass(MemberResolver.java:412)
at javassist.compiler.MemberResolver.lookupClassByJvmName(MemberResolver.java:319)
at javassist.compiler.MemberResolver.lookupClass(MemberResolver.java:336)
at javassist.compiler.MemberResolver.lookupClass(MemberResolver.java:323)
at javassist.compiler.Javac.compileField(Javac.java:133)
at javassist.compiler.Javac.compile(Javac.java:93)
at javassist.CtField.make(CtField.java:162)
... 38 more
编辑:
澄清一下 - 库已经存在并且项目可以编译。该错误仅在我实际运行测试时发生
我在使用 jdk17 时遇到了与您相同的问题,然后我注意到我没有添加该库所需的所有依赖项。您似乎错过了以下内容:
我也犯了一个错误,你没有,但只是想添加它以用于文档目的: 我添加了错误的线程编织器库。我添加了 com.googlecode.thread-weaver 而不是 Maven 存储库中的 org.mapdb。
在我添加缺少的库并用正确的库替换错误的库后,它起作用了,但测试本身失败了。
此外,我在 github 上找到了一个 google repo,其中解释了 thread-weaver 的代码和所需的依赖项: https://github.com/google/thread-weaver