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

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).

Related

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

Spring boot 2.1.7 having tomcat-embed-core conflit

I am migrating existing Spring project into Spring boot.unable to run spring boot application its showing following error.
The error log says there is a conflict on tomcat-embed-core.
In eclipse Dependency hierarchy of porm.xml is given below
i exclude the maven architect ,and try to run the application its showing following error
porm.xml
<modelVersion>4.0.0</modelVersion>
<artifactId>MyService</artifactId>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
<!-- 2.1.3.RELEASE -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springframework.boot.version>2.1.7.RELEASE</springframework.boot.version>
</properties>
<name>MyService</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.sybase.jdbc3.jdbc</groupId>
<artifactId>jconn3</artifactId>
<version>${jconn3.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-juli</artifactId>
<version>${tomcat.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>${tomcat.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
what was wrong in this porm.xml
Where is
${tomcat.version}
defined?
That version probably does not match the tomcat version that auto magically is included with spring boot items.
And thus the conflict.
Go here:
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web/2.1.7.RELEASE
And start following the COMPILE dependencies, and you'll find the versions that are auto included with 2.1.7.RELEASE. and you have to alter the other includes that are overwriting the springboot auto include tomcat versions.
Again, follow the COMPILED dependency trail.
So below is what you should find by crawling the COMPILED dependency trail (from immediately above in my answer)
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat/2.1.7.RELEASE
And you'll find you need to set
tomcat.version to
9.0.22
By defining tomcat.version as 8.x, you are breaking it.
Another way to put it
You have to go ~way~ back to springboot 1.5.2.RELEASE or 1.5.3.RELEASE
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat/1.5.2.RELEASE
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat/1.5.3.RELEASE
(Again, in the two above links, looked at the COMPILE dependencies)
To find a version of tomcat (that is auto included with springboot) that gets close to tomcat 8.5.x (where 8.5.x is the one you are attempting to use)
That's pretty old.
The principal you are missing is that springboot auto includes dependencies. And anything else you import has to play nice with everything springboot auto includes.
And your current value for tomcat.version is NOT playing nice with everything springboot 2.1.7.RELEASE is auto including.
And now that you've been through all of that. You'll find you'll make your life easier if you engage the springboot world more completely.
Alot of times, springboot will have a (sub)package that will bring in the thing you really desire.
spring-boot-starter-jdbc
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc/2.1.7.RELEASE
You would probably be better off bringing that package in, vs hand-picking ones. Aka, get rid of your "tomcat-jdbc" include and see if the spring-boot-starter-jdbc can give you what you want.
The curse/blessing of spring-boot is that it is its own universe. But if you engage, you probably want to play by its rules more often than not.
PS
It is pom.xml, not porm.xml
Try adding spring-boot-starter-tomcat as a dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Remove tomcat-juli and tomcat-jdbc dependencies. If you need JDBC support, add the corresponding starter:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
If you use JSP views, you will probably need the following dependencies as well:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
Also, pay attention to your dependencies versions. Spring Boot's parent POM defines version management for many common artifacts so you don't need to set the <version></version> for these libraries. See https://docs.spring.io/spring-boot/docs/2.1.7.RELEASE/reference/htmlsingle/#appendix-dependency-versions

common-data maven project needs to be included in two spring boot application

i have three projects
common-data : separate maven java project
application-1 : spring boot app
application-2: spring boot app
i want to include that common data project in both application-and application-2 as dependency.
but when i am adding that dependency in application-1 spring boot project is started but not running .
http://localhost:8090/hello this is end point: when included dependency of common data in any of application i am getting 404 found error.
<dependencies>
<!-- Add typical dependencies for a web application -->
<!-- Adds Tomcat and Spring MVC, along others -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.stocksrin</groupId>
<artifactId>stocksrin-common</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
now this is not working
You can do this way:
Step 1: Edit dependency org.stocksrin:
<dependency>
<groupId>org.stocksrin</groupId>
<artifactId>stocksrin-common</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/stocksrin-common.jar</systemPath>
</dependency>
${basedir} : PROJECT ROOT FOLDER
Step 2: you can set 'includeSystemScope' to true:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>

ClassNotFoundException on Spring JavaMailSenderImpl in JUnit test with Spring Boot

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.

spring boot app initialization failure

Adding the spring boot dependencies in pom.xml and running spring boot app causes following errors -
> xception in thread "main" java.lang.IllegalAccessError: tried to
> access method
> org.springframework.core.io.support.SpringFactoriesLoader.loadFactoryNames(Ljava/lang/Class;Ljava/lang/ClassLoader;)Ljava/util/List;
> from class org.springframework.boot.SpringApplication at
> org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:392)
> at
> org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:384)
> at
> org.springframework.boot.SpringApplication.initialize(SpringApplication.java:260)
> at
> org.springframework.boot.SpringApplication.<init>(SpringApplication.java:236)
> at
> org.springframework.boot.SpringApplication.run(SpringApplication.java:1185)
> at
> org.springframework.boot.SpringApplication.run(SpringApplication.java:1174)
> at com.catalina.platform.batch.Application.main(Application.java:12)
See below the snap shot of my pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.3.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-ftp</artifactId>
<version>${spring.ftp.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Usually IllegalAccessError problems are due to an incorrect version of Spring being pulled in by Maven. Spring Boot 1.3.6 needs Spring Framework 4.2.7.
The spring-boot-dependencies dependencyManagement import is normally enough to ensure the correct version is used and the limited section of the POM that you've posted looks correct.
I would suggest running mvn dependency:tree to check that the correct version is being pulled in. Also check that you're on the latest Maven release.
Failing that, a project that reproduces the issue would help a lot with tracking down the cause.
I too had the same problem when i created a Spring Simple Web Maven Project and added few other dependencies in my pom.xml to write a web service for spring boot application
Updating my dependencies so that the version mismatch is handled and then doing Run As-->Maven clean , Run As --> Maven Install followed by Maven--Update Project worked in my case

Resources