agent.junit
Use Java Agents in JUnit tests (4.12 and 5.3.0-M1) without any external configuration
Easily add a Java Agent in a JUnit test without any external configuration.
Provides both JUnit 4 Rules and JUnit 5 Extensions.
Note: this module is built against com.sun.tools.attach, see io.earcam.wrapped:com.sun.tools.attach for more details.
Examples
Given some setup:
protected static final String DIR = "target"; protected static final String FILE = "AbstractAgentJarTest-stubagent.jar"; public static final String JAR_NAME = DIR + '/' + FILE; public static final Path JAR_PATH = Paths.get(DIR, FILE); public static final String AGENT_ARGUMENTS = "-Awinter=socks";
A JUnit 4 Rule can be used with the Agent class:
@Rule public AgentRule rule = AgentRule.agent(StubAgent.class, AGENT_ARGUMENTS);
Or, almost identically, with the Agent JAR:
private static final String AGENT_ARGS = "-Apop=socks"; @Rule public AgentRule rule = AgentRule.agent(JAR_PATH, AGENT_ARGS);
Similarly a JUnit 5 Extension may be used with the Agent class:
@AgentClass(value = StubAgent.class, arguments = AGENT_ARGUMENTS) public class AgentClassTest {
Or with the Agent JAR:
@AgentJar(value = JAR_NAME, arguments = AGENT_ARGUMENTS) @Test public void test() { // ...
View full sources of associated tests.