I am experiencing a problem I hope you can help with.
I want to use Mockito in my Spring Boot w/Gradle project, but STS cannot resolve the dependancy.
I have the following in my build.gradle file;
repositories { jcenter() }
dependencies { testCompile('org.mockito:mockito-core:1.+') }
When I do a ./gradlew --info build I can see that it is resolving Mockito:
Resolved versions: {org.mockito:mockito-core=1.+}
Using version '1.+' for dependency 'org.mockito:mockito-core:1.+'
Using version '1.+' for dependency 'org.mockito:mockito-core:1.10.19'
After a ./gradlew cleanEclipse eclipse it is in my STS Project's Build Path
My Code File is showing the following message:
I have another project, setup in exactly the same way and it is working fine.
Please help me out guys, Luke.
Use with a static import:
import static org.mockito.Mockito.when; ...or...
import static org.mockito.Mockito.*;
To add to #Harshad's response, you can make your life easier and let Eclipse handle the static imports by adding the mockito methods to your favorite imports
This will then result in the following hint:
Add the dependencies with androidTestImplementation "org.mockito:mockito-core:2.28.2" instead of the testImplementation "org.mockito:mockito-core:2.28.2"
The dependencies added with androidTestImplementation will be available in:
app\src\androidTest
The dependencies added with testImplementation will only be available in the android test folder available on:
app\src\test
Add testCompile('org.mockito:mockito-core:3.1.0') dependency to build.gradle and then download sources works for me in Netbeans IDE. Version number can be different.
Related
I have a project that uses buildSrc to create a plugin to avoid duplicate logic, one of this type of duplicate is test dependencies.
I notice that when we publish the jar that the test dependencies are not included in the pom file. I tried modifying the versionMapping but can't find a solution to get the dependencies added
tried
versionMapping {
allVariants {
fromResolutionOf('testRuntimeClasspath')
}
}
but see the test dependencies are still not found.
The buildSrc plugin defines the following dependencies
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testImplementation 'ch.qos.logback:logback-classic:1.2.3'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}
How do I get these dependencies to show up in the published pom?
Thanks!
Edit: The main reason we want this is to act as documentation on how to use the -tests.jar also published, as the main consumer of this library uses -tests.jar for testing
It's probably very simple, but only to people who know what they are doing.
I have a Java program that imports these two:
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
As an aside, I don't want to use the lang3 package but the lang package.
I do not have anything in my Gradle file about these. When I try to build the file, it gives me errors for these two, saying the packages do not exist.
My questions are:
Do I need to add them as "compile" or as "api"?
What is the exact syntax? I have lines that look like this:
api group: 'commons-httpclient', name: 'commons-httpclient', version: '3.1'
How do I find the right name (or should I just invent one)? and the version?
Anything your code needs (besides basic JRE classes) is a dependency for your code. Gradle manages these dependencies, usually downloading them from a repository.
First you need to find such a repository. You probably have repositories already configured in your build.gradle, like so:
buildscript {
repositories {
mavenCentral()
// maybe more repositories
}
}
That means Gradle will try to download dependencies from Maven Central. You can either do a web search for "gradle" and your dependency, or go to repository and search, or check the dependency's homepage.
You'll end up with a dependency name and version like 'org.apache.commons:commons-lang3:3.12.0'. This needs to go in your build.gradle.
Gradle has different dependencies:
buildscript dependencies provide code that Gradle needs to execute to build your project, e.g., a tool to pull in version control system information or generate code
implementation dependencies are dependencies your code needs to run, like a logging framework or JSON parser or PDF generator
test dependencies are dependencies needed to run your automated tests, like JUnit
Depending on where you need the dependency, you put it in the buildscript or the dependencies block.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.tmatesoft.svnkit:svnkit:1.9.+'
}
}
dependencies {
implementation 'pdfbox:pdfbox:0.7.3'
testImplementation 'junit:junit:4.+'
}
You don't need to repeat the implementation dependencies for the testImplementation btw, as it inherits them automatically.
You can define your own configurations as need; see the Gradle manual on dependencies, for example if you have different test suites (unit, integration, performance, ...) that need different dependencies.
you'll have to go to their official website and get the implementations then add those to the dependencies(you'll find it at the bottom of the file) in the build.gradle(module) it would look something like -
the $lifecycle_version might be somethiing like 1.2.3 or some version number.
this is what I got (not exactly sure if this is right)-
implementation 'org.apache.commons:commons-lang3:3.12.0'
got from library website in the gradle short tab
in the build.gradle(project), look for maven repo.
Once done, you'll be able to import the respective libraries.
After creating class for unit tests like in official documentation I imported com.google.common.truth.Truth.assertThat and org.junit.Test libraries, but android studio can't pick it up and shows errors "Cannot resolve symbol "Truth"" and "Cannot resolve symbol "Test"".
After searching the ways to solve this problem I came across a question on StackOverflow that asked similar thing. I did all the things that were recommended over there but I still can't import those classes.
My project build.gradle
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
My project module build.gradle
dependencies {
testImplementation 'junit:junit:4.13.2'
testImplementation 'com.google.truth:truth:1.1.3'
}
My test class
import com.google.common.truth.Truth.assertThat;
import org.junit.Test;
Android gradle plugin requires java 11 to run.
Please change gradle java plugin to 11 like below screenshot.
I am looking to add a test dependency to my project which uses gradle. What I simply need is for gradle to download the dependency jar so I can import part of the package. What I need is the following package:
import org.springframework.security.test.*
So I edited my gradle file to contain
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
runtime('org.hsqldb:hsqldb')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test') // This is what I added to try and download the new dependency
}
I tried building the project after entering this. Unfortunately this didn't work and I cannot import org.springframework.security.test.* I then ran gradlew test which actually had some success and showed:
Download https://repo1.maven.org/maven2/org/springframework/security/spring-security-test/4.0.3.RELEASE/spring-security-test-4.0.3.RELEASE.pom
Download https://repo1.maven.org/maven2/org/springframework/security/spring-security-test/4.0.3.RELEASE/spring-security-test-4.0.3.RELEASE.jar
Though I have no idea where it downloaded to. Navigating to $HOME/.gradle/caches/modules-2/files-2.1/org.springframework.security I can see no sign of the downloaded package (and Idea doesn't recognise it in its external libraries). Running gradle build --refresh-dependencies didn't seem to work.
Any help would be very much appreciated.
Closes SO question I could find
I had the same issue, it works if a specific version is included in the dependency, such as:
testCompile group: 'org.springframework.security', name: 'spring-security-test', version: '5.1.6.RELEASE'
I'm using gradle to build a groovy/java application.
This worked fine until I added a dependency to google guice 3.0.
Gradle does not add the guice jars to the compilation classpath, at least it seems so.
I get errors like these:
C:\dev\workspaces\initial>gradle -q compileJava
C:\dev\workspaces\initial\src\main\java\com\comp\test\solmon\di\GuiceDI.java:3: package com.google.inject does not exist
import com.google.inject.Guice;
^
C:\dev\workspaces\initial\src\main\java\com\comp\test\solmon\di\GuiceDI.java:4: package com.google.inject does not exist
import com.google.inject.Injector;
In my build.gradle file I have the following dependencies:
dependencies{
runtime 'com.beust:jcommander:1.27'
runtime "org.slf4j:slf4j-api:1.7.1"
runtime "ch.qos.logback:logback-classic:1.0.7"
runtime 'com.google.inject:guice:3.0'
testRuntime 'junit:junit:4+'
}
I'm developing the application in Springsource Tool Suite 2.9.2 with its gradle plugin and it uses gradles dependency management to get all dependencies. Sts manages to compile the code just fine, it's only gradle that fails.
I've tried to run the gradle compilation with the "--debug" parameter but I can not see which classpath gradle gives to the compiler.
Any ideas how to get gradle to compile my application?
You've added Guice to the runtime dependencies (i.e. the dependencies necessary to run the application, but not to compile it). Add it to the compile dependencies:
dependencies {
...
compile 'com.google.inject:guice:3.0'
}
A compile dependency is also a runtime dependency, obviously.