I am using IntelliJ idea for writing simple unit test cases using JUnit and Mockito. I am using Maven for dependency management.
IntelliJ idea keeps complaining that following imports cannot be resolved:
import org.junit.Test; //Cannot resolve symbol 'Test'
import static org.mockito.Mockito.*; //Cannot resolve symbol 'mockito'
Following is the dependencies section of my project:
<dependencies>
<!-- Dependency for JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!--<scope>test</scope>-->
</dependency>
<!-- Dependency for Mockito -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<!--<scope>test</scope>-->
</dependency>
</dependencies>
Following is my project structure:
Try View -> Tool Windows -> Maven projects, then click the blue icon in the top left of the tool window (Reimport all Maven projects). You should be able to find the dependencies in the project view, under external libraries.
If this does not work, there is probably something wrong with your maven config (pom.xml). Try mvn clean install from the command line see if it gives any errors.
My IDE was not resolving JUnit & Mockito dependencies. Tried reimport in IntelliJ and mvn clean install multiple times which didn't help. Reimport worked for me, but with the following steps.
Please be aware that you will lose any run configurations that you have created.
Close IntelliJ
Go to Project Folder and remove .idea folder (rm -rf ./idea)
Import Maven project again (You will need to add back any run configurations that were deleted)
I solved this problem by adding mockito to module I used. Just go to File -> Project structure and in Modules add mockito to your selected module - the one in which you use mockito (usually Test).
You can also use command
mvn -U idea:idea
This command forces a check for missing dependencies and helps in resolving them.
Restartig intellIJ fixed that for me
For me, I deleted org.mockito package from m2 repository and then did mvn clean install.
this reimport the package.
Try using lower versions of junit and mockito.
The latest versions weren't working in my project but it worked once I used lower versions.
Few other things you can do:
After adding the dependency just make sure to update the maven
project. (right-click on project folder -> Maven -> Reload project)
You can also try clicking on the tiny m symbol on the top right to
load maven changes or select maven button to get clean+install
options and click play button.
Related
Need a help !
I'm trying to connect spring security library to my project using Maven.
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
Library org.springframework.security.core have been downloaded successfully.
But when i'm trying to extend my class(AuthorizedUser.class) from org.springframework.security.core.userdetails.User
Intellij says, that can not resolve it. But other classes and interfaces from: org.springframework.security.core.userdetails resolving well. See the link...
When i goes to external library tree and opened org.springframework.security.core.userdetails library i saw User.class like this See the link...
Please, if anyone knows something about this problem, I'll be very thankful for any information
Windows 7
Intellij IDEA version 2017.2.4 Ultimate
Java version 1.8.0_144
Maven version 3.5.0
Maybe, you just need to create a constructor matching with super class or first try to create a class property instead of extending it just for test. apidoc
if it does not help, probably it is not related to ide, it is possible maven could not resolve dependencies because of some misconfiguration.
You can try these:
1- check local maven repository and confirm right version of jar files are there.
c:\users{username}.m2\repository\org\springframework\security
2- try build from commandline with following command and confirm build finish succesfully
mvn clean install
3- Try reimport all maven project
4 - Also, confirm intellij use right maven version, repository and settings xml. You can write "maven settings" on search bar, ant it will lead you to maven settings page.
if you find any of them is not as expected, let me know so I will try to help.
I need new third party jar for reading csv in maven based project. So, I did entry in pom.xml for same as below.
<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>2.0</version>
</dependency>
But when I run mvn install or mvn package command, It does not download newly added dependency and just build project and generate war.
I am trying to get the issue! Please share solution if anybody face this earlier!
Regards
Try running a forced update:
mvn clean install -U
The -U (uppercase U) forces maven to look at all dependencies and try to update them.
If the dependency is defined in a <dependencies> block that is within a <dependencyManagement> block, adding it without the version number to a <dependencies> block that is outside <dependencyManagement> may fix the problem.
This is because the purpose of <dependencyManagement> block is to manage dependency versions, and not to install the dependencies. See this other article: Differences between dependencyManagement and dependencies in Maven
I resolved this issue by following steps:
1). Remove concerned jar from local /m2 folder.
2). Run mvn eclipse:eclipse command
3). And last run: mvn clean install
Now I am looking for concerned jar in my project class path!
If you are using IntelliJ Idea as your editor then simply follow 3 simple steps:
Right click on your project
Select Maven (last option probably)
Select "Reload project"
And that's it, IntelliJ Idea will download the dependencies and now you can proceed further.
Try:
Menu -> Project -> Clean -> Select the project
Right Click on the project -> Maven -> Maven clean
Right Click on the project -> Maven -> Maven install
Happened to me and it has fixed my problem. Hope it helps you.
I deleted .m2 folder and then from eclipse ran maven install then took maven update project. It resolved my issue and jar file got downloaded.
You can usually resolve these errors by updating Maven dependencies as follows:
Right-click on your top-level project (not on the pom.xml file) in the Project Explorer view.
From the menu, choose Maven > Update project
Make sure ForceUpdate of Snapshots/Releases is checked, and click OK.
You'll see a progress indicator in the lower-right-hand corner of the application window. When the update completes, you should be able to generate code normally, and the error markers should disappear.
In IntelliJ
Right-click on your root folder of the project in the Project Explorer view. From the menu, choose Maven > Reload project.
After that, your new dependencies will be downloaded. Then you should be able to generate code normally, and all the error markers will disappear.
Problem: Maven can't find some dependencies inside my tests
import org.hamcrest.core.StringStartsWith; // HIGHLIGHTED AS RED IN INTELLIJ
It's imported like this in my pom.xml:
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
I already tried:
mvn test-compile
Invalidate Intellij caches
Restart Intellij
Clear .m2 and rebuild project
The way to figure this out is to run your tests from the command line, via mvn verify, and ensure that things compile and run correctly. This will allow you to determine whether the problem is in your POM configuration, or something unique to Intellij.
If it's a problem in Intellij, it is very likely to be something around folder configuration: generally only folders marked as "Test Source Root" will have access to things in the test scope.
Ensure that the dependency is at least specified under <project><dependencies> on not only under <project><dependencyManagement><dependencies>.
I've made a small library, lets call it lib. It dependends on another library, sublib which is available in Maven central:
lib/pom.xml:
<dependencies>
<dependency>
<groupId>3rdparty</groupId>
<artifactId>sublib</artifactId>
<version>x</version>
</dependency>
</dependencies>
Now I'm trying to use lib in my project proj. I've set it as a dependency:
proj/pom.xml:
<dependencies>
<dependency>
<groupId>mynamespace</groupId>
<artifactId>lib</artifactId>
<version>y</version>
</dependency>
</dependencies>
When I run mvn exec:java -D exec.mainClass=mynamespace.proj.Main the program runs fine.
However if I run it from IntelliJ, I get the following error:
java.lang.NoClassDefFoundError: 3rdparty/SomeSubLibClass
at mynamespace.SomeLibClass.method(SomeLibClass.java:100)
This seems to indicate that IntelliJ does not load the transitive sublib dependency. How can I fix this?
You can manually right click on the pom.xml file in the file tree and select maven > reimport.
Sometimes you'll see a popup saying "Maven projects need to be imported"; you should select Enable Auto-Import.
This option can be found in Preferences > Maven > Importing > [x] Import Maven projects automatically (and is unchecked by default):
What worked for me was changing from using maven (Intellij) version and using my latest version that was installed on my machine previously.
I had a similar problem. The below command resolved the problem. It downloaded all the dependency jars into my IDEA project.
mvn -U idea:idea
I'm merging two projects into one(mergin project1 into project2). Now I'm going to copy all dependencies from project1 pom into project2 pom, like :
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.6</version>
</dependency>
And some others. The build is successful the thing in eclipse annoys me so much like when hovering over import org.apache.commons.configuration.ConfigurationException:
The import org.apache.commons.configuration cannot be resolved
But the build still succeds, what can I do to get rid of these?
If you use m2eclipse, you need to click Maven -> Update Dependencies.
Otherwise I guess you need to regenerate the Eclipse project with mvn eclipse:eclipse.
Try doing 'mvn eclipse:clean eclipse:eclipse'. That will force maven to delete all of the old eclipse configurations for your project and rebuild them from scratch.
Maybe try to edit POM by inserting whitespace and saving it. It should trigger rebuilding of the project. There is also a "Refresh Dependencies" option in m2eclipse plugin.
I had similiar problems when I used Eclipse integrated with Maven and the 1st technique usually helped.
Make sure a build is done successfully and ensure that on your Package Explorer the org.apache.commons jar is found under the Maven Dependencies section (given you have m2eclipse plugin installed in your eclipse)
What version of Eclipse, mvn and m2eclipse? Might want to double-check those.
You shouldn't need to do mvn eclipse:eclipse anymore - that's obsolete. You can try deleting your Eclipse project files and reopening the project (settings, project, classpath).