Creating .jar from JUnit Class in IntelliJ - java

I need to run my Selenium scripts in Jmeter. I would like to use Junit request. To reach this I need to make a .jar file from my test file. But I dont know how.
This is my class with the test:
When I try to make a .jar using: File -> Project Structure -> Artifacts -> + -> Jar from modules with dependencies -> here I am trying to choose class:
but it is not possible because my Login class is "Unit test class".
Is it possible to make .jar file from Unit test class somehow? And will the depedendecies in my pom.xml be preserved?
I tried to make it under Main class, but there is a problem with a JUnit annotations.
I wasnt able to achieve this with these tips unfortunatelly :( https://maven.apache.org/plugins/maven-jar-plugin/examples/create-test-jar.html

You don't need to choose a main class because you're not creating an executable jar, you're creating a .jar with tests
You need to tick "include tests" box
I believe it would be easier to go for Maven Jar Plugin (from your own link) in combination with Maven Assembly Plugin to add all transitive dependencies.
And the easiest would be just switching to WebDriver Sampler which can be installed using JMeter Plugins Manager and put your code there, it will be much easier to debug and amend it, moreover you won't have to take care about running it with > 1 threads

Related

To run cucumber java project without maven

I am performing automation setup using java, cucumber and junit. However, due to restrictions of firewall and proxy in company, not able to create maven project with archtype templates. Hence initially created java project with source folder as src/test/java, placed feature file, stepdefinition and test runner class within this source folder, however, while running test runner class, I am facing "test class not found error for selected project".
I tried all sort of solutions such as ordering JRE file to bottom, giving entire path of feature file,adding output folder in using run configuration etc however it seems not working.
Hence finally would like to know whether can we run cucumber with only java and junit in the absence of maven.
Kindly help me in resolving this issue.

Intellij not able to find changes made in dependency modules

I have a Maven project which has dependency on other Maven projects. I have managed to import those dependency modules via 'project structure' into my project. Able to mvn clean install and able to run the program. Also in my main project, if I were to 'command click ' on a file that is located in the dependency modules, it will nicely go to that .java file (instead of heading to the .class file).
But changes made under the dependency project does not reflect when I run the program. Re building or a new mvn install doesn't make any difference.
For example, I have tried just adding a new variable declaration in a class located in the dependency modules. This variable does not even exist when I run the program and check via debug.
Similarly created an entire method in a class inside the dependency module.
That method doesn't exist either. When I mouse over a breakpoint I placed under this method, I get following message:
No executable code found at line 112 in class com.name....
Information which may help.
This project was working fine without having to add these dependencies cos they existed as jar files as part of this project. Since I needed to make changes in those dependencies, I have removed those jar files 'via Project Structure' and instead imported those dependencies as modules 'via Project Structure'.
As mentioned above, this is working. I am able to build and launch the app. Just not able to see changes when I make them.
Another issue which surfaced and again I don't get it. There was a parameter which was not working on my main project (compilation was failing) until I made the change in my dependency. Now it is working fine in terms of compilation. But when I run it, under debug I get the correct param value, but after the next step, it just jumps to InvocationTargetException. Added short snippet on the code change below.
// Under Main Project
someMethod(Doable do){
// do something
}
DoSomething doSomething = null;
someMethod(doSomething); // compilation error unless I implement Doable
// Under dependency
// implement Doable is the change which compiles now
class DoSomething implements Doable{
//implement something
}
Added the project structure for reference.
Please advice. Thank you.
MyMainProject
mvn_structure
pom.xml
MainDependency
SubDependency1
mvn_structure
pom.xml
SubDependency2
mvn_structure
pom.xml
SubDependency3
mvn_structure
pom.xml
pom.xml
Your project relies on the dependency jars in your maven repo, not on the dependency source codes (the other projects). So in order to have your changes in the other project available to your project, you need to build the other projects first so that their jars in the repo are updated. Only then are those changes available to your project.

JUnit tests in Eclipse on Java 9 modular project

I'd like to run tests with JUnit 5 on Java 9 modular project in Eclipse, with no Maven, Gradle or all that fancy stuff. So I have src/main/java path where module-info.java and module's packages live and also src/test/java where all the test classes are. Id est business as usual, prior to the Jigsaw module system. I have Eclipse Oxygen.3a (4.7.3a) an Java 10.0.1.
I've seen some video from Eclipse showing, how to add JUnit test to modular project, but this flabbergasted me deeply: they put required keyword into module-info.java of a module, binding it to JUnit module. Is that actually even correct?
I've seen also all these --patch-module/--add-reads solutions (when we're talking about working in a console) and it seems like it's the proper way to do it, but I have no idea, how to achieve that in Eclipse without binding module under test to JUnit module. Is that even possible in Eclipse (without Maven and s.o)?
I tried to solve this problem for quite a while, too. My approach is to add a filter to the source code directory for src/main/java that filters out the module-info.java. This allows to have a different module-info.java in src/test/java. It will be this one that gets copied to the output folder. This way you can run your unit tests from within the IDE and use the other one for the final build. However, you need to keep the content of the one in src/main/java updated yourself.
Right click on the project > Properties > Java Build Path > Source
Select the src/main/java entry, click Edit > Next > Exclusion Patterns > Add

Running JUnit tests together with Java project

I'm writing code in Intellij and have a JUnit test class included in a project and i understand that running of JUnit should always be done at build time.
Is there a way to first run the JUnit and only if there were no test error run the project itself ? I want them to run together with 1 click (NOT run them seperately/manually).
Also, i would like the above to work even when the project is packed as a .jar file.
How can it be done ?
Thanks !
In Intellij:
Run -> Edit Configurations
Create a JUnit configuration for your tests
Create a Run configuration for your project.
And on "Before Launch": Add -> Run Another Configuration and choose the one created at point 1.
It doesn't matter how your project is packed (jar, ...)
Normally this is done by using a build management tool like: maven, gradle, ant. In this way the build tool will run tests for you and stop if they fail.
With maven, it's just a command: mvn clean package exec:java which will compile code, build project, run tests and execute your code.
See example project: https://github.com/HaveACupOfJava/maven-jar-demo

How to modify the Eclipse Run Configuration classpath?

I'm developing a project in Eclipse JDT, that has optional dependencies on OSGI - that is, it supports being used as an OSGI bundle, and if it is used as a bundle inside an OSGI environment, it will also reference some classes from OSGI.
Now I want to write JUnit test cases for both running inside OSGI and running without OSGI, included in two different test suites. The OSGI tests are run as JUnit Pulg-in Tests and the Non-OSGI tests should be run as normal JUnit Tests.
Now I have the problem, that I couldn't find any way in Eclipse to exactly specify the classpath for the JUnit Test Run Configuration and exclude the optional OSGI jars.
Is there any way to exclude jars or modify the default classpath for an Eclipse Run Configuration?
If not, does anyone have a suggestion, how one should setup JUnit tests in such a case?
The only solution I was able to find is, to create a jar file from my unit tests and run the tests without OSGI from a different project with the test jar file on the classpath. But I would prefer a more elegant solution, ideally without the necessity of a second test project.
With the help of Gimbys comment, I was able to solve the problem. Although it is not possible in Eclipse to modify the default classpath generated by JDT, it is possible to entirely remove the default classpath and then add your own classpath in the classpath tab of the Runtime Configuration.
To remove the default classpath, one has to select the root entry that is named after the currently run project, and then click on the "remove" button. After that one can add all the jar files and projects that should be loaded in the classpath. The downside of this approach is, that jar files, that are normally provided by Eclipse plugins like e.g. junit.jar, must then also be selected manually (e.g. by adding it to a library folder of the project or by selecting the external jar in the plugins directory of the Eclipse installation folder).
I would suggest that you split up your project in 4 separate projects:
Logic: This package contains all the logic, so basically what your program/plug-in does
Logic.plugin: This Project resembles a Plugin-Project (OSGI-Bundle). This project contains only configurations needed for your plugin and everything dependent on osgi. It has a dependency on your Logic Project.
Logic.plugin.test: All the test cases for your osgi bundle. It is the normal plugin approach to split up logic and test cases
Logic.cli: This project contains the command line interface (or whatever your alternate application is). This also depends on the Logic project.
If your CLI project is only very small you may merge it with the Logic project.

Categories

Resources