Running Arquillian tests with Maven dependencies in IntelliJ Idea - java

When deploying an Arquillian test with Intellij using pom dependencies like:
File[] libs = Maven.resolver().offline()
.loadPomFromFile("pom.xml")
.importRuntimeDependencies()
.resolve().withTransitivity().asFile();
The test run just fine in other IDEs, but with IntelliJ, there is an error that does not let execute the test properly.

Go to Edit Configurations (under Run menu), select Defaults branch and
then JUnit. You can see that Working directory is set to project’s
directory. That’s why pom.xml could not be found in this location.
Fortunately it can be changed either to MAVEN_REPOSITORY or to
MODULE_DIR and the latter is exactly what you want. Changing that and
saving it in defaults makes the issue above disappear.
Now you can run your tests from IntelliJ even if you have maven
dependencies to be included in your #Deployment archive.
As seen here: http://michalostruszka.pl/blog/2012/10/24/arquillian-tests-with-maven-deps-intellij/

Related

How to execute newly developed maven plugin in the same project

I have a maven project with a couple of child modules. One is a new maven plugin. Another one is a test module where I want to run my newly developed plugin to ensure that it works fine.
In the test module I do a normal reference to my plugin: build > plugins > plugin > <definition of my plugin>. And then if I executed mvn clean test it works fine. The plugin is being compiled and later, in the next module it is executed.
But there are a couple of problems with this setup:
When I do mvn clean execution fails with plugin cannot be resolved as the second module does not have even compiled version of the plugin.
In IDE (Intellij IDEA) reimport of maven modules fails with the same problem.
I do not want to do mvn install to have it locally so the dependencies are resolved as I do changes quite frequently and do not want to reinstall it every time.
I do not want to write integration tests instead of running the plugin directly as I want a realistic test and moreover, the plugin setup is quite tricky and would be hard to replicate that in a test.
I thought that I could use systemPath to reference the plugin but I am not sure if that will work and how to set it up.

Use classpath of multiple modules in IntelliJ IDEA

I have a Maven project, imported from Eclipse, where the dependencies are set to scope provided. When the project is deployed, the jars are deployed as well so that works fine.
While developing, however, I use a "debugging project" that calls the Maven project, and when it runs I get a bunch of Class Not Found errors when the Maven dependencies are set to provided.
If I change the scope of the Maven dependencies to Compile then the project works fine.
If I change the scope of the dependencies to compile, would that change the output of the project? i.e. add a bunch of jars? That would be undesirable.
I also tried to change the Debug Configuration settings and specified the Maven project in "Use classpath of module", but then the files of the debugging project are not found.
How can I specify the classpath to be of both the Maven project and the debugging project, so that classes from both projects including the dependencies will be on the classpath?
Thanks!
There are 3 types of dependency scope: compile, test, and provided,
compile: the dependency library will be used in all steps: compile , test and run,
test: the dependency library will only be used in the test
provided: the dependency library will only be used in compile and test, but in the run time, the dependency library must be provided by the container otherwise it will throw class no find issues.
Your issues is that you did not provide the dependency library in the run environment ( container) when running your project.
hope this can help you
How did you import the project to Idea? If the project is opened as a Maven projects, it should work out of box.
Can you try to open the project by selecting pom.xml?

How to add classpaths for unit tests in maven ?

I am using maven to make a simple application and writing unit tests for it. I am new to both using maven as well as writing unit tests with Junit.
My directory structure is as follows :-
The source code classes used in my project are in the directory : -
project-name/src/main/java/com/somename/app/packagename
Main file (which contains static main method) is in :-
project-name/src/main/java/com/somename/app/App.java
The default unit test provided by Maven is in :-
project-name/src/test/java/com/somename/app/AppTest.java
Now, I have added a unit test in the above directory (in which AppTest.java) is there and this unit test needs to access the classes defined in the "packagename" directory as described above.
If it were a simple commandline , I would have added the classpath to the directory when compiling and running the application.
How should I do the same in Maven ? Also, is there any alternative to adding the classpath directly in maven ?
When using the Maven Standard Directory Layout (which I always recommend as Maven is all about conventions), the following directories are always part of the classpath when running the tests:
src/main/java
src/main/resources
src/test/java
src/test/resources
So there is no need to manually adjust anything for running the tests. The mvn test command simply does what you want.
Maven Directory structure is always added in the class path. if you are using eclipse just got to Run as->maven build.(make sure you have maven plugin installed) otherwise you can directly use the mvn clean , mvn install command for building the project in maven.

Resolving dependencies in Eclipse for Fitnesse

My goal is to run unit tests in fitnesse.responders.run.slimResponder for testing my DataFlex SlimRunner implementation. So I downloaded the Fitnesse source code, and made it a new Java project in Eclipse. I was able to compile it by selecting Run As Ant Build (2) on the build.xml file. But in order to resolve include errors in the Problems view in Eclipse, I ended up manually adding dozens of external JARs by hand. I found that Maven/Ivy had apparently downloaded the jars as part of the Ant build. But somehow these were not added to the Java Build Path.
It seems reasonable to me to assume that there should be an easier way to set up the Java Build Path than to add the JAR files manually, since build.xml apparently contains all this information already. What am I missing?
The Fitnesse Readme.md mentions using Apache Ivy for dependency management. Download IvyDE from Eclipse Marketplace, and set it up (use the ivy.xml that is part of the Fitnesse source code).

Class Not Found Exception when running JUnit test

I am getting this error when running JUnit test in Eclipse:
Class not found com.myproject.server.MyTest
java.lang.ClassNotFoundException: com.myproject.server.MyTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:693)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:429)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
I've tried adding JUnit library in the classpath although I'm using maven, and the JUnit library is in the POM dependency.
I already have tried cleaning the project and created a new JUnit test case using the JUnit plugin for Eclipse, still getting the same error.
This appears to occur because only the source code is compiling when you use mvn clean compile (I'm using maven 3.1.0 so I'm not sure if it always behaved like this).
If you run mvn test, the test code will compile as well, but then it runs the tests (which may not be immediately desirable if you're trying to run them through Eclipse.) The way around this is to add test-compile to your Maven command sequence whenever you do a mvn clean. For example, you would run mvn clean compile test-compile.
I had a similar problem with my tests and found somewhere in the Web, that you have to go to Build Path in your project's properties and move Maven Dependencies above JRE System Library. That seems to have worked for me.
I faced the same problem and I was able to fix it using #slomek's answer but the issue resurfaced after that.
I finally fixed it by adding my project's output folder to the JUnit test's run configuration. Steps are:
Find your project's output folder from Project properties -> Java Build Path -> Default output folder
Usually this is <project-folder>/bin
Go to Run -> Run Configurations...
Click on the unit test's run configuration and click on the Classpath tab
Explicitly add your project's output folder under User Entries - even if the project is already included there
Click on Advanced -> Add folder to add the output folder
This issue might be due to weird project setup in Eclipse - e.g. source folder with nested maven projects imported as a single project/folder (at least that was how my project was setup).
If this problem occurs in Eclipse only, executing command Project -> Clean... on selected project may help.
After having tried everything here with no improvement, I solved my issue by just restarting Eclipse
I fixed my issue by running maven update.
Right click project your project > Maven > Update Project
In my case I had a wrong maven directory structure.
Which should be like:
/src/test/java/ com.myproject.server.MyTest
After I fixed that - everything worked like a charm.
In my case, only next steps helped me to resolve this issue:
Project->properties->Run/Debug Settings.
In "Launch configurations for '.....'" select classes/projects
Edit -> Classpath -> "Restore Default Entries"
I had the similar problem with my Eclipse Helios which debugging Junits. My problem was little different as i was able to run Junits successfully but when i was getting ClassNotFoundException while debugging the same JUNITs.
I have tried all sort of different solutions available in Stackoverflow.com and forums elsewhere, but nothing seem to work. After banging my head with these issue for close to two days, finally i figured out the solution to it.
If none of the solutions seem to work, just delete the .metadata folder created in your workspace. This would create an additional overhead of importing the projects and all sorts of configuration you have done, but these will surely solve these issue.
Hope these helps.
Earlier, in this case, I always did mvn eclipse:eclipse and restarted my Eclipse and it worked. After migrating to GIT, it stopped working for me which is somewhat weird.
Basic problem here is Mr Eclipse does not find the compiled class.
Then, I set the output folder as Project/target/test-classes which is by default generated by mvn clean install without skipping the test and proceeded with following workaround:
Option 1: Set classpath for each test case
Eclipse ->Run ->Run Configurations ->under JUnit->select mytest -> under classpath tab->Select User Entries->Advanced->Add Folder -> Select ->Apply->Run
Option 2: Create classpath variable and include it in classpath for all the test cases
Eclipse ->Windows ->Classpath Variables ->New->[Name : Junit_test_cases_cp | path : ]->ok
Then go to
Eclipse->Run ->Run Configurations ->JUnit->select mytest ->under classpath tab ->Select User Entries->Advanced->Add classpath variables->Select Junit_test_cases_cp->ok->Apply->Run
This is the only thing currently working for me after trying all the suggestions online.
NoClassDefFoundError really means it can't initilize the class. It has nothing to do with finding the class. I got this error when calling trim() on a null String.
JUnit won't show NullPointerException. The string isn't null when running normally because
I'm fetching the string from a properties file which is not availible for tests.
My advice is to remove pieces from the class until your tests start passing. Then you can determine which line is giving the error.
In my case, changing the order of Maven Dependencies from Build Path configuration did not work for me. I changed its order from Run configuration.
Follow these steps:
• Go to Run -> Run Configurations...
• Click on the unit test's run configuration and click on the Classpath tab.
• check Use temporary JAR to specify classpath (to avoid classpath length limitations).
May be it is enough and your test works. You should try, so press Run button. If it did not work. Follow all previous steps again and without pressing Run button, go to the next step.
• In sub branches of User Entries move Maven Dependencies above your project or test class.
• Click Run button.
These steps worked for me.
Delete the content of local Maven repository.
run mvn clean install in the command line. (cd to the pom directory).
Build Project in Eclipse.
1- mvn eclipse:eclipse
2- project clean all projects
3- restart
It's worth mentioning as another answer that if you're using eGit, and your classpath gets updated because of say, a test coverage tool like Clover, that sometimes there's a cleanup hiccup that does not completely delete the contents of /path/to/git/repository/<project name>/bin/
Essentially, I used Eclipse's Error Log View, identified what was causing issues during this cleanup effort, navigated to the source directory, and manually deleted the <project name>/bin directory. Once that finished I went back to Eclipse and refreshed (F5) my project and the error went away.
It can also be due to "[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?"
I had the same problem with a Gradle project with a test SourceSet with two resource directories.
This snippet comes from a main-module.gradle and adds a resource dir to the test SourceSet:
sourceSets {
test {
resources {
srcDir('../other-module/src/test/resources')
}
}
}
Doing this I had two resource directories related to the test SourceSet of the project main-module:
../other-module/src/test/resources
src/test/resources (relative to the main-module folder, automatically added by the java plugin)
I find out that if I had two files with the same name in both the source directories, something in the process resources stage went wrong. As result, no compilation started and for this reason no .class were copied in the bin directory, where JUnit was looking for the classes. The ClassNotFoundException disappeared just renaming one of the two files.
It seems compile issue. Run project as Maven test, then Run as JUnit Test.
For project that does not use maven : This worked for me https://ihategeek.wordpress.com/2012/04/18/eclipse-junit-test-class-not-found/
Adding the jre and project src at the bottom in Order and exports in build path
Pls check if you have added junit4 as dependency.
e.g
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
For me I had to put the project x/src/test/java/ at the bottom of the "order and export" in the "java build path"
Check if your project is opened as a Maven project and not just a regular Java project. Actually a no-brainer, but that is exactly the same reason why you might miss it.
I had faced the same issue. I solved it by removing the external JUnit jar dependency which I added by download from the internet externally. But then I went to project->properties->build path->add library->junit->choosed the version(ex junit4)->apply.
It automatically added the dependency. it solved my issue.
Making some dummy change and saving the test class can solve the problem. It will build the .class automatically
Might be you forgotten to place the Main class and Test Case class in
/src/test/java. Check it Once.

Categories

Resources