I am using Spring Boot 2.7.2 to create a rest API that communicate with Filenet Content Engine using Filenet APIs and my pom file as follows :
<?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.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>storage</artifactId>
<version>0.0.1</version>
<name>storage</name>
<description>Storage API</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.9</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Filenet Jars -->
<dependency>
<groupId>Jace</groupId>
<artifactId>Jace</artifactId>
<version>5.2.1</version>
</dependency>
<dependency>
<groupId>stax-api</groupId>
<artifactId>stax-api</artifactId>
<version>5.2.1</version>
</dependency>
<dependency>
<groupId>xlxpScanner</groupId>
<artifactId>xlxpScanner</artifactId>
<version>5.2.1</version>
</dependency>
<dependency>
<groupId>xlxpScannerUtils</groupId>
<artifactId>xlxpScannerUtils</artifactId>
<version>5.2.1</version>
</dependency>
<!-- End Of Filenet Jars -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<!--
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>MCI-maven</id>
<url>http://myhost/repository/maven-public/</url>
</repository>
</repositories>
</project>
The project compiles and starts up fine, but when trying to execute any filenet API I get a log4j error:
ClassNotFoundException: org.apache.log4j.Priority
That is because filenet APIs is using log4j1 classes.
The class is : com.filenet.apiimpl.util.BaseLogger.
The class imports are:
import com.filenet.api.exception.EngineRuntimeException;
import com.filenet.api.exception.ErrorLoggingLevel;
import com.filenet.api.util.UserContext;
import com.filenet.apiimpl.exception.ExceptionContext;
import java.util.Enumeration;
import org.apache.log4j.Category;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
The current solution is to add log4j 1.2.17 in pom file
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
When adding this the runtime exception disappears and everything works fine. But I only get the following warning on first time invoking filenet API:
log4j:WARN No appenders could be found for logger (filenet_error.api.com.filenet.apiimpl.util.ConfigValueLookup).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[Perf Log] perflog.dir=null not found, auditor disabled
[Perf Log] No interval found. Auditor disabled.
Is there any better solution to this scenario rather than adding old log4j dependency ?
According to your actual pom.xml file, following the transitive dependencies spring-boot-starter-web and spring-boot-starter, you will end using spring-boot-starter-logging and its different dependent libraries, mainly based on logback and slf4j.
As you indicated, the Filenet APIs use Log4j 1.x.
The support for that version of Log4j was removed with the release of Spring Boot 2.x.
As a consequence, by default, the library will not give you support for that logging library.
That is the reason why you are facing the ClassNotFoundException: org.apache.log4j.Priority error.
In my opinion the solution you applied based on the inclusion of the log4j dependency itself is not a bad solution. A word of caution though:
the library has different vulnerabilities.
it was declared end-of-life in August of 2015.
the solution has the drawback of requiring a different configuration for the library and probably different output artifacts in which the log traces should be written.
As an alternative, you could use the Log4j 1.x bridge provided by Slf4j:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</dependency>
You can use the version 1.7.36 although it is managed by Spring Boot Starter Parent as well, so you don't need to provide a specific version for it.
Maybe there can be some caveats, some functionally used by the Filenet logger not implemented, but at first glance it seems to provide all the required stuff used by com.filenet.apiimpl.util.BaseLogger.
If that's the case, it will give you a better solution than using the log4j library, for the reasons explained, especially because it will allow you to integrate the Filenet loggers with your existing Spring Boot logging configuration and infrastructure.
Adding the following dependencies fixed my problem:
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-logging', version: '2.7.1'
implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.17.1'
implementation group: 'org.slf4j', name: 'log4j-over-slf4j', version: '1.7.36'
Related
I want to customize springboot 2.6.8 elasticsearch dependency to elasticsearch 7.17.2, is this ok?
Based on springboot dependency versions, 2.6.8 is mapped to elasticsearch version 7.15.2.
But I want to changed it since elasticsearch version 7.15.2 has some deprecated functions.
How to do it?
the pom.xml I am using does not have spring-boot-starter-data-elasticsearch
and if I use it Maven cannot find it in:
<repositories>
<repository>
<id>shibboleth_repository</id>
<name>Shibboleth Maven Repository</name>
<url>https://build.shibboleth.net/nexus/content/repositories/releases/</url>
</repository>
</repositories>
<properties>
<java.version>17</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.8</version>
</parent>
<dependencies>
:
:
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
:
</dependencies>
I want to customize springboot 2.6.8 elasticsearch dependency to elasticsearch 7.17.2, is this ok?
One never knows until they try. The ElasticSearch version 7.17.2 contains backward-compatible changes 7.15.2, hence it should be ok.
How to do it?
Check the dependency trees:
spring-boot-starter-parent:2.6.8
spring-boot-starter-data-elasticsearch:2.6.8
spring-data-elasticsearch:4.3.4
org.elasticsearch.client:...:7.15.2
... and ...
spring-boot-starter-parent:2.7.0
spring-boot-starter-data-elasticsearch:2.7.7
spring-data-elasticsearch:4.4.0
org.elasticsearch.client:...:7.17.3
You might want either to update Spring Boot to 2.7.0 for full compatibility that imports transitive dependencies of ElasticSearch version 7.17.3 or override the versions: Exclude all the org.elasticsearch.client dependencies and import them as separate dependencies.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<exclusions>
<!-- repeat for all ElasticSearch dependencies -->
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>...</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- repeat for all ElasticSearch dependencies -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>...</artifactId>
<version>7.17.2</version>
</dependency>
I'm trying to upgrade an application to Java 11.0.2, from Java 8. So those are my very first steps with Jigsaw modules!
My application uses Guice, and the Assistedinject, and Throwingproviders extensions.
Here's my current module-info.java:
`
module com.example.mymodule {
requires com.google.guice;
requires com.google.guice.extensions.assistedinject;
requires com.google.guice.extensions.throwingproviders;
//...
}
`
The application is based on Maven and when I run mvn package I get no error. But In Eclipse (2018-12), I have this error "`The package com.google.inject is accessible from more than one module":
I tried commenting each of the required module in module-info.java but I clearly need the three of them.
Is there something I can do to remove this error? Or is this an Eclipse bug?
Here's 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>example</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.7</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.7</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.26</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-throwingproviders</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-assistedinject</artifactId>
<version>4.2.2</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
</plugins>
</build>
</project>
Here is a minimal project to reproduce my error.
And here's a video of the issue (to be watched in 1080P for clarity!).
Eclipse uses its own compiler which is even stricter than javac in following the specs. You are requiring different Modules/Jars in your module-info which are all using the package com.google.inject. This is some kind of a split package situation which is not allowed in the JPMS spec. AFAIK javac only yields an error if there are actual classes in the same packages of different moduls, but the eclipse compiler is even more picky here.
There are some solutions out there for solving split package problems. If you don't find a newer version of the libs where the problem is solved (which unfortunately is not very likely for most dependencies today) you could f.e. merge the modules into one custom modules, but this is not a perfect solution of course.
For more background information on the issue see also
Eclipse can't find XML related classes after switching build path to JDK 10 and https://bugs.openjdk.java.net/browse/JDK-8215739
I can reproduce the error on my machine (same Eclipse version, OpenJDK 11), works fine on Apache NetBeans IDE 10.0.
Seems to be a bug in Eclipse, you should file it here. You already have a minimal project that reproduces the error, that helps a lot.
Your test project already works in RC2 of 4.11 (which will be released on March 20, 2019)
You can download the release candidate at https://download.eclipse.org/eclipse/downloads/drops4/S-4.11RC2-201903070500/
Better way to deal with this kind of problem by just go to dependencies hierarchy in pom.xml, where you can find duplicate jar, sometimes it could be difficult to find duplicate jar as jar itself have pom dependencies and "Dependencies hierarchy" will help you to deal with maven problem
`
I am making a Maven project it is my first time with Maven. I'm using Netbeans and Tomcat server and I am not able to import any javax.servlet e.g. import javax.servlet.RequestDispatcher; etc. It looks like that:
There is info: javax.servlet does not exist and a solution proposed by Netbeans is for example: "Search Dependency at Maven Repository for javax.servlet.RequestDispatcher. When I click it then there is a pop-up window without anything to do:
I have the pom.xml file located in C://pathToNetbeansProjects/myProject/pom.xml
and I added a dependency for javax-servlet now my pom.xml looks like this:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.glassfish.jersey.archetypes</groupId>
<artifactId>ParkingSystem</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>ParkingSystem</name>
<build>
<finalName>ParkingSystem</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<!-- uncomment this to get JSON support -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<jersey.version>2.27</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
I have no more ideas if I am doing something wrong with my pom.xml or maybe I need to do something in Netbeans to make it work. But I don't know really what.
The problem is 99% caused by a different import done by Maven on that library.
Maven imports your libs following a hierarchical manner, so probably there's some lib that you have imported that contains the javax.servlet, but it's not the version that you need.
First I suggest you to looking for which one is doing that for resolving the conflict by looking into maven hierarchy, you can achieve this with console command mvn dependency:tree -Dverbose ( look here for an example).
Then you can omit the unwanted libraries by a specific maven command inside your library:
<dependency>
....
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
This is an explanation useful for understanding "why" is happening this, so you can understand it.
Btw a quick fix, that you can try as first instance, is moving the import you wanted
javax.servlet as first element of your pom.
Force update your maven project or run
mvn clean install
on your project's directory to download all dependency of pom.xml. Build your project then and javax-servlet will be available.
Using Intellij IDEA (version 2017.2.1) I have a Java/Maven project in which I want to include slf4j with the slf4j-binding.
I know that StackOverflow as an abundance of questions about slf4j and its missing binding, but most refer to Eclipse. My problem however occurs under Intellij.
In the pom.xml I list under <dependencies>:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
Now when I let Intellij build an artifact JAR and I then run the JAR from the command line I get the (dreaded) error:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
Upon inspection the JARdoes indeed not contain slf4j-simple classes (slf4j classes are present though). How can I fix this and instruct Intellij to incorporate slf4j-simple as listed ion the pom.xml?
For completeness, here full project pom.xml Intellij is using:
<?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>au.gov.acic.travelalert</groupId>
<artifactId>extract-data</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency> <dependency>
<!-- jsoup HTML parser library # https://jsoup.org/ -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.3</version>
</dependency>
</dependencies>
</project>
Final note: At the beginning when I set up the project logging didn't even work inside the IDE, despite the correct entries in the pom.xml. But somehow, after additionally adding (and subsequent removing) of a dependency entry for slf4j-log4j12, the IDE picks the slf4j-simple up, but still does not bundle it when writing out the artefact JAR...
execute mvn dependency:tree and check:
slf4j-api and slf4j-simple must be present there.
No other slf4j binding library shoud be present
slf4j bridges can be present (those that end with "-slf4j" suffix on library name; for example, log4j-over-slf4j).
ensure all slf4j library versions match
ensure no duplicate slf4j libraries with different version.
If all of this is ok, no reason for slf4j-api present on artifact but no slf4j-simple. What are you doing to package jar?
The problem is that IntelliJ's XML configuration for the JAR artifact does not contain the slf4j-simple library.
This explains that IntelliJ finds the lib ray (downloaded according by maven according to the `POM/XML~) and uses it for internal execution of the project, but it does not pack it into the JAR.
The solution is to add the libarary to the XML config, either
manually, by editing the XML file. (You find it under [projectroot].idea/artifacts/[projectname_jar.xml)
or
via IntelliJ's GUI, by opening the Artifacts dialog (File --> Project structure --> Artifacts) and then adding the Libra to the Output Layout list.
you can try this solution; go to home directory and delete the .m2 directory(hidden) the update the maven project and try to rebuild. and also check your eclipse version if you are using (Indigo, Juno and Kepler ) version of eclipse the upgrade with latest version or stil if you want to work in it then you can refer below like; this stackoverflow link having most sutable answer on this issue:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". error Question by Konstantinos Margaritis and answer by many java experts.
I'm a newbie at Spring and this is also my very first question on StackOverflow so I'm going to try to make this as understandable as possible.
I'm trying to make a web service client using Spring and Maven on this tutorial: and I get this error: The import org.springframework.test.context.junit4 cannot be resolved
Here is my code:
package demo;
import hello.WsClientApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; //this won't import
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = WsClientApplication.class)
public class WsClientApplicationTests {
#Test
public void contextLoads() {
}
}
Here is my pom.xml in case you need it.
<?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>org.springframework</groupId>
<artifactId>gs-consuming-web-service</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- tag::wsdl[] -->
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.3</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaLanguage>WSDL</schemaLanguage>
<generatePackage>hello.wsdl</generatePackage>
<schemas>
<schema>
<url>http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl</url>
</schema>
</schemas>
</configuration>
</plugin>
<!-- end::wsdl[] -->
</plugins>
</build>
</project>
I have tried some other solutions in StackOverflow but I can't get it to work.
Thanks.
You need to add a dependency on spring-boot-starter-test:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
Now, if you are using latest spring-boot, 1.5.2 RELEASE, #SpringApplicationConfiguration is no longer available, instead you must use #SpringBootTest. Reference here(#spring boot starter test in Spring Boot)
Gradle
In gradle this would be done by adding
testCompile("org.springframework.boot:spring-boot-starter-test")
to the dependencies block as in:
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("junit:junit")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
After that it should be possible to write an import statement at the top of your class
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Allowing you to use the annotation:
#RunWith(SpringJUnit4ClassRunner.class)
public class MyAppTest {
}
I know this is answered kind of late, but I had the same problem and I found I was able to fix it 2 ways.
I was able to remove the < scope > test < / scope> tags and that removed the restriction
I had an issue with intellij not properly handling it when doing tests, so I made sure it was marked as test-root, I changed it from green -> gray -> back to the green test-root and that resolved my issue.
Just to improve in the situation.
I managed to fix it like this:
before, spring boot initializr created a dependency to spring-boot-starter-test but with the exclusion, like this:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
Eclipse accused the annotation #Runwith to be Junit 4, therefore, the logic thing to do was to remove the exclusion.
Final POM dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
sometimes maven start downloading dependencies and doesn't finish.
Go to your .m2 folder and type:
find . -name *progre*
this command will show you every file with the tag "in-progess", which are the files missing or not finished.
delete the folder and try to update the dependencies again.
In case, you are not using the spring boot, but only spring framework you should add dependency for spring-test.
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>2.5</version>
<scope>test</scope>
</dependency>
You can check the latest version for spring-test using mvn, and update accordingly.
If you are using spring-boot project than add spring boot dependency, if you are not using spring-boot the below will work, but not recommended.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>2.4.1</version>
</dependency>
NOTE: If you are using spring-boot project, adding <version> will not be necessary.