'Spring boot Test with non-Spring Boot Application

I have an application using Spring MVC 4, and not using any spring-boot-starter-* dependencies. i.e, I don't have that class with a main method calling SpringApplication.run. I'm configuring all by myself with xml and other @Configuration classes.

Question: Is it possible to configure Spring Boot Test (using the spring-boot-starter-test) in that non Spring Boot Application?



Solution 1:[1]

spring-boot-starter-test has dependency on spring-boot-test JAR. There are some spring-boot packages used. So if you wouldn't use these features, you may be able to use it as pure test dependency. But better alternative would be to use plain spring-test module. That covers most of the Spring test functionality anyway.

Solution 2:[2]

The testCompile('org.springframework.boot:spring-boot-starter-test') dependency has all you need, but it is dependency that you need to have, it will give you all power from spring-boot side, i.e. if you have your test class annotated with

@RunWith(SpringRunner.class)
@SpringBootTest(
    classes = ${YourMainClass}.class
)

it should work, even though I would strongly recommend using whole power of Spring Boot framework with the usage of all proper dependencies.

Solution 3:[3]

Test class:

@ContextConfiguration(locations = { "classpath:web-sdk-test-config.xml" })
@Test
@TestPropertySource("classpath:application.properties")
public abstract class AbstractTest extends AbstractTestNGSpringContextTests {
    private static final int TIMEOUT = 36000;


}

web-sdk-test-config.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    ">

    <context:component-scan base-package="com.company" />

</beans>

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>
    <artifactId>web-sdk</artifactId>
    <groupId>com.company</groupId>
    <version>2.0.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
    </parent>

    <properties>
        <direct.data.loader.common>2.2.48</direct.data.loader.common>
        <timbermill.version>2.4.1</timbermill.version>
    </properties>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
       
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <version>2.2.5.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.2.5.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.13.6</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>


    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 luboskrnac
Solution 2 AmanicA
Solution 3 Barak Kedem