简体   繁体   English

测试失败:java.lang.IllegalStateException:加载ApplicationContext失败

[英]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.我有一个在 IDE 中正确运行的项目,但是当我尝试使用“mvn”命令将其捆绑到“jar”文件中时,由于“测试失败”而失败: java.lang.IllegalStateException:无法加载 ApplicationContext。

Here is the test class (the default one):这里是测试 class (默认的):

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应用 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 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.提前致谢。

you are having this error because it's now recommended to specify the class (classes) you would like to test:您遇到此错误是因为现在建议指定您要测试的 class(类): 在此处输入图像描述

To fix, you just have to specify the classes like this:要修复,您只需指定如下类:

在此处输入图像描述

Or if you want to ignore the tests, you can simply run:或者如果你想忽略测试,你可以简单地运行:

mvn clean install -Dmaven.test.skip=true mvn clean install -Dmaven.test.skip=true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 测试 java.lang.IllegalStateException: 加载 ApplicationContext 失败 - Test java.lang.IllegalStateException: Failed to load ApplicationContext java.lang.IllegalStateException:无法加载ApplicationContext - java.lang.IllegalStateException: Failed to load ApplicationContext @WebMvcTest 失败,出现 java.lang.IllegalStateException:无法加载 ApplicationContext - @WebMvcTest fails with java.lang.IllegalStateException: Failed to load ApplicationContext SpringBoot + Mybatis + MySQL,java.lang.IllegalStateException:无法加载ApplicationContext - SpringBoot + Mybatis + MySQL, java.lang.IllegalStateException: Failed to load ApplicationContext Junit错误:java.lang.IllegalStateException:无法加载ApplicationContext - Junit error :java.lang.IllegalStateException: Failed to load ApplicationContext Spring JUnit-java.lang.IllegalStateException:无法加载ApplicationContext - Spring JUnit - java.lang.IllegalStateException: Failed to load ApplicationContext java.lang.IllegalStateException:使用HSQL嵌入式数据库运行单元测试时,无法加载ApplicationContext - java.lang.IllegalStateException: Failed to load ApplicationContext when running unit test with HSQL embedded database 运行SpringWS JUnit Test时发生异常:java.lang.IllegalStateException:无法加载ApplicationContext - Exception While Running SpringWS JUnit Test : java.lang.IllegalStateException: Failed to load ApplicationContext 我正在尝试在 springboot 中进行单元测试,但测试抛出 java.lang.IllegalStateException: Failed to load ApplicationContext - I´m trying to make a unit test in springboot but the test throw me java.lang.IllegalStateException: Failed to load ApplicationContext java.lang.IllegalStateException:无法加载 ApplicationContext 错误 java spring-boot - java.lang.IllegalStateException: Failed to load ApplicationContext error java spring-boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM