JUnit Test framework for Javaagent Instrumentation Framework

痞子三分冷 提交于 2019-12-06 03:34:25

You can take inspiration from Byte Buddy's own unit tests for creating a Java agent. For this, declare a test dependency on the byte-buddy-agent module. That module includes a class that is capable of attaching a Java agent at runtime using ByteBuddyAgent.install() which returns an Instrumentation instance. Make sure that you remove a Java agent after running a unit test. Otherwise, your agent will remain active for any subsequent test.

On tricky part for creating repeatable tests is the fact that a class must not be loaded before the agent is applied. Byte Buddy's test harness solves this by creating a ByteArrayClassLoader that is capable of all the classes that are subject to instrumentation. As this class loader is created dynamically, this can be guaranteed.

Most JDK-bundled VMs are capable of a runtime attachment of a Java agent. To be sure, Byte Buddy does however define a AgentAttachmentRule for JUnit that asserts this capability before running a test. You might consider this as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!