6

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?

Jim Holden
  • 1,196
  • 1
  • 16
  • 37
  • @resource are usually interpreted by spring during init of context. In this case i should investigate your code deeper, maybe you should look at how spring test case initialize themselves or you can try to override setup method to inject manually resources. By the way if my response was correct you should mark it as so and maybe open another question instead of changing the initial one, that's how SO works. – UBIK LOAD PACK Aug 10 '12 at 12:35
  • @Teinacher have you found a solution for this? I have the same and don't know how to solve it – javagirl Jun 26 '13 at 12:27

4 Answers4

4

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.

UBIK LOAD PACK
  • 33,980
  • 5
  • 71
  • 116
2

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

JMeter option to search for JUnit 4 annotations

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.

Nacho Cougil
  • 552
  • 6
  • 15
0

With Jmeter 4.0, instead of you put dependencies in JMeter's lib folder you could specify paths to dependencies locations via the “user.classpath” property. This property lives in the “user.properties” file under the /bin folder of your JMeter installation.

A path item can either be a jar file or a directory. Any jar file in such a directory will be automatically included, jar files in sub directories are ignored.

When adding paths pay attention and use your platform path separator (java.io.File.separatorChar in Java) to separate multiple paths:

#Example for windows (; separator)
#user.classpath=../classes;../lib;../app1/jar1.jar;../app2/jar2.jar

#Example for linux (:separator)
#user.classpath=../classes:../lib:../app1/jar1.jar:../app2/jar2.jar

user.classpath=C:/git/adf-bpm-autotesting-tool/libs;C:/git/adf-bpm-autotesting-tool/libs/selenium-tools;C:/git/adf-bpm-autotesting-tool/libs/selenium-2.52.0;C:/git/adf-bpm-autotesting-tool/libs/selenium-2.52.0/libs

As a correct result while jmeter gui starts you will see in jmeter.log records like that:

2019-04-08 18:51:46,871 INFO o.a.j.JMeter: Adding to classpath and loader: C:/git/adf-bpm-autotesting-tool/libs
2019-04-08 18:51:46,872 INFO o.a.j.JMeter: Adding to classpath and loader: C:/git/adf-bpm-autotesting-tool/libs/selenium-tools
2019-04-08 18:51:46,873 INFO o.a.j.JMeter: Adding to classpath and loader: C:/git/adf-bpm-autotesting-tool/libs/selenium-2.52.0
2019-04-08 18:51:46,873 INFO o.a.j.JMeter: Adding to classpath and loader: C:/git/adf-bpm-autotesting-tool/libs/selenium-2.52.0/libs

After that in JUnit Request Sample you will found all you junit tests.

Egor B Eremeev
  • 982
  • 11
  • 20
-1

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).

Hufnal
  • 9
  • 1