适用于Javaagent Instrumentation框架的JUnit测试框架 [英] JUnit Test framework for Javaagent Instrumentation Framework
问题描述
为Java代理和工具库的代码创建单元测试的标准方法是什么?我已经使用Byte Buddy框架创建了一个Java代理,用于在Web应用程序的顶部开发探查器,现在我想为此代理编写JUnit测试用例.
What are the standard ways for creating unit tests for code of a Java agent and instrumentation libraries. I have created a Java agent using the Byte Buddy framework for developing a profiler on top of a web applictaion and now i wanted to write JUnit test cases for this agent.
推荐答案
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.
创建可重复测试的棘手问题是,在应用代理之前不得加载类. Byte Buddy的测试工具通过创建ByteArrayClassLoader
来解决此问题,该ByteArrayClassLoader
能够处理所有受检测的类.由于该类加载器是动态创建的,因此可以保证.
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.
大多数捆绑了JDK的VM都可以运行时附加Java代理.可以肯定的是,Byte Buddy确实为JUnit定义了AgentAttachmentRule
,它在运行测试之前断言了此功能.您可能也会考虑这一点.
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.
这篇关于适用于Javaagent Instrumentation框架的JUnit测试框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!