Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use java.lang.instrument.Instrumentation in JUnit tests?

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

like image 606
paul Avatar asked Aug 04 '10 12:08

paul


People also ask

What type of software testing uses JUnit with Java?

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.

Can JUnit test contain main method?

You can call main method from junit test like this: YourClass. main(new String[] {"arg1", "arg2", "arg3"});

What is Java Lang instrument?

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.

Which Java framework is used to write unit tests in android?

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.


1 Answers

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);
like image 131
Integrating Stuff Avatar answered Oct 10 '22 09:10

Integrating Stuff