6

I am running a spring boot application with spring-boot-starter dependencies, am facing a compile errors in my test cases if I don't import the following test dependencies

  • spring-boot-test
  • spring-test
  • assertj-core

My understanding is that these are present already in the spring-boot-starter-parent and I can see them too. However, because of the compile time errors I am forced to import them into pom.xml as below, but then I get warnings that

Duplicating managed version 1.5.6.RELEASE for spring-boot-test

Duplicating managed version 4.3.10.RELEASE for spring-test

and similiarly for assertj-core

You can see the places where the warnings occur in pom.xml here enter image description here

And my pom.xml is as follows

 <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <json.version>20160810</json.version>   
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </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-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jdt.core.compiler</groupId>
        <artifactId>ecj</artifactId>
        <version>4.6.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.hateoas</groupId>
        <artifactId>spring-hateoas</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>
    </dependency> -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-test</artifactId>
        <version>1.5.6.RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.3.10.RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>3.8.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>${json.version}</version>
    </dependency>

Parts of my code where the compile errors occurs if I dont include the test dependencies is below. The @SpringBootTest and the TestRestTemplate cannot be imported if the dependencies are not present.

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import com.fasterxml.jackson.core.JsonProcessingException;

@RunWith(SpringRunner.class) 
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) 
public class MatchControllerTest {

    // Test RestTemplate to invoke the APIs.
    @Autowired
    private TestRestTemplate testRestTemplate;
    //....and other part of the code

Why is this happening ?

  1. Why are the test dependencies having to be manually included in the pom.xml if they are already present in the spring-boot-starter-parent
  2. Once included, it is showing a duplcating managed version warning (probably rightly so...).

I am probably doing something silly/wrong - please help !

HopeKing
  • 3,317
  • 7
  • 39
  • 62
  • may be jars wouldnt have downloaded properly, do mvn clean install or go under maven repository (.m2) folder and see whether there is any problem with the jar downloaded. Simply delete and Right on the project -> Maven ->> update project. – Barath Aug 11 '17 at 06:22

3 Answers3

5

As you can see in the pom:

<dependencyManagement>
        <dependencies>
            <!-- Spring Boot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot</artifactId>
                <version>1.5.6.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot</artifactId>
                <type>test-jar</type>
                <version>1.5.6.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-test</artifactId>
                <version>1.5.6.RELEASE</version>
            </dependency>
 ...

The dependency is under the tag <dependencyManagement>. That means, if you need it in your project you get the version 1.5.6.RELEASE

So you have only to add

  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-test</artifactId>
        <scope>test</scope>
    </dependency>

Without the version number and the warning should go away.

Jens
  • 67,715
  • 15
  • 98
  • 113
  • Perfect. As I suspected my Dependency Management concept was not clear. This may help someone like me. https://stackoverflow.com/questions/2619598/differences-between-dependencymanagement-and-dependencies-in-maven – HopeKing Aug 11 '17 at 06:49
2

Replace all scopes "test" with "compile".

0

in project properties, java build path, check its running on JRE? if it's JRE, make it as JDK. I faced the same issue and able to solve it.

  • Please read [How to answer](https://stackoverflow.com/help/how-to-answer) and update your answer. – fartem Apr 15 '21 at 05:45