Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-boot-test dependencies having to be manually imported inspite of them being present in Spring-boot-starter-parent - Why?

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 !

like image 390
HopeKing Avatar asked Aug 11 '17 05:08

HopeKing


People also ask

What is the Spring Boot starter test dependency?

The spring-boot-starter-test is the primary dependency that contains the majority of elements required for our tests. The H2 DB is our in-memory database. It eliminates the need for configuring and starting an actual database for test purposes.

How to create a simple unit test in Spring Boot?

With Spring Boot, we need to add starter to our project, for testing we only need to add the spring-boot-starter-test dependency. It pulls all the dependencies related to test. After adding it, we can build up a simple unit test. We can either create the Spring Boot project through IDE or generate it using Spring Initializr.

What is @springboottest annotation in Spring Boot?

The above code implements two annotation by default: @SpringBootTest, and @Test. @SpringBootTest: It applies on a Test Class that runs Spring Boot based tests.

What is testentitymanager in Spring Boot?

The Spring Boot TestEntityManager is an alternative to the standard JPA EntityManager that provides methods commonly used when writing tests. EmployeeRepository is the component that we are going to test. Now let's write our first test case:


1 Answers

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.

like image 92
Jens Avatar answered Oct 27 '22 01:10

Jens