JUnit test classes not showing up in JMeter

2019-02-22 08:07发布

问题:

I'm trying to run JUnit test with JMeter 2.7. However, when selecting the test classes in the drop down of the JUnit sampler, they don't show up. As I found out, that's because the test classes are extending from another class (AbstractJUnit4SpringContextTests is the base class, with various abstract classes inbetween providing convenience methods) for all tests. A test class that is not extended from those base classes can be selected.

The JAR file containing the test classes is created by Maven (test-jar), the JAR containing all dependencies is created by the maven fatjar plugin. Both jars are placed in the JMeter/lib/junit directory.

I know that the JMeter manual says that all test classes must extend from the JUnit test class, but that only seems to be true for JUnit3. With JUnit4, JMeter does not need that requirement. Of course, I could rewrite all test so that they don't have to extend from the base class, but that would result a huge maintenance issue. So, how do I execute JUnit tests with JMeter that extend from a base class?

UDPATE 2012-08-09

Thanks to the hint of PMD, I now copied the dependencies one by one to the lib folder of JMeter, and now the GUI shows all my unit tests. Before that was possible, I had to solve a couple of problems by myself:

  • Copying the logkit-1.0.1.jar into the folder prevented the JMeter GUI from starting. No idea why, no error or log message was given. The JVM just started and terminated.
  • The were some version conflicts caused by maven dependencies which introduced older versions of the spring test packages. That resulted in some test classes extending from an older base class with the same name. Excluding those dependencies in the pom file helped.

I can now execute my JUnit test cases. However, several references in my classes are annotated with @Resource. The Testrunner of JMeter doesn't seem to inject those references, because every time a reference is accessed, a NullPointerException is thrown, as can be seen in the JMeter log. So, how I do I get JMeter to inject those dependencies, is that even possible?

回答1:

You must put your junit classes in lib/junit folder as you did and dependencies in lib folder.

You shouldn't use fatjar as sometimes these tools removed files from meta-inf or only keep one from all jars, spring pûts one in each of its jars .

Add all your jars one by one in lib folder.

Check jmeter logs to see if you have any exception .

If it still fails, ask a question on jmeter user list and if you don't get any answer create a simple test case and open a bug.



回答2:

Did you check the option that JUnit sampler has to search for v4 tests?

I've tried and this works for a simple project I've created with JUnit 4, it filters only tests with the @Test annotations even those classes doesn't extend TestCase class.



回答3:

As Teinacher wrote, JUnit test will show up in JMeter after copying all project dependencies (all .jar files) into JMeter's /lib directory (JMeter restart is needed).