ClassNotFoundException on Spring JavaMailSenderImpl in JUnit test with Spring Boot - maven

I've only just started a new project with Spring Boot, which I'm trying to get to grips with. I'm using Spring Tool Suite which has created a Spring Boot project for me and loaded some dependencies which I know I'll need further down the line (specifically Spring Batch and Spring Data)
This is my pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- JDBC driver -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.0.jre8</version>
</dependency>
</dependencies>
I've been reading the reference documentation and know that I'll need mail at some point down the line, so I added this to my application.properties file:
spring.mail.host=my.mail.host
I then tried to build the application with mvn package but it threw an error in the tests. The test consists of the following:
package its.idcard.batch;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
#RunWith(SpringRunner.class)
#SpringBootTest
public class IdCardBatchApplicationTests {
#Test
public void contextLoads() {
}
}
So all it's doing is trying to load the application context, but it fails with a ClassNotFoundException on org.springframework.mail.javamail.JavaMailSenderImpl.
I know this is a problem purely with the test, as after adding a bit of Spring Data stuff, and skipping tests in maven, my stub application runs as expected (and looking in the generated jar, I can see that spring-context-support-4.3.4-RELEASE.jar is present, containing the offending class), so it appears to be a classpath issue on the tests but I'm stumped. If I comment out the spring.mail.host line in the properties file, the test runs without problems.
Any ideas would be gratefully appreciated.

I think that the problem that I was just having appeared to be corrupt jar files in my maven repository. I decided to manually create the MailSender bean in my configuration class to see if that helped, but Eclipse was flagging up the class as not found, even though the jar containing it was in the maven dependencies. I then tried mvn compile from the command line, which gave me a more useful error, indicating corrupt jars, so I deleted the offending jars from my local repository and had maven re-download them and now I'm no longer getting the error in the configuration above.
Hope this helps others.

Related

not able to resolve import org.springframework.amqp.*; statement in my spring boot project

I'm trying to use the spring apmq core package in my spring boot project. I have added the dependency in my pom file but for some weird reason the import statement for org.springframework.amqp is not getting resolved. Any idea why is this happening??
Here is the dependency I'm using in my pom file-->
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-amqp</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
And this is the import statement I'm using in my config files
import org.springframework.amqp.*;
I've already tried invalidating the caches and restarting intellij and it doesn't work for me.
You should use the Spring Boot Starter for AMQP instead:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

Spring Boot Application displays no error, but tomcat not initialized and application.properties is unused

I created a new project using Spring Initializr, and opened it with Intellij IDEA version 2020.3.2 ide. After maven installed all dependencies in the pom.xml file, I ran the main method in the DemoApplication class. I didn't add anything new or different than the default project created by the Spring Initializr.
Two Problems occured:
1- Console outputted only these 3 logs, and Tomcat wasn't initialized
2021-02-03 23:48:45.073 INFO 15872 --- [ main] com.example.DemoApplication: Starting DemoApplication using Java 1.8.0_281 on DESKTOP-M with PID 15872 (D:\demo\target\classes started by M in D:\demo)
2021-02-03 23:48:45.078 INFO 15872 --- [ main] com.example.DemoApplication : No active profile set,falling back to default profiles: default
2021-02-03 23:48:46.355 INFO 15872 --- [ main] com.example.DemoApplication: Started DemoApplication in 2.062 seconds (JVM running for 2.87)
Process finished with exit code 0
2- Anything I write in the application.properties file turns grey (unused)
I watched a lot of tutorials, and did exactly as they do. Their console output always has more than 3 statements and is even colored, which mine isn't
My pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
DemoApplication.java
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Would appreciate it if you would help me figure out how to correctly run the project and have the server up and running.
Should I add anymore run Options?
External Libraries:
This issue is reproducible with a simple war initializer, when you run DemoApplication with a "standalone java" (IDE run) configuration.
The solution/correct usage is: To run (the project) in a "maven" spring-boot:run (IDE run) configuration.
Like:
It looks like you are using IntelliJ IDEA Community Edition which has no support for Spring Boot.
That is why the properties are not highlighted and the output is not colored.
IntelliJ IDEA Ultimate will use Spring Boot Run/Debug configuration type automatically:
As a workaround you can run your code via Maven spring-boot:run.
I saw the same behavior when I setup a project with pox.xml you provided.
The "Include dependencies with "Provided" fixed it for me. However I had to reload the Maven dependencies like this a few times: pom.xml (right click) -> Maven -> Reload Project.
I would still suggest that you remove <scope>provided</scope> in spring-boot-starter-tomcat.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!-- Comment this out -->
<!-- <scope>provided</scope> -->
</dependency>
Here is a good answer - Java Spring Boot - spring-boot-starter-tomcat dependency doesn't work with scope provided when running locally
If you still need this project to run in an external Tomcat container and thus want to mark the spring-boot-spring-boot-starter dependency as "provided", you can specify a separate profile for that configuration - https://stackoverflow.com/a/42302247

ClassNotFoundException with Spring WebFlux Security

I'm having the following problem:
I am trying to setup a basic Spring Boot (2.0.0.M2) Project containing Spring Webflux and Spring Security.
My pom.xml looks like this (generated via start.spring.io):
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
...
</dependencies>
SpringSecurityConfig:
#EnableWebFluxSecurity
public class SecurityConfig extends WebFluxSecurityConfiguration {}
I am not getting any further because I get an exception on startup:
Caused by: java.lang.ClassNotFoundException: org.springframework.security.web.server.context.SecurityContextRepository
So I figure that the whole org.springframework.security.web.server package is not on the classpath, although it should be there, since it is included in the API docs.
Seems like I am doing something wrong, but I don't know what.
Currently Spring Boot's spring-boot-starter-security does not include spring-security-webflux.
Add it as project dependency explicitly.
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-webflux</artifactId>
</dependency>
See this issue : Provide auto-configuration for WebFlux-based security.
BTW, check my working codes: https://github.com/hantsy/spring-reactive-sample/tree/master/boot
Updated: In Spring Boot 2.0.0.RELEASE, spring-boot-starter-security includes webflux support.

CrudRepository cannot be resolved to a type

I'm working on a Spring Boot project (generated by Spring Initializr), using Maven. I want to create a CrudRepository, but I'm getting the error "CrudRepository cannot be resolved to a type" and the package org.springframework.data.repository does not contain the CrudRepository class. I tried to follow a bunch of tutorials to understand what is wrong and I didn't find anything. My POM looks right to me, I don't have any build failure when I run maven clean and package goals, I tried to update the project and download sources, but nothing works. Eclipse can't find the CrudRepository class.
Here is my POM file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myquickstart</groupId>
<artifactId>SpringQuickstart</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringQuickstart</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
And my repository:
package com.myquickstart.repositories;
import org.springframework.stereotype.Repository;
import org.springframework.data.repository.CrudRepository;
import com.myquickstart.entities.Movie;
#Repository
public interface MovieRepository extends CrudRepository<Movie, Long> {
}
Thanks for your help
Edit:
I've updated the project with the "Maven => Update project" in Eclipse. After that, I got this error in the "Problems" view:
C:/Users/carrm/.m2/repository/org/hibernate/hibernate-core/5.0.11.Final/hibernate-core-5.0.11.Final.jar' in project 'SpringQuickstart' cannot be read or is not a valid ZIP file
I'm not sure to know how to solve this/what is the link with my CrudRepository problem.
P.S - I know the original answer used interface not class. But as we can't ask same question again. The answer is for beginner who may missed the interface part.
We generally get this type of error when we create the repository as class instead of interface. CrudRepository is an interface, you most probably extend it using an interface only:
Wrong (Some times when we create the repository we create a class by mistake ):
public class MovieRepository extends CrudRepository<Movie, Long>
Error: CrudRepository cannot be resolved to a type
Just changed it to interface:
public interface MovieRepository extends CrudRepository<Movie, Long>
It looks like it was an issue with Maven libraries. I deleted the whole content in .m2/repository and ran Maven > Update project in Eclipse, so that Maven had to download the whole content again. No more error after this!
Edit
As pointed out by user3578953, executing maven-clean does the same thing that I did by deleting the whole m2 repository content. I didn't know much about Maven when I first asked this question, but this is obviously a better way to solve the issue.
If you are having this problem, check if the org.springframework.data.repository.CrudRepository import is working. If it is then this answer probably won't be the solution. If it is not working and the "data" part is highlighted, then check your pom.xml and if you don't have it already, add the following dependency to it:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
After adding the dependency, delete your local .m2 directory and build from the command-line. You may also want to reimport the maven project to your workspace.
check mavendependencies for spring-data-jpa-...RELEASE.jar if it contains CrudRepository.class under org.springframework.data.jpa.repository package.
If the class file is there, just maven->update project and run->maven-clean.
If the class file is not there, just search the package in mvnrepository.com.
All seems nice with your dependencies, please try next variants:
build project with Maven from command line($ mvn spring-boot:run)
In Eclipse IDE: right click on project-Maven-update project
i had the same problem, just delete all .m2 content and re-import Maven. it works!
Here is how i got the message to disappear:
In my case I wasn't able to import CrudRepository
to Resolve this
1. Using Intellij idea
type "extends CrudRepository" click on CrudRepository and press Alt and Enter
Click add maven dependency
Select CrudRepository
2. Adding dependency
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.4.8</version>
</dependency>
Sometimes
(after adding dependency) you will get an error, importing org.junit.jupiter.api.Test
then add
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.0-M1</version>
<scope>test</scope>
</dependency>
Add JPA dependency when creating your project. Worked for me!
CrudRepository is an interface not a class. Replace extends with implements.

SpringApplicationConfiguration not found: Erroneous spring-boot-starter-test content?

Getting a compilation error in Maven:
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /C:/prototypes/demo-sse-spring-boot-master/src/test/java/com/cedric/demo/sse/SseDemoApplicationTests.java:[6,37] package org.springframework.boot.test does not exist
[ERROR] /C:/TITAN/demo-sse-spring-boot-master/src/test/java/com/cedric/demo/sse/SseDemoApplicationTests.java:[10,2] cannot find symbol
symbol: class SpringApplicationConfiguration
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
Maven repo seems to have the jar present:
howerever that jar doesn't have any compiled classes inside it. only META-INF dir:
Is that by design? Where do I get the jar containing SpringApplicationConfiguration class to make Maven happy?
Here's the relevant parts of my pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>jquery</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
In your release the #SpringApplicationConfiguration annotation no longer exists. The new annotations are :
#RunWith(SpringRunner.class)
#SpringBootTest(classes = YourApplicationMainClass.class)
#WebAppConfiguration
spring-boot-starter-test, like all the other Spring Boot starters, is really just a pom that pulls in a number of other dependencies transitively. It only has a jar to keep some build systems that don't like pom-only dependencies happy.
It looks like you have upgraded an application from Spring Boot 1.4 to Spring Boot 1.5. Spring Boot 1.5 removes a number of classes that were deprecated in 1.4, including org.springframework.boot.test.SpringApplicationConfiguration.
I would recommend dropping back to Spring Boot 1.4.4.RELEASE and fixing all of the deprecation warnings. You should then be able to upgrade to Spring Boot 1.5.1.RELEASE without difficulty.
As the error is due to the upgrade of Spring Boot from 1.4 to 1.5, its important to note (from below) that several new classes that are introduced in 1.4 deprecated some of the existing classes leading way to finally getting removed in 1.5. The details of such can be found at: Spring boot release notes
Quoted from website (edited):
Additionally, Spring Boot 1.4 (and above) attempts to rationalize and simplify the various ways that a Spring Boot test can be run. You should migrate the following to use the new #SpringBootTest annotation:
From #SpringApplicationConfiguration(classes=MyConfig.class) to #SpringBootTest(classes=MyConfig.class)
From #ContextConfiguration(classes=MyConfig.class, loader=SpringApplicationContextLoader.class) to
#SpringBootTest(classes=MyConfig.class)
From #IntegrationTest to
#SpringBootTest(webEnvironment=WebEnvironment.NONE)
From #IntegrationTest with #WebAppConfiguration to
#SpringBootTest(webEnvironment=WebEnvironment.DEFINED_PORT) (or RANDOM_PORT)
From #WebIntegrationTest to
#SpringBootTest(webEnvironment=WebEnvironment.DEFINED_PORT) (or RANDOM_PORT)
Tip Whilst migrating tests you may also want to replace any
#RunWith(SpringJUnit4ClassRunner.class) declarations with Spring 4.3’s
more readable #RunWith(SpringRunner.class).

Resources