我正在尝试动态分析带有ByteBuddy(1.7.5)的JUnit测试,因此我事先并不知道它们。我通过下面的run()方法加载它们,并拦截使用下面的Agent()方法构建的代理。
单元测试在Eclipse中直接运行或在不安装代理程序的情况下运行它时按预期工作。但是一旦安装了代理,测试就会抛出异常并失败。
例如:当测试创建文档时抛出java.io.FileNotFoundException
(请参阅下面的堆栈跟踪)。我正在使用开源单元测试(在本例中来自terrier-core,测试类org.terrier.indexing.TestSimpleXMLCollection
中的测试)。
有人知道为什么会这样吗?它是否像原始测试和拦截器一样发生了竞争条件?
我的代码如下:
public void run(@SuppressWarnings("rawtypes") Class classWithName, String testMethodName) {
interceptor.traversed = false;
Request request = Request.method(classWithName, testMethodName);
junit.run(request);
}
和
public Agent(String pn) {
this.projectName = pn;
this.junit = new JUnitCore();
this.interceptor = new InterceptorX2();
new AgentBuilder.Default().type(
ElementMatchers.not(ElementMatchers.named("junit.framework.testcase"))
.and(ElementMatchers.nameStartsWithIgnoreCase(projectName)
.or(ElementMatchers.nameEndsWithIgnoreCase("test"))
.or(ElementMatchers.nameEndsWithIgnoreCase("tests")))
,
ElementMatchers.not(ElementMatchers.isBootstrapClassLoader()))
.transform(new AgentBuilder.Transformer() {
@Override
public Builder<?> transform(Builder<?> builder, TypeDescription arg1, ClassLoader arg2, JavaModule arg3) {
return builder
.method(
ElementMatchers.isPublic()
.and(ElementMatchers.not(ElementMatchers.isDeclaredBy(Object.class))
.and(ElementMatchers.not(ElementMatchers.isDeclaredBy(junit.framework.TestCase.class))))
).intercept(MethodDelegation.to(interceptor));
}
}).installOn(ByteBuddyAgent.install());
}
修改
我的拦截器没有做任何特别的事情:
@RuntimeType
public Object intercept(@Origin Method m, @SuperCall Callable<?> zuper, @AllArguments Object[] args, @This Object thiz) throws Exception {
return zuper.call();
}
Stacktrace错误:
java.io.FileNotFoundException: C:\Users\jackt\AppData\Local\Temp\junit6014103019704928063\terrier.properties (Das System kann die angegebene Datei nicht finden)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at org.terrier.tests.ApplicationSetupBasedTest.makeEnvironment$original$m3joH5OH(ApplicationSetupBasedTest.java:81)
at org.terrier.tests.ApplicationSetupBasedTest.makeEnvironment$original$m3joH5OH$accessor$c8PeV6zd(ApplicationSetupBasedTest.java)
at org.terrier.tests.ApplicationSetupBasedTest$auxiliary$fr9IrFTX.call(Unknown Source)
at myproject.bytebuddy.introspect.InterceptorX2.intercept(InterceptorX2.java:79)
at org.terrier.tests.ApplicationSetupBasedTest.makeEnvironment(ApplicationSetupBasedTest.java)
at org.terrier.indexing.TestSimpleXMLCollection.makeEnvironment$accessor$BjbiedTZ(TestSimpleXMLCollection.java)
at org.terrier.indexing.TestSimpleXMLCollection$auxiliary$BcvkPOor.call(Unknown Source)
at myproject.bytebuddy.introspect.InterceptorX2.intercept(InterceptorX2.java:79)
at org.terrier.indexing.TestSimpleXMLCollection.makeEnvironment(TestSimpleXMLCollection.java)
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:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at myproject.bytebuddy.introspect.Agent.run(Agent.java:71)
at myproject.analysis.MutatorAnalysis.runAgent(MutatorAnalysis.java:43)
at myproject.Analysis.getResultsForTestCase(Analysis.java:196)
at myproject.Analysis.startPrivateAnalysis(Analysis.java:163)
at myproject.Analysis.start(Analysis.java:147)