I am using Springboot 2.1.3.RELEASE version.
Recently I have written DAO, Service, Component and Integration layer test cases using Junit 5 and Mockito framework all these test cases are working fine.
If I want to run all these test cases then individually I have to run each test classes.
To overcome this problem I have implemented Spring Suite using Junit 5.
Recently I made changes in build.gradle file or added junit-platform-runner dependency.
After adding this dependency my Suite class throwing below exception.
The type org.junit.runner.Runner cannot be resolved. It is indirectly referenced from required .class files
Here is my Spring Suite class
SpringSuitTest.java
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.runner.RunWith;
#RunWith(JUnitPlatform.class)
#SelectClasses({someclasseshere.class})
public class SpringSuitTest {
}
My dependencies
// Runtime dependencies
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.3.2")
testRuntime("org.junit.platform:junit-platform-runner:1.5.2")
testCompile("org.junit.jupiter:junit-jupiter-api:5.3.2")
testCompile("org.mockito:mockito-core:2.27.0")
testCompile("org.mockito:mockito-junit-jupiter:2.27.0")
I googled a lot but none of the solutions worked for me.
Recently I have added Junit runner dependency, I am considering may be invalid version I have added
testRuntime("org.junit.platform:junit-platform-runner:1.5.2")
Any idea why I am getting such exception.
Thank you
After adding below 2 dependencies resolved the issue.
testImplementation group: 'org.junit.vintage', name: 'junit-vintage-engine', version: '5.3.2'
testImplementation group: 'org.junit.platform', name: 'junit-platform-runner', version: '1.2.0'
Related
I am using openjdk11 and using lombok library. My #Slf4j works fine in java path (src/main/java) but gets error in test path(src/test/java). The error is " can not find symbol". May I get any insight on why is happening and how to fix?
I am using spring boot and unit tests are running with MockitoJUnitRunner. So the test classes are annotated as
#RunWith(MockitoJUnitRunner.class)
#Slf4j
public class ErrorPathUnitTest {
}
Gradle version: 5.5
LOMBOK_VERSION:1.18.6
I could resolve this following the instruction here: https://projectlombok.org/setup/gradle
Try adding the following gradle dependencies:
compileOnly 'org.projectlombok:lombok'
testCompile 'org.projectlombok:lombok'
Make sure you don't have compileOnly for lombok dependency in build.gradle
I'm writing Minecraft Plugin using IntelliJ IDEA Ultimate with gradle. I have added dependency org.spigotmc:spigot-api:1.13.2-R0.1-SNAPSHOT as compileOnly. During development, I noticed that gradle compiles my code in different way than IntelliJ does. For example, IntelliJ was unable to accept addPassenger on Boat, but gradle compiled it. In the opposite way, if I changed it into setPassenger, IntelliJ didn't mark it as error, but gradle failed to compile. I tried to invalidate caches, reimport, clean, even remove %userprofile%\.gradle directory, nothing helped. As a POC I changed compileOnly to compile and it worked well, IntelliJ and gradle compilation results were consistent. What's the reason?
Ok, I found the solution (and forgot about this question).
I had been using multiple dependencies, and one load another with older version that I loaded implicitly in my build.gradle. However, they weren't exactly the same dependencies, but parallel ones. So gradle could not choose higher version of one dependency. Solution was to exclude this one explicitly loaded dependency and everything worked well.
Before:
dependencies {
compileOnly 'com.sk89q.worldedit:worldedit-bukkit:7.0.1'
compileOnly group: 'org.spigotmc', name:'spigot-api', version: '1.15.1-R0.1-SNAPSHOT'
}
After:
dependencies {
compileOnly('com.sk89q.worldedit:worldedit-bukkit:7.0.1') {
exclude `org.bukkit:bukkit:1.15.1-R0.1-SNAPSHOT`
}
compileOnly group: 'org.spigotmc', name:'spigot-api', version: '1.15.1-R0.1-SNAPSHOT'
}
IntelliJ is not including by logback-classic dependency on classpath when running the application from IDE (it is kotlin application, but it shouldn't matter). My gradle build contains a line:
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.+'
Dependency is correctly listed under "External Libraries" in IntelliJ.
When I try to manually reference a class that the dependency provides in the source code, compiler doesn't complain and it compiles the code successfully.
However, when I run the application I get ClassNotFoundException.
//This import is provided by logback-classic library
import org.slf4j.impl.StaticLoggerBinder
...
// This compiles successfully, but will trigger ClassNotFoundException when run
val singleton = StaticLoggerBinder.getSingleton()
I included the code to list out the runtime classpath:
val cl = ClassLoader.getSystemClassLoader()
val urls = (cl as URLClassLoader).urLs
for (url in urls) {
System.out.println(url.file)
}
And the output, when run from IDE, doesn't include logback-classic. Sample output:
...
/Users/knesek/.gradle/caches/modules-2/files-2.1/commons-httpclient/commons-httpclient/3.1/964cd74171f427720480efdec40a7c7f6e58426a/commons-httpclient-3.1.jar
/Users/knesek/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-databind/2.8.8/bf88c7b27e95cbadce4e7c316a56c3efffda8026/jackson-databind-2.8.8.jar
/Users/knesek/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.module/jackson-module-jaxb-annotations/2.8.8/e2e95efc46d45be4b429b704efbb1d4b89721d3a/jackson-module-jaxb-annotations-2.8.8.jar
/Users/knesek/.gradle/caches/modules-2/files-2.1/com.sun.mail/javax.mail/1.5.6/ab5daef2f881c42c8e280cbe918ec4d7fdfd7efe/javax.mail-1.5.6.jar
/Users/knesek/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-core/1.1.11/88b8df40340eed549fb07e2613879bf6b006704d/logback-core-1.1.11.jar
/Users/knesek/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.1.1/98e484e67f913e934559f7f55f0c94be5593f03c/kotlin-stdlib-1.1.1.jar
/Users/knesek/.gradle/caches/modules-2/files-2.1/org.springframework/spring-beans/4.3.9.RELEASE/daa5abf3779c8cad1a2910e1ea08e4272489d8ae/spring-beans-4.3.9.RELEASE.jar
...
Fascinatingly, logback-core is in there while logback-classic is not (and logback-core is a transitive dependency of logback-classic).
I tried invalidating InteliJ cahces, rebuilding and restarting InteliJ. Compling from gradle and running the jar works fine. Any suggestions?
It seems this a bug in InteliJ for which I'll file a bug report. Including this answer with a workaround here for anyone who might encounter similar issue.
It seems that when there is a certain combinations of dependencies present on the classpath, then when you use gradle dependency like this
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.+'
it will not get included when run from the IDE. But if you include it like this:
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.+'
then it works. First example did work for a while, until I included more dependencies.
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.
I'm experiencing an issue where multiple versions of the same class are showing up in my classpath. The class in question is javax.ws.rs.core.UriBuilder. The version I want to use is brought in by javax.ws.rs:javax.ws.rs-api:2.0.1. However, we also use the Jira rest client library which has a dependency on the older version of jersey (com.sun.jersey:jersey-core) which has included the java.ws packages bundled in it's jar.
Here is an example snippet from the build file:
dependencies {
compile 'com.atlassian.jira:jira-rest-java-client-core:2.0.0-m31'
compile 'javax.ws.rs:javax.ws.rs-api:2.0.1'
compile 'org.glassfish.jersey.core:jersey-client:2.17'
}
I can't remove com.sun.jersey:jersey-core as it uses different package name from the new version and would cause class def not found exceptions in the Jira client.
As far as I can tell, my options at this point are:
Revert to using Jersey 1.x and it's implementation of jsr311
Somehow have gradle exclude the javax.ws package from the old jersey client.
I'd like to keep using the newer version of jersey so #2 would be my ideal solution but I'm not sure if it's even possible. Does anyone know how to go about this? If that's not possible, I'm open to other suggestions.
You can exclude an transitive dependency module like this:
compile ('org.glassfish.jersey.core:jersey-client:2.17') {
exclude group: 'javax.ws.rs'
exclude module: 'javax.ws.rs-api'
}
ref: 50.4.7 here
I found out that com.sun.jersey:jersey-core:1.19 doesn't bundle the javax.ws.rs class files and instead lists them as a compile-time dependency. Adding this snippet to my build.gradle fixed the issue.
configurations.all {
resolutionStrategy {
// For a version that doesn't package javax.ws
force 'com.sun.jersey:jersey-core:1.19'
}
}
exclude the group and module as below.
Ex :
implementation('org.apache.httpcomponents:httpclient:4.5.13') {
exclude group: 'commons-codec', module: 'commons-codec'
}