How to work with JMeter and Junit - jmeter

I am very new to JMeter
I am trying to use Junit Request sampler in JMeter.In my project we have a class called PayloadProcessorTest.java. from these class methods i am calling some other class methods.It has lot of dependencies
How can i create jar file for PayloadProcessorTest.java with dependencies
I saw many examples for JMeter with Junit Request sampler. But, those all examples are independent classes
Can any one please help me

There are several ways of creating a .jar file:
Using Maven
Using Ant
Using Eclipse
.jar files are basically ZIP archives so you can just compile your PayloadProcessorTest.java and put resulting PayloadProcessorTest.class into /lib/junit/test.jar file keeping package structure. After restart JMeter will pick up the class. Don't forget to add any 3rd-party jars used in PayloadProcessorTest (if any) to JMeter classpath. For more information check out How to Use JUnit With JMeter guide.

If you want to get the dependencies with Maven you can run
mvn install dependency:copy-dependencies, which will create a folder inside your target folder called 'dependency' filled with the dependencies. To speed this up you can add the command as External Tool in Eclipse using Run > External Tools > External Tools Configurations.
Or if you want to use Eclipse you can choose File > Export > Java > Runnable JAR file and select the option 'Copy required libraries into a sub folder next to the generated JAR'. However to do this you will need to add a main class, and run it once as a Java Application before trying to export. The Main class can be empty or not.
package test;
public class Main {
public static void main(String[] args) {
}
}

Really, it depends what packaging capabilities you have.
You need to compile your classes in one or more jars, and then put them in the %JMETER_HOME%/lib/ext folder.
Or use maven to do it all for you.

Related

Is there a simple way to access project resource files from a gradle task?

I am working on a Gradle task for a java project. The task needs to read files from a subfolder of the project’s resources directory. I was expecting to find a standard way for Gradle to access project resources but have been unable to find one. Does Gradle provide simple way to find and import resource files? Maybe through a plugin?
If you mean the src/main/resources folder, then no. That is by convention used by the Java plugin to hold resources for the actual module and not the build classpath. But if you just want to read a file in it from a task, just use normal Java, Groovy or Kotlin APIs. You don't need a plugin for that.
Here is one for Groovy:
task printMyResource {
doLast {
logger.quiet(file("src/main/resources/subfolder/my_file.txt").text)
}
}
(The file method resolves a path to a File object relative to the project folder.)

How to pass spring.config.location="somepath" while building SpringBoot application with command-line Gradle (6.4) build

I have a SpringBoot application where I have application.properties file outside of project (it's not in usual place src/main/resources).
While building application with gradle clean build, it fails as code is not able to find properties files.
I have tried many command to pass vm args, gradle opts but its not working.
gradle clean build -Djvmargs="-Dspring.config.location=/users/home/dev/application.properties" //not working
It fails on test phase when it creates Spring application context and not able to substitute property placeholders. If I skip test as gradle clean build -x test it works.
Though I can run the app with java -jar api.jar --spring.config.location=file:/users/home/dev/application.properties
Please help how I can pass spring.config.location=/users/home/dev/application.properties in gradle build using command line so that build runs with all Junit tests
If I were you, I would not get involved the actual properties to junit test. So I would create a test properties for the project under src/test/resources/application-test.properties and in junit test I would load the test properties.
Example:
#RunWith(SpringRunner.class)
#SpringBootTest(classes = MyProperties.class)
#TestPropertySource("classpath:application-test.properties")
public class MyTestExample{
#Test
public void myTest() throws Exception {
...
}
}
System properties for running Gradle are not automatically passed on to the testing framework. I presume this is to isolate the tests as much as possible so differences in the environment will not lead to differences in the outcome, unless explicitly configured that way.
If you look at the Gradle API for the Test task, you can see that you can configure system properties through through the systemProperty method on the task (Groovy DSL):
test {
systemProperty "spring.config.location", "/path/to/my/configuration/repository/application.properties"
}
If you also want to read a system property from the Gradle command line and then pass that the test, you have to read it from Gradle first, e.g. as a project property, and then pass that value to the test:
test {
if (project.hasProperty('testconfig')) {
systemProperty 'spring.config.location', project.getProperty('testconfig')
}
}
Run it with gradle -Ptestconfig="/path/to/my/configuration/repository/application.properties" build
However, I would discourage using system properties on the build command line if you can avoid it. At the very least, it will annoy you greatly in the long run. If the configuration file can be in different locations on different machines (depending on where you have checkout out the repository and if it is not in the same relative path to your Spring Boot repository), you may want to specify it in a personal gradle.properties file instead.
I think there is a misunderstanding.
spring.config.location is used at runtime
As you validated:
java -jar api.jar --spring.config.location=file:/users/home/dev/application.properties
spring.config.location is used or required at runtime, not at build time.
When your spring boot app is building, an application.properties is required. An approach could be use an src/main/resources/application.properties with template values, but at runtime you will ignore it spring.config.location=file...
For unit tests
In this case as #nikos-bob said, you must use another properties, commonly inside of your src/test/resources
Environment variables instead external properties
We don't want to have hardcoded values in our main git repository src/main/resources/application.properties so the first idea is use an external properties. But this file must be stored in another git repository (equal to main repository ) or manually created.
Spring and other frameworks give us an alternative: Use environment variables.
So instead of manually external creation of application.properties or store it in our git repository, your spring boot app always must have an application.properties but with environment variables:
spring.datasource.url=jdbc:oracle:thin:#${DATABASE_HOST}:${DATABASE_PORT}:${DATABASE_SID}
spring.datasource.username=${DATABASE_USER}
spring.datasource.password=${DATABASE_PASSWORD}
spring.mail.host = ${MAIL_HOST}
spring.mail.username =${MAIL_USERNAME}
spring.mail.password =${MAIL_PASSWORD}
Advantages:
No manually creation of application.properties allowing us a more easy devops automations
No spring.config.location=file.. is required

JUnit class is not displayed in JMeter

I have created a Java project with 1 class containing 2 simple tests, exported as jar and added the jar in my JMeter JUnit folder, now after restarting the JMeter i don't see my class in JMeter even after selecting Annotation 4 option.
This is my class:
package print;
import org.junit.Test;
public class PrintClass {
#Test public void test() {
System.out.println("Hello World..!");
}
}
Consider the following checklist:
Your JUnit test class should have either empty constructor or constructor taking a single string as an argument. Other options are not supported
You should place your .jar file(s) under "lib/junit" folder of your JMeter installation
If there are any dependencies you need to put them somewhere in JMeter classpath as well
JMeter restart will be required to pick the .jars up
In case of any problems first of all check jmeter.log file (normally it lives under "bin" folder of your JMeter installation and contains enough troubleshooting information)
See How to Use JUnit With JMeter article for more details.
Even I faced same kind of issue then I added the dependency jars to the lib file.
Make sure you copy these files at this location -> apache-jmeter-5.1.1\lib
1.Selenium webdriver jar file (selenium-server-standalone)
2. Junit4 Jar file (junit4)
Make sure you add Jar file from eclipse or any IDE to this location -> apache-jmeter-5.1.1\lib\junit
1. Jar file which contains the automation selenium code (Using Junit)
Restart Jmeter and continue which the normal process of adding thread group and adding Junit sampler etc...
This resolved my issue and I was able to run my scripts on Jmeter.
In eclipse make sure that you create a JUnit class, not just the class and add junit annotations to this, even i was facing the same issue, it got resolved when i was created a JUnit class and then uploaded my project in JMeter
Did you put the jar in :
/lib/junit
Check you jar by running below command to see if it is ok:
jar -tvf <your jar>
And its dependencies as described in:
JUnit test classes not showing up in JMeter
See this for more details:
http://jmeter.apache.org/usermanual/junitsampler_tutorial.html

Setting dependency of a component to mantle-usl - custom Groovy class

I have a custom groovy class inside mantle-usl component. I would like to use the class in other component. Hence, I need to add a dependency so that the new component (or project) has the jar of mantle-usl ready for use.
Is there anyone who can help with this? I attempted to modify the build.gradle file of the project. And add a project dependency, but it returned an error.
project(':runtime/component/warehouse-items-masterenumerator') {
dependencies {
compile project(':runtime/component/mantle-usl')
}
}
As you would expect, this does not work. It seems that I do not have the project references set correctly.
The mantle-usl component doesn't have any compiled code in it so the build.gradle file does not build a jar file, it is only used for running the Spock tests.
I wouldn't recommend adding your own code to mantle-usl, it is easier and cleaner to put it in a separate component. For an example of a build.gradle file that does build a jar file look at the moqui/example component or most of the moqui tool components (such as moqui-elasticsearch).
You also don't need to modify the main build.gradle file from the moqui-framework repository, dependencies should be declared in the build.gradle file in each component (which are picked up automatically in the main build).

Selenium Netbeans Project to runnable jar

I'm developing tests using junit, maven and Selenium inside Netbeans IDE. I don't have any main class inside the src folder. I can run the tests easily from the IDE, but I'm trying to pack all of them into one jar file (so I can later use with linux cron and schedule daily tests). I've searched around the web but so far my search hasn't been successful. Can anyone point me in some enlightment path please?
Add a class in your project that contains the main function and accepts test class names as parameter.
class RunTest
{
public static void main(String args[])
{
// Run the tests contained in the classes named in the args.
org.junit.runner.JUnitCore.main(args);
}
}
Now create the jar using maven including all the dependancies in pom.xml. you will be able to run tests through jar by passing test class names
for more information read this

Resources