Is it possible to use java.lang.instrument.Instrumentation in JUnit tests? I am using mockrunner to simulate a Servlet and want to measure the size of objects stored in a session
JUnit is a unit testing open-source framework for the Java programming language. Java Developers use this framework to write and execute automated tests. In Java, there are test cases that have to be re-executed every time a new code is added.
You can call main method from junit test like this: YourClass. main(new String[] {"arg1", "arg2", "arg3"});
instrument Description. Provides services that allow Java programming language agents to instrument programs running on the JVM. The mechanism for instrumentation is modification of the byte-codes of methods.
JUnit is a “Unit Testing” framework for Java Applications which is already included by default in android studio. It is an automation framework for Unit as well as UI Testing. It contains annotations such as @Test, @Before, @After, etc.
Yes, this is possible, but not very straightforward.
The problem with using java.lang.instrument.Instrumentation is that you will ALWAYS need to use a JVM agent. A good introduction to JVM agents is available at http://www.javabeat.net/2012/06/introduction-to-java-agents/.
However, since your unit tests run in a jvm too, you can specify the agent as a JVM argument.
A memory reporting JVM agent for what you want to do, and ready for packaging, is available at https://github.com/jbellis/jamm. You can build it with either Maven or Ant. In order to use it, you build it and then pass the following as a jvm argument when you start the unit test or unit test suite:
-javaagent:<path to>/jamm.jar
Within the unit tests, you can then instantiate the MemoryMeter and use it:
MemoryMeter meter = new MemoryMeter();
meter.measure(object);
meter.measureDeep(object);
meter.countChildren(object);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With