简体   繁体   English

在 JUnit 测试类中使用属性值而不加载整个 Spring 引导应用程序上下文

[英]Using property values in JUnit test classes without load whole Spring Boot application context

I have a Spring Boot Component as below我有一个 Spring 引导组件,如下所示

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class Client {

    @Value("${SecretKey}") private String secretKey;
    
    public void createAccount() {
        log.error("secretKey" + secretKey);
    }

}

How do I write a test class where Client class automatically use the resource/application.yaml in the test classpath, without loading the whole application context?如何编写测试 class 客户端 class 自动使用资源/application.yaml 在测试类路径中,而不加载整个应用程序上下文? I want to use like below:我想像下面这样使用:

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@Slf4j
@ExtendWith(SpringExtension.class)
public class ClientTest {

    @Autowired
    private Client client;

    @DisplayName("Create Client")
    @Test
    public void givenX_thenCreateAccount() throws Exception {
       client.createAccount();
    }

}

The test class above throws:上面的测试 class 抛出:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.nerdility.payment.stripe.ClientTest': Unsatisfied dependency expressed through field 'client'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'Client' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

I don't want to load the whole application context just for secretKey thing because it is expensive.我不想仅仅为 secretKey 加载整个应用程序上下文,因为它很昂贵。

I also don't want to create a constructor and manually give the key myself.我也不想创建一个构造函数并自己手动提供密钥。

Main reason for this exception is without loading your class Client in springContext, you are trying to read it.此异常的主要原因是没有在 springContext 中加载您的 class 客户端,您正在尝试阅读它。 So you are getting NoSuchBeanDefinitionException所以你得到NoSuchBeanDefinitionException

You need to do 2 things.你需要做两件事。

  1. Load your class in context with class annotation @ContextConfiguration(classes = Client.class)使用 class 注释@ContextConfiguration(classes = Client.class)在上下文中加载 class
  2. Explicitly load you application.yaml from src folder as you are not having it in test/resource folder as @TestPropertySource(locations = "/application.yaml")从 src 文件夹中显式加载application.yaml因为你没有在 test/resource 文件夹中作为@TestPropertySource(locations = "/application.yaml")

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

相关问题 如何使用 JUnit 在没有 @SpringBootApplication 的情况下测试 Java Spring Boot 应用程序? - How to test Java Spring Boot application without @SpringBootApplication using JUnit? 测试 Spring boot 使用 Junit 启动应用程序 - Test Spring boot Application startup using Junit 用于单元测试的spring-boot junit负载测试属性资源 - spring-boot junit load test property resource for unit test Spring boot junit 测试加载应用程序上下文失败 - Spring boot junit test failed loading application context 使用 Junit 5 的 Spring Boot Cucumber 测试不会加载 application-test.properties - Spring Boot Cucumber Test with Junit 5 will not load application-test.properties Spring 引导 - 没有 Spring 上下文的测试实用程序类 - Spring Boot - Test utility classes without Spring context 如何更正 Spring 引导单元测试无法在两个几乎相同的测试类之一中加载应用程序上下文? - How can I correct Spring Boot unit test failed to load application context in one of two nearly identical test classes? 使用 junit 测试将命令行参数传递给 Spring Boot 应用程序 - Using junit test to pass command line argument to Spring Boot application 如何使用 JDBC 在 spring 引导测试类上创建事务上下文? - How to create a transactional context on spring boot test classes using JDBC? Spring JUnit 测试未加载完整的应用程序上下文 - Spring JUnit Test not loading full Application Context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM