How to run tests while developing javaagents?

I am trying to mess with Foursquare HeapAudit and I am trying to configure it using IntelliJ IDEA. I managed to create it just fine using the dependencies from pom.xml.

However, when I actually try to run JUnit tests, basically all of them fail. I assume this is due to the fact that using HeapAudit requires the JVM to start with it as -javaagent , according to github:

 $ java -javaagent:heapaudit.jar MyTest 

Presumably the tests will pass if I put this line, and refer to heapaudit.jar i, loaded / built earlier. However, it seems to me that if I made changes to the source code, I would need to repackage this stupid .jar file to make sure that it works.

Is there any way to run tests using -javaagent without going through the entire compilation build cycle -> package-in-jar every test cycle? Perhaps IntelliJ -javaagent newly compiled .class files as -javaagent before running the tests?

+7
source share
1 answer

1) Take the can only with META-INF/MANIFEST.MF

the manifest must be properly configured using Premain-Class and other attributes. The bank does not need any other files. Use this jar with -javaagent . If the agent classes are in the class path, the agent starts normally.

This can lead to an error when using maven-surefire-plugin with forkMode=never , since by default application classes are loaded into the child class of ClassLoader.

Works great with Eclipse and Intellij.

If you do this, double-check the syntax of the manifest (I once spent a lot of time figuring out that the package name was wrong).

2) Use ea-agent-loader

It will allow you to load the agent (any agent) at runtime (it uses VM.attach() ). However, VM.attach() sometimes interrupts debugging, and breakpoints may not start.

It will have the same problems with surefire in forkMode = never

3) Download the agent at run time.

Write your code on the agent download at runtime . And name it from @BeforeClass . You still need a jar (which you can create at runtime if you want).

You just need to call this (only once):

 AgentLoader.loadAgentClass(YourAgentClass.class.getName()); 
+2
source

All Articles