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, see for more details.

Dependency Graph

Module Dependency


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:

public AgentRule rule = AgentRule.agent(StubAgent.class, AGENT_ARGUMENTS);

Or, almost identically, with the Agent JAR:

private static final String AGENT_ARGS = "-Apop=socks";

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)
public void test()
	// ...

View full sources of associated tests.

Back to top

Version: 0.1.0. Last Published: 2018-10-08.

Earcam Maven Skin.