我使用ByteBuddy创建了一个java代理用于方法日志记录。
return new AgentBuilder.Default()
.type(ElementMatchers.nameStartsWith(targetPackageName))
.transform((builder, typeDescription, classLoader, javaModule) -> {
return builder
.visit(Advice.to(MyAdvice.class).on(ElementMatchers.isMethod()));
});
MyAdvice。类
具有用@Advice修饰的方法。OnMethodEnter
。但是,在这些方法中,我引用了一个公共静态类及其所有属性和方法公共静态。在运行应用程序时,我能够使用这种方法记录所有方法,没有问题。
但是,当我运行一个用@RunWith(SpringJUnit4ClassRunner.class)
注释的测试类时,出现了一个问题。出于某种原因,插入指令的代码似乎消失了。还有其他人经历过类似的问题吗?
作为参考,当在IntelliJ中运行测试类时,我做了一个-详细:class
,并且在测试类之前加载了以下类:
[Loaded org.junit.runners.ParentRunner$1 from file:/Users/cberruz/.m2/repository/junit/junit/4.12/junit-4.12.jar]
[Loaded org.junit.runners.model.TestClass from file:/Users/cberruz/.m2/repository/junit/junit/4.12/junit-4.12.jar]
[Loaded org.junit.runners.model.FrameworkField from file:/Users/cberruz/.m2/repository/junit/junit/4.12/junit-4.12.jar]
[Loaded org.junit.runners.model.TestClass$FieldComparator from file:/Users/cberruz/.m2/repository/junit/junit/4.12/junit-4.12.jar]
[Loaded org.junit.runners.model.TestClass$MethodComparator from file:/Users/cberruz/.m2/repository/junit/junit/4.12/junit-4.12.jar]
[Loaded java.util.concurrent.ExecutionException from /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre/lib/rt.jar]
更新#1:添加后。使用(Listener.StreamWriting.toSystemOut())
发送给我的代理。代理能够发现一个类,但在我出现以下错误之后:
java.lang.IllegalArgumentException
at org.objectweb.asm.ClassVisitor.<init>(Unknown Source)
at org.objectweb.asm.ClassVisitor.<init>(Unknown Source)
at net.bytebuddy.pool.TypePool$Default$TypeExtractor.<init>(TypePool.java:7672)
at net.bytebuddy.pool.TypePool$Default.parse(TypePool.java:680)
at net.bytebuddy.pool.TypePool$Default.doDescribe(TypePool.java:665)
at net.bytebuddy.pool.TypePool$Default$WithLazyResolution.access$001(TypePool.java:745)
at net.bytebuddy.pool.TypePool$Default$WithLazyResolution.doResolve(TypePool.java:843)
at net.bytebuddy.pool.TypePool$Default$WithLazyResolution$LazyTypeDescription.delegate(TypePool.java:912)
at net.bytebuddy.description.type.TypeDescription$AbstractBase$OfSimpleType$WithDelegation.getModifiers(TypeDescription.java:8331)
at net.bytebuddy.matcher.ModifierMatcher.matches(ModifierMatcher.java:48)
at net.bytebuddy.matcher.ModifierMatcher.matches(ModifierMatcher.java:27)
at net.bytebuddy.matcher.ElementMatcher$Junction$Disjunction.matches(ElementMatcher.java:160)
at net.bytebuddy.agent.builder.AgentBuilder$RawMatcher$ForElementMatchers.matches(AgentBuilder.java:1299)
at net.bytebuddy.agent.builder.AgentBuilder$RawMatcher$Disjunction.matches(AgentBuilder.java:1191)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.doTransform(AgentBuilder.java:10302)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.transform(AgentBuilder.java:10263)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.access$1600(AgentBuilder.java:10029)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer$LegacyVmDispatcher.run(AgentBuilder.java:10648)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer$LegacyVmDispatcher.run(AgentBuilder.java:10595)
at java.security.AccessController.doPrivileged(Native Method)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.transform(AgentBuilder.java:10186)
at sun.instrument.TransformerManager.transform(TransformerManager.java:188)
at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:428)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
at sun.reflect.GeneratedMethodAccessor14.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:459)
at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:336)
at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:492)
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:93)
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91)
at org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61)
at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34)
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116)
at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291)
at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:480)
at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:337)
at org.springframework.aop.framework.ObjenesisCglibAopProxy.createProxyClassAndInstance(ObjenesisCglibAopProxy.java:54)
at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:201)
at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:109)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:463)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:346)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:295)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:421)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1634)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:756)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:281)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:249)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Spring通常会在连接代理之前扫描可能触发加载类的内容。因为您没有指定。使用代理生成器中的(RetransformationStrategy.RETRANSFORM)
,Byte Buddy不会尝试重新定义此类类,转换可能会丢失。
您可以使用。使用(Listener.StreamWriting.toSystemOut())
如果您想调试它,请查看Byte Buddy拾取了哪些类。
从很久以前的某个版本开始(大约是1.b.35版),Nutz 就偷偷的加入了 maven 的 pom.xml 文件, 极好的解决了测试时需要依赖其他 jar 包的问题,接下来就让我们使用 Maven 来进行 JUnit 测试吧。 Nutz 在 Github 的 地址为 https://github.com/nutzam/nutz(Git@OSC镜像 https://git.oschina.net/n
现在您已经了解了TestNG及其各种测试,您现在必须担心如何重构现有的JUnit代码。 没有必要担心,因为TestNG提供了一种按照自己的节奏从JUnit转换到TestNG的方法。 您可以使用TestNG执行现有的JUnit测试用例。 TestNG可以自动识别并运行JUnit测试,因此您可以将TestNG用作所有现有测试的运行器,并使用TestNG编写新测试。 您所要做的就是将JUnit库放在Te
使用: cucumber芯-1.2.4 cucumber-java-1.2.4 cucumber-junit-1.2.4 Junit-4.12 Eclipse Mars.1 Java 8
我试图用内存中的H2 DB测试某些实体的持久性,但我认识到无论是在构建平台上运行还是在运行RunAs时,都不会按应有的方式被调用- 我可以肯定的是,序列是在H2 DB内部生成的。当我连接到这个生成的H2时,我甚至可以选择它们。所以这绝对不是H2内部的问题,而是Hibernate的问题。(通常Hibernate会在持久化需要ID的实体时自动分配ID)。 实体 引用实体中的引用。。。 持久性单位...
我已经从https://howtodoinjava.com/spring-batch/java-config-multiple-steps/'复制了Spring批处理程序。 我从https://docs.spring.io/spring-batch/docs/current/reference/html/testing.html创建了以下JUnit测试用例: 我的Maven pom文件是: 谢谢
我有一个带有Java配置类的Spring(4.2.5)项目。这些在正常运行项目时起作用,但我无法使我的测试工作。在下面的测试中,为空。 如果添加注释,则会出现异常 其中和在和中定义,后者是基于活动配置文件选择的。 [编辑2] 经过更多的搜索,我发现了这个问题,并且在检查了我的依赖项之后,我发现我在maven依赖项下有Spring-Core-4.1.9,而且我没有在我的POM中包含一个依赖项。添加依