javaspringspring-bootmaventesting

Test Failure : java.lang.IllegalStateException: Failed to load ApplicationContext


I have a project that runs correctly in the IDE, but when I tried bundle it into a "jar" file using the "mvn" command it fails due to "Test Failure": java.lang.IllegalStateException: Failed to load ApplicationContext.

Here is the test class (the default one):

package com.example.businesslogicserver;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class BusinessLogicServerApplicationTests {

    @Test
    void contextLoads() {
    }

}

the application class

package com.example.businesslogicserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SpringBootApplication
public class BusinessLogicServerApplication {
    private static final Logger logger = LoggerFactory.getLogger(BusinessLogicServerApplication.class);

    public static void main(String[] args) {

        SpringApplication.run(BusinessLogicServerApplication.class, args);
        logger.info("Your Spring Boot Application has started successfully...");
    }
}

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 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.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>business-logic-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>business-logic-server</name>
    <description>Project for Handling a list of items</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-api</artifactId>
            <version>0.11.2</version>
        </dependency>

        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-impl</artifactId>
            <version>0.11.2</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-jackson</artifactId>
            <version>0.11.2</version>
            <scope>runtime</scope>
        </dependency>

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

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

        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.properties

server.port=9090
auth.server.base.url=http://localhost:8080
jwt.signing.key=very1secure2secret3key_10264564684846d4fdfhdfsasa1235saa23154asas

spring.jpa.show-sql=true
spring.datasource.url=jdbc:mysql://localhost:3306/itemlist?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.data.rest.basePath=/api

Can anyone help please ? thanks in advance.


Solution

  • you are having this error because it's now recommended to specify the class (classes) you would like to test : enter image description here

    To fix , you just have to specify the classes like this :

    enter image description here

    Or if you want to ignore the tests , you can simply run :

    mvn clean install -Dmaven.test.skip=true