mockito in gradle not working for java project - java

I have a basic java project and I want to run tests using Mockito (since I use it at work and it's easy). So I referred to this link and added the following to my build.gradle
dependencies {
testCompile 'junit:junit:4.11'
testCompile "org.mockito:mockito-core:1.+"
}
Even though I think mavenCentral() should be enough, I went ahead and added these to my repositories list
repositories {
mavenLocal()
jcenter()
mavenCentral()
}
The ./gradlew clean build runs perfectly fine, but when I try to add the import for Mockito it doesn't get it. And my External Libraries folder in the project doesn't have the Mockito jar. I even tried using mavenLocal() in the hope that it'll pick it up from my local .m2 directory, but it doesn't. I've been looking around and trying out all combinations for 2 hours now with absolutely no result. I don't want to add the jar to the project. I want to let gradle pull it from the central repo and compile it.
Thanks for any help in advance.

I found the answer here Apparently clicking on that refresh button refreshes the dependencies. Else right click on module -> Open Module settings -> Go to Dependencies tab and add your dependency either from your local m2 folder or provide the maven central URL for the dependency.
I was too used to Eclipse doing stuff for me I guess. :)

Is this in Eclipse? If so, right-click on the project and go gradle > Update All (I think it is - I don't have open at moment and don't think about it any more). This will update the dependencies.

If you are referring those libraries other than test package, that is main package you will get this error.
Because you provided scope testCompile in build.gradle, so these libraries are available only in test package.
So, change testCompile to compile in build.gradle.

Related

How to include reference to library in java gradle project

I am coming from a C# background. I am used to NuGet and Visual Studio project references so the Java ecosystem has confused me quite a bit.
I have a gradle library project. I want to import org.apache.commons.codec.binary.Base64;
However I keep getting cannot resolve errors.
I am using VSCode as my IDE and I would like to include the codec dependancy. How would I achieve this in VSCode/gradle.
I have downloaded the commons-codec-1.14.jar file, but don't know where to put it in the project.
Gradle is a tool that, among other things, manages your dependencies. This means that, you do not need to manually download and add dependencies to your project. Gradle solves this for you.
See the official documenation on how to handle dependencies with Gradle.
You probably have a build.gradle file, in which you need to include your dependency. It would look something like:
dependencies {
implementation 'commons-codec:commons-codec:1.14'
}
This lets Gradle know that you have a dependency to version 1.14 of commons-codec which your codes need to build and run.
This will automatically be downloaded from a remote repository, which you also can specify in your build.gradle file:
repositories {
mavenCentral()
}
This tells gradle to download the dependencies from Maven Central, which probably is the most typical Maven/Gradle repository and most likely hosts most dependencies you would need.

IntelliJ IDEA using JavaScript "version" of dependency specified in Gradle build file?

This issue just recently (past couple days) started occurring on one of my development machines.
I'm using Eclipse's Vert.x dependency for a web project:
build.gradle
dependencies {
...
// Kotlin
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
// Vert.x for web framework
compile group: 'io.vertx', name: 'vertx-core', version: '3.4.2'
compile group: 'io.vertx', name: 'vertx-web', version: '3.4.2'
...
}
This has worked fine in the past - I think the triggering action was my upgrading to IntelliJ 2017.2.2, but now:
IntelliJ cannot resolve any of the -web imports:
If I examine the Dependencies list for my Module, the JavaScript version of the dependency is shown?
How did this happen, and how can I make sure it's properly recognized as a Java dependency?
Edit: Sample project available here: https://youtrack.jetbrains.com/issue/IDEA-177950
This is a bug in the Kotlin plugin which is fixed in version 1.1.4-2. After you update the plugin, you need to delete the incorrect libraries and reimport your project from Gradle to have your project fixed.
If you face such problems, the first two things you always can do is:
(in IntellJ) File > Invalidate Caches/Restart
(in IntellJ's Gradle Bar) Press button for Refresh all gradle dependencies
If this doesn't help, please check if ./gradlew clean testClasses succeeds or also fails with such an error.

Installing Mockito using Gradle

I have installed Gradle by adding the path to it into the system variables. I am quite new to Java and this is the first time that I am trying to install an external library for it. On the Mockito web-page, they say that one can:
Declare a dependency on “mockito-core” library using your favorite
build system. With Gradle one can do:
repositories { jcenter() }
dependencies { testCompile "org.mockito:mockito-core:1.+" }
So I have no idea what it means. I changed the directory in cmd to the Gradle folder and tried to execute these commands, but that is not how one is supposed to do it. Can you give me a hand here?
You have to create a build.gradle file where you can insert the dependency. I recommend using an ide like eclipse or IntelliJ which can generate a gradle project for you so you don't have to do this manually. Just install the corresponding Gradle Plugin. This also makes sure you have a correct project structure.

Issues with Dependency Libraries in Eclipse Using Gradle

I a newbie in Gradle and trying to find my way around it but facing some issues that needs to be resolved before i can proceed my Gradle tutorials.
At the moment i have compiled the app from the command prompt and seems to be working.
The issues i have now is that i can't see the dependencies (spring libraries) that Gradle has downloaded in Eclipse. If i look into the "Project and External Dependencies" folder in eclipse under the Gradle project i have created, i can't see the spring libraries there. For instance in Maven, the moment you add a dependency and save the POM file, the dependencies are added to the library instantly. Because of this issue red-error-markers appear in the source codes that are using the spring libraries though the application runs perfect from the command line.
How do i fix this problem? Does Gradle put the libraries somewhere?
My buld.gradle file is
apply plugin: 'java'
apply plugin: 'application'
mainClassName = System.getProperty("mainClass")
repositories {
mavenCentral()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.14'
testCompile 'junit:junit:4.12'
compile 'org.springframework:spring-context:4.0.5.RELEASE'
}
thanks.
Extra question :: is there a way to let Gradle run in eclipse so that the output of my application is displayed in the eclipse console?

Use Gradle to download external dependencies without compiling?

Can I use Gradle to download Java external dependencies without compiling my source code?
The external dependencies have made big changes to package structure since I created my code. I would like to use Gradle to download the new versions and then fix my import statements using the tools in my IDE.
Gradle build seems to be failing without downloading the dependencies because it can't compile my source.
Thanks.
You can't download your dependencies with some custom task, which aims just for that. Dependencies are downloaded on demand, that means, that if you have changed dependencies versions in your gradle build script and then call the task, which have to compile your sources, all the dependencies will be downloaded. Sure, if the imports get wrong, your build will fail and you'll need to update your imports.
So, in other words, if you've changed dependencies versions and then called some task, that compile your sources, your dependencies will be downloaded automatically before the compilation start.
Here is a gradle task to download manually all dependency sources https://gist.github.com/ngtignacio/d0720b7a565729037d0fef1936655793
I adapted the script at https://stackoverflow.com/a/58748741/2439283
It should download all available sources even if the project does not compile.

Categories

Resources