The import org.springframework.boot.test cannot be resolved - spring

I am currently working on a Spring application using SpringBoot. To manage depedencies of this project, I'm using Gradle.
I wanted to implement some tests but I got this error: "The import org.springframework.boot.test cannot be resolved".
Here is the code.
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
#RunWith(SpringRunner.class)
#SpringBootTest
class ClassTest {
#Test
public void contextLoads() {
}
}
And the content of the build.gradle
plugins {
id "com.moowork.gulp" version "1.2.0"rk.node" version "1.2.0"
id "org.springframework.boot" version "2.1.5.RELEASE"
id "java"
}
configurations {
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
test {
useJUnitPlatform()
}
dependencies {
implementation "org.springframework.boot:spring-boot-starter-web"
developmentOnly "org.springframework.boot:spring-boot-devtools"
testImplementation "org.springframework.boot:spring-boot-starter-test"
implementation "org.springframework.boot:spring-boot-starter-thymeleaf"
implementation "nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect"
testCompile "org.junit.jupiter:junit-jupiter:5.4.2"
annotationProcessor "org.projectlombok:lombok"
implementation "org.projectlombok:lombok"
implementation "org.apache.commons:commons-csv:1.1"
implementation "org.apache.commons:commons-math3:3.0"
}
The simplified stack trace is the following:
java.lang.Error: Unresolved compilation problems:
The import org.springframework.boot.test cannot be resolved
The import org.springframework.test cannot be resolved
SpringRunner cannot be resolved to a type
Class<SpringRunner> cannot be resolved to a type
SpringBootTest cannot be resolved to a type
at climenvi.webapp.ClassTest.<init>(ClassTest.java:12)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
I have tried some solutions found already for this but nothing corrected the problem. Could you help me please ? Thank you for your help.

Related

EnableEurekaClient import doesn't exist

I added spring-cloud-starter-netflix-eureka-client gradle depedency in
my project and shrik the depedency. But when go use
#EnableEurekaClient in my Main class it show me suggestion create
#EnableEurekaClient annotation. Don't show any import file of eureka
client.
Unresolved reference: EnableEurekaClient
productserviceApplication.kt
package com.main.productservice
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
#SpringBootApplication
#EnableEurekaClient
class ProductServiceApplication
fun main(args: Array<String>) {
runApplication<ProductServiceApplication>(*args)
}
gradle.kt
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.5.2"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.5.20"
kotlin("plugin.spring") version "1.5.20"
}
group = "com.main"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.
kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
I am getting error at #EnableEurekaClient
#EnableEurekaClient is deprecated no need to annotate at springboot main applicationit is fine if we add the spring-cloud-starter-netflix-eureka-client dependency in pom and if we have the application name in yml or properties file it will be registered to Eureka Server
Make sure to import the Spring Cloud BOM in your Gradle build definition:
extra["springCloudVersion"] = "2020.0.3"
dependencyManagement {
imports {
mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
}
}
The EnableEurekaClient annotation is in package org.springframework.cloud.netflix.eureka. You should add the following import to your main application file:
import org.springframework.cloud.netflix.eureka.EnableEurekaClient

Stackoverflow error when uses Apache Jmeter Core

I am new in Gradle. I am using my gradle with kotlin dsl script. When I execute using
gradle bootRun
Then it throw StackoverFlow error for log4J
Exception in thread "main" java.lang.StackOverflowError at
java.lang.reflect.InvocationTargetException.(InvocationTargetException.java:72)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498) at
org.apache.logging.log4j.util.StackLocator.getCallerClass(StackLocator.java:110)
at
org.apache.logging.log4j.util.StackLocator.getCallerClass(StackLocator.java:123)
at
org.apache.logging.log4j.util.StackLocatorUtil.getCallerClass(StackLocatorUtil.java:55)
As Soon as I comment the implementation( "org.apache.jmeter:ApacheJMeter_core:5.0") from the build script, it doesn't throw any Error, what I should do? Please help.
Build.Gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
java
kotlin("jvm") version "1.3.11"
"kotlin-spring"
id("org.springframework.boot") version "2.1.1.RELEASE"
}
group = "org.kayd"
version = "1.0-DEV"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation(kotlin("reflect"))
implementation("org.apache.commons:commons-collections4:4.2")
implementation("org.springframework.boot:spring-boot-starter:2.1.1.RELEASE")
implementation( "org.apache.jmeter:ApacheJMeter_core:5.0")
// implementation("org.apache.jmeter:ApacheJMeter_java:5.0")
// implementation( "org.apache.jmeter:ApacheJMeter_http:5.0")
// implementation( "org.antlr:antlr:3.1.3")
// implementation( "org.json:json:20180813")
testCompile("junit", "junit", "4.12")
}
configure<JavaPluginConvention> {
sourceCompatibility = JavaVersion.VERSION_1_8
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
Main Fun file
package org.kayd
import org.springframework.boot.Banner
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.runApplication
#EnableConfigurationProperties
#SpringBootApplication
open class StartApplication
fun main(args: Array<String>) {
runApplication<StartApplication>(*args) {
setBannerMode(Banner.Mode.OFF)
setLogStartupInfo(true)
}
}
If you list your project dependencies you will see that
org.apache.jmeter:ApacheJMeter_core:5.0 has transitive dependency on commons-collections:commons-collections:3.2.2
which in its turn clashes with your org.apache.commons:commons-collections4:4.2
So you're suffering from a form of a Jar Hell. You cannot reliably use 2 different versions of a same JAR in one application as the order of loaded classes in the CLASSPATH is a big question mark. Theoretically you can control this using a custom classloader however it might be an overkill for your project.
The easiest would be just removing your implementation("org.apache.commons:commons-collections4:4.2") dependency and re-write the impacted code to use Commons Collections 3 syntax.
Alternatively you will have to reconsider the way you're using JMeter in your application and go for one of the alternative JMeter execution options.

Gradle Spring Boot Project Cannot Run From IDEA [duplicate]

This question already has answers here:
NoClassDefFoundError after IntelliJ IDEA upgrade
(3 answers)
Closed 5 years ago.
I am struggling to set up the spring boot project that i have imported in intelliJ IDEA 2016 1.4.
Every time i run . I get the below Exception:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
at com.patientConnect.DemoApplication.main(DemoApplication.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more
My gradle settings (linked gradle projects,user gradle wrapper ) all looks good.
package com.Demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemoApplication {
public DemoApplication() {
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
build.gradle:
buildscript {
repositories {
mavenLocal()
mavenCentral()
maven {
url 'https://plugins.gradle.org/m2/'
}
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.4.2.RELEASE'
classpath 'gradle.plugin.com.jamesward:atom-gradle-plugin:0.0.1'
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-parent:1.5.4.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-devtools'
compile 'org.springframework.boot:spring-boot-starter-security'
compile 'org.springframework.security.oauth:spring-security-oauth2'
compile 'org.apache.httpcomponents:httpclient'
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile 'org.postgresql:postgresql'
compile 'org.springframework.boot:spring-boot-starter-jdbc'
compile 'org.hibernate:hibernate-entitymanager'
}
allprojects {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
import org.gradle.internal.os.OperatingSystem
task devClasses(type: Exec) {
if (OperatingSystem.current().isWindows())
commandLine 'gradlew', '-t', 'classes'
else
commandLine './gradlew', '-t', 'classes'
}
task devBootRun(type: Exec) {
if (OperatingSystem.current().isWindows())
commandLine 'gradlew', 'bootRun'
else
commandLine './gradlew', 'bootRun' //, '--debug-jvm'
}
import java.util.concurrent.*
task dev() << {
def devClassesFuture = Executors.newSingleThreadExecutor().submit({ devClasses.execute() } as Callable)
def devBootRunFuture = Executors.newSingleThreadExecutor().submit({ devBootRun.execute() } as Callable)
devClassesFuture?.get()
devBootRunFuture?.get()
}
task stage {
dependsOn build
}
Try deleting your SpringBoot dependency on your computer.
It will be located at ~/.gradle/caches/modules-2/files-2.1/org.springframework.boot Assuming you kept your gradle dependencies in the default location.
rm -rf ~/.gradle/caches/modules-2/files-2.1/org.springframework.boot
or to delete all dependencies (Just to be sure if no other dependencies is corrupted)
rm -rf ~/.gradle/caches/modules-2/files-2.1/*
then to re-download
gradle build --refresh-dependencies

slf4j LoggerFactory class definition not found with Gradle

I'm trying to build and run a Spark webserver that uses slf4j logging in IntelliJ. It builds fine (also in IntelliJ) on the computer that was used to originally add logging, but not on my machine.
Here's the full build.gradle file (see project(':webserver') for the part that uses the logger:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.avast.gradle:docker-compose-gradle-plugin:0.3.27"
}
}
subprojects {
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
apply plugin: "java"
apply plugin: "idea"
apply plugin: "checkstyle"
apply plugin: "pmd"
apply plugin: "findbugs"
apply plugin: "application"
apply plugin: "com.avast.gradle.docker-compose"
checkstyle {
configFile = "../config/checkstyle/checkstyle.xml" as File
toolVersion = "6.0"
}
}
project(':webserver') {
mainClassName = "org.passport.webserver.WebServer"
dependencies {
compile project(':core')
compile "com.sparkjava:spark-core:2.6.0"
testCompile 'junit:junit:4.12'
compile group: 'org.json', name: 'json', version: '20090211'
compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.6.1'
}
sourceSets {
main {
java {
srcDir 'src/main/java/'
}
}
}
}
project(':customsUI') {
mainClassName = "org.passport.customsui.CustomsUI"
dependencies {
compile project(':core')
testCompile 'junit:junit:4.12'
}
sourceSets {
main {
java {
srcDir 'src/main/java/'
}
}
}
}
project(':handlerUI') {
mainClassName = "org.passport.handlerui.HandlerUI"
dependencies {
compile project(':core')
testCompile 'junit:junit:4.12'
}
sourceSets {
main {
java {
srcDir 'src/main/java/'
}
}
}
}
project(':core') {
mainClassName = "org.passport.core.PassPortChaincode"
dependencies {
compile 'io.grpc:grpc-all:0.13.2'
compile 'commons-cli:commons-cli:1.3.1'
compile 'org.glassfish:javax.json:1.1.0-M1'
compile files('lib/shim-client-1.0.jar')
testCompile 'junit:junit:4.12'
}
sourceSets {
main {
java {
srcDir 'src/main/java/'
}
}
}
}
Here's the code that uses the logger:
package org.passport.webserver;
import org.passport.webserver.endpoints.ContainerClaimDao;
import org.passport.webserver.endpoints.PackageClaimDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static spark.Spark.before;
import static spark.Spark.get;
import static spark.Spark.path;
import static spark.Spark.post;
/**
* The server matches requests (routs) and calls the appropriate endpoints.
*/
public final class Server {
private static Logger logger = LoggerFactory.getLogger(Server.class);
private static ContainerClaimDao containers = new ContainerClaimDao();
private static PackageClaimDao packages = new PackageClaimDao();
private Server() {
}
/**
* Starts the router.
* #param args run arguments
*/
public static void main(String[] args) {
path("/containers/:container-id", () -> {
before("/*", (q, a) -> logger.info("Received api call: " + q.url() + "."));
post("", (request, response) -> containers.add(request, response));
get("/container-claims", (request, response) -> containers.get(request, response));
get("/package-claims", (request, response) -> packages.get(request, response));
});
}
}
And here's the error I get:
"C:\Program Files\Java\jdk1.8.0_91\bin\java" -Didea.launcher.port=7534 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.2.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_91\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\rt.jar;C:\Users\lover\repositories\passport\webserver\build\classes\main;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.2.4\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain org.passport.webserver.Server
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at org.passport.webserver.Server.<clinit>(Server.java:18)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:123)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 4 more
I've looked at several questions on StackOverflow, but none seem to solve the problem:
java.lang.NoClassDefFoundError: org.slf4j.LoggerFactory - I'm building with gradle, don't think I should have to download/ put some jar in my classpath
ClassNotFoundException: org.slf4j.LoggerFactory - Same as above
NoClassDefFoundError: org/slf4j/Logger - Again jar stuff
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory - I'm not sure whether "fat jars" / ShadowJar apply, and not sure how to modify my build file to use them
NoClassDefFoundError: org/slf4j/LoggerFactory while creating a runnable *.jar with gradle - I've tried refreshing gradle several times, restarting IntelliJ, and even restarting my restarting my computer
Gradle Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory - See below
I've tried the following dependencies:
compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.6.1'
compile 'org.slf4j:slf4j-api:1.7.24' and compile 'org.slf4j:slf4j-simple:1.7.24'
compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.6.1'
None of this seems to make a difference. Any ideas?
If you're using Gradle add these 2 lines to your build.gradle dependencies to use Spark 2.6.0
compile "com.sparkjava:spark-core:2.6.0"
compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.21'
I ended up deleting my Gradle cache (.gradle folder) and re-downloading everything, which solved the problem.

unable to resolve ssh class in gradle.build

I am trying to invoke a custom groovy plugin from gradle build file. But I am getting the error while resolving the classes for ssh. Below is the build file, part of custom groovy plugin and the error.
build.gradle
plugins {
id 'org.sonarqube' version '2.0.1'
id 'groovy'
id 'org.hidetake.ssh' version'2.7.0'
}
dependencies {
compile gradleApi()
compile localGroovy()
}
CustPlugin.groovy
package com.nielsen.gradle
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.text.SimpleDateFormat
import org.gradle.api.Project
import org.gradle.api.Plugin
import org.gradle.api.GradleException
import org.gradle.api.plugins.BasePlugin
import org.gradle.api.tasks.bundling.Zip
import org.hidetake.groovy.ssh.Ssh.*
import org.hidetake.groovy.ssh.core.Service
import com.nielsen.gradle.cmRegistry.CMRegistryPlugin
Error
C:\Users\528302\Documents\gradle_all\projectf1>gradle build
:compileJava UP-TO-DATE
:compileGroovy
startup failed:
C:\Users\528302\Documents\gradle_all\projectf1\src\main\groovy\com\nielsen\gradle\CustPlugin.groovy: 14: unable to resolve class org.hidetake.groovy.ssh.Ssh
# line 14, column 1.
import org.hidetake.groovy.ssh.Ssh
^
C:\Users\528302\Documents\gradle_all\projectf1\src\main\groovy\com\nielsen\gradle\CustPlugin.groovy: 15: unable to resolve class org.hidetake.groovy.ssh.core.Service
# line 15, column 1.
import org.hidetake.groovy.ssh.core.Service
^
C:\Users\528302\Documents\gradle_all\projectf1\src\main\groovy\com\nielsen\gradle\CustPlugin.groovy: 17: unable to resolve class com.nielsen.gradle.cmRegistry.CMRegi
stryPlugin
# line 17, column 1.
import com.nielsen.gradle.cmRegistry.CMRegistryPlugin
^
Please help resolving this...Thanks.
You are mixing two things. By using the plugins { } closure you are adding dependencies for the buildscript itself. But in this case the code you are building has a dependency on some library, not the buildscript.
Try to add the following into dependencies { }
compile group: 'org.hidetake', name: 'groovy-ssh', version: '2.8.0'
so you end up having
plugins {
id 'org.sonarqube' version '2.0.1'
id 'groovy'
id 'org.hidetake.ssh' version'2.7.0'
}
dependencies {
compile gradleApi()
compile localGroovy()
compile group: 'org.hidetake', name: 'groovy-ssh', version: '2.8.0'
}

Resources