3

I need to inherit from my organization's parent pom so I have following setup.

<?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>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-boot.version>2.0.5.RELEASE</spring-boot.version>
    <maven-failsafe-plugin.version>2.19.1</maven-failsafe-plugin.version>
    <maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<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>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring-boot.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>build-info</goal>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven-surefire-plugin.version}</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>${maven-failsafe-plugin.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

My main entrypoint class is in com.example.demo package.

Now I have an integration test in com.example.demo package as follows:

package com.example.demo;

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 DemoApplicationIT {

    @Test
    public void contextLoads() {
    }

}

When I run mvn clean install the integration test is failing with following error:

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.526 sec <<< FAILURE! - in com.example.demo.DemoApplicationIT
initializationError(com.example.demo.DemoApplicationIT)  Time elapsed: 0.008 sec  <<< ERROR!
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

If I remove <goal>repackage</goal> from spring-boot-maven-plugin then it is working fine. But I need to run repackage goal to build fat jar.

Is there anyway to resolve this issue?

K. Siva Prasad Reddy
  • 11,786
  • 12
  • 68
  • 95

3 Answers3

4

Apparently I hit this issue https://github.com/spring-projects/spring-boot/issues/6254

Fixed the issue by adding <classesDirectory> configuration as follows:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>

    <configuration>
          <classesDirectory>${project.build.directory}/${artifactId}.jar.original</classesDirectory>
    </configuration>
</plugin>
K. Siva Prasad Reddy
  • 11,786
  • 12
  • 68
  • 95
  • This worked for me, but it took me longer than I care to admit to realise that my jar was actually called ${artifactId}-${version}.jar.original – Clarkey Sep 04 '20 at 20:51
  • Been looking at this for hours. The changing to the outputDirectory did not work. Finally found this and fixed the issue. Thank you! – Marquee Nov 16 '21 at 18:13
3

If you do not include the spring-boot-starter-parent as a parent in you pom.xml, you will loose (among other things) the following:

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-failsafe-plugin</artifactId>
          <executions>
            <execution>
              <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <classesDirectory>${project.build.outputDirectory}</classesDirectory>
          </configuration>
        </plugin>

You can check this by actually looking into the spring-boot-starter-parent's POM file, e.g. here.

Therefore, in your pom.xml, you need to add the missing <configuration> section to your maven-failsafe-plugin definition.

olafr
  • 76
  • 4
  • You have saved me a day. Thanks! – Rafi May 27 '21 at 21:16
  • This is the most common answer I've seen, but it did not work for me with Spring Boot 2.5.6. I had to use K. Siva's answer (https://stackoverflow.com/a/52502068/631272). – Marquee Nov 16 '21 at 18:23
0

Actally if You create Your own parent and plan to use in a library without spring-boot plugin it will fail.
Add the classes directory instead
http://maven.apache.org/surefire/maven-failsafe-plugin/integration-test-mojo.html#additionalClasspathElements
${project.build.directory}/classes

Péter
  • 2,161
  • 3
  • 21
  • 30
  • I tried this, and it resolved the error from OP but I still had some other issues, which didn't occur when I used @K. Siva's answer. It looked like some dependencies not resolving, but i didn;t dig into it since the other answer worked fine. YMMV! – Clarkey Sep 04 '20 at 20:50