关键词:
【中文标题】如何在 Spring Boot 中将属性注入测试类?【英文标题】:How to get properties injected into test class in Spring Boot? 【发布时间】:2020-02-07 02:32:41 【问题描述】:如何将 application-test.properties 中的属性加载到 Spring Boot 中的测试类中?我做错了什么但无法弄清楚?
我可以从 application.properties 中获取下面给出的配置类的属性
package org.vinodh.testing;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import lombok.Data;
@Configuration
@ConfigurationProperties(prefix = "caching")
@Data
public class CachingConfig
@Value("$caching.name")
private String name;
@Data
public static class CacheSpec
private int minutesToExpire;
private int maximumSize;
private Map<String, CacheSpec> specs;
@Bean
public void test()
System.out.println(name);
System.out.println(specs);
但是当我尝试在测试类中做同样的事情时,我会得到空值,请参见下面的代码。如何让我的测试类打印属性文件中的值?
package org.vinodh.testing;
import java.util.Map;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import lombok.Data;
@RunWith(SpringRunner.class)
@ContextConfiguration
@ConfigurationProperties(prefix = "caching")
@TestPropertySource("/application-test.properties")
@Data
public class CachingConfigTest
@Value("$caching.name")
private String name;
@Data
public static class CacheSpec
private int minutesToExpire;
private int maximumSize;
private Map<String, CacheSpec> specs;
@Test
public void test()
System.out.println(name);
System.out.println(specs);
application.properties
caching.specs.test.minutesToExpire=10
caching.specs.test.maximumSize=10
caching.name=Vinodh
application-test.properties
caching.specs.test.minutesToExpire=10
caching.specs.test.maximumSize=10
caching.name=Vinodh
【问题讨论】:
添加@ActiveProfile("test")
。
属性的拼写应该像caching.specs.test.minutes-to-expire=10
和caching.specs.test.maximum-size=5
【参考方案1】:
试试这个 spring.config.additional-location=classpath:/application-test.properties
【讨论】:
【参考方案2】:其实你不需要在测试类中创建CacheSpec
也不推荐,你可以使用源码包中的原始的。但只需将getter
添加到CachingConfig
中的私人地图specs
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
@Profile("test")
public class CachingConfigTest
@Autowired
private CachingConfig cachingConfig;
@Test
public void test()
System.out.println(cachingConfig.getSpecs());
而且你也不需要@TestPropertySource
,你可以使用@Profile
加载测试配置文件并使用@ActiveProfile
将test
作为该测试类的活动配置文件
【讨论】:
在单元/集成测试阶段向 Spring Boot 属性文件注入/覆盖属性值
】在单元/集成测试阶段向SpringBoot属性文件注入/覆盖属性值【英文标题】:Inject/overrideapropertiesvaluetoaSpringBootpropertiesfileinunit/integrationtestsphase【发布时间】:2018-11-2609:42:28【问题描述】:使用SpringBoot和Testcontainers我需要一种方法... 查看详情
使用注入 java 和 spring boot 的 RestTemplate 类进行单元测试
】使用注入java和springboot的RestTemplate类进行单元测试【英文标题】:UnittestusingclasswithRestTemplateinjectedwithjavaandspringboot【发布时间】:2019-11-2110:40:29【问题描述】:我正在使用注入了RestTemplate的类运行单元测试,并且只有在我运行... 查看详情
如何在 Wildfly 中将外部属性文件加载到 Spring Boot
】如何在Wildfly中将外部属性文件加载到SpringBoot【英文标题】:HowtoloadanexternalpropertyfiletoSpringBootinWildfly【发布时间】:2015-09-2918:26:53【问题描述】:我想知道如何将在Wildfly9中运行的应用程序的外部属性加载为WAR,我尝试将java参... 查看详情
如何在 Spring Boot 中将属性文件值读入字符串集
】如何在SpringBoot中将属性文件值读入字符串集【英文标题】:HowtoreadpropertyfilevaluesintosetofStringsinSpringboot【发布时间】:2017-03-0509:12:45【问题描述】:我尝试过使用@Value("#\'$white.listed.hotel.ids\'.split(\',\')")privateSet<String>fareAlertw... 查看详情
如何在 Spring Boot 类之间正确注入 @Autowired?
】如何在SpringBoot类之间正确注入@Autowired?【英文标题】:Howtoproperlyinject@AutowiredbetweenSpringBootclasses?【发布时间】:2021-10-0705:34:13【问题描述】:我有一个实现interfaceA的classA,带有一个methodA,然后我有一个classB,我在其中调用... 查看详情
如何在 Spring Boot 中创建一个调用包含构造函数注入的服务的测试?
】如何在SpringBoot中创建一个调用包含构造函数注入的服务的测试?【英文标题】:HowtocreateatestinSpringtBootthatcallsaservicewhichincludesconstructorinjection?【发布时间】:2019-12-2712:37:50【问题描述】:我正在尝试在Spring-Boot项目中编写测试... 查看详情
多部分表单数据输入 Java 模型属性未在请求类中注入元素 - Spring Boot
...多部分表单数据输入Java模型属性未在请求类中注入元素-SpringBoot【英文标题】:MultipartFormDataInputJavaModelAttributeisnotinjectingtheelement\'sinRequestClass-SpringBoot【发布时间】:2022-01-2310:27:33【问题描述】:我正在尝试上传文件以及一些表... 查看详情
如何在spring boot中的每个测试类之后销毁和重新创建bean
】如何在springboot中的每个测试类之后销毁和重新创建bean【英文标题】:Howtodestroyandrecreatebeanaftereachtestclassinspringboot【发布时间】:2017-04-1920:45:56【问题描述】:我有一个应用程序,我创建beanA来安排配置中定义的任务,但在测... 查看详情
如何在 Spring Boot 中测试配置类
】如何在SpringBoot中测试配置类【英文标题】:HowtotestaconfigurationclassinSpringBoot【发布时间】:2021-05-2207:45:19【问题描述】:我有以下SpringBoot配置类,以便在我们的MongoDB中创建索引:@Configuration@DependsOn("mongoTemplate")@Profile("!test")publ... 查看详情
如何在 Spring Boot 应用程序中将 java lang long 转换为实体类
】如何在SpringBoot应用程序中将javalanglong转换为实体类【英文标题】:HowtocastjavalanglongtoentityclassinSpringBootapplication【发布时间】:2021-12-1407:00:55【问题描述】:我创建了一个应用程序,当我去更新我的数据库中的一列时,我得到... 查看详情
由于注入持久性依赖项失败,无法在 Spring Boot 应用程序上运行测试
】由于注入持久性依赖项失败,无法在SpringBoot应用程序上运行测试【英文标题】:Can\'truntestonspringbootapplicationbecauseofinjectionofpersistencedependenciesfailed【发布时间】:2019-07-1120:33:13【问题描述】:我有我的javaspringboot应用程序,我... 查看详情
spring-boot 属性占位符
】spring-boot属性占位符【英文标题】:spring-bootpropertyplaceholder【发布时间】:2014-07-1622:22:03【问题描述】:我无法弄清楚为什么我无法在spring-boot中将值注入到我的application.properties文件中。外部属性到logging.file变量中。我有一个... 查看详情
在 Spring Boot 中将 jsessonid cookie 设置为 SameSite=Strict 属性?
】在SpringBoot中将jsessonidcookie设置为SameSite=Strict属性?【英文标题】:SettingjsessonidcookietoSameSite=Strictattributeinspringboot?【发布时间】:2019-04-0207:19:23【问题描述】:将jsessionIdcookie设置为SameSite=Strict的spring-boot配置是什么。JsessionId需... 查看详情
spring boot 测试无法注入 TestRestTemplate 和 MockMvc
】springboot测试无法注入TestRestTemplate和MockMvc【英文标题】:springboottestunabletoinjectTestRestTemplateandMockMvc【发布时间】:2017-01-0521:56:56【问题描述】:我正在使用弹簧靴1.4.0.RELEASE。我正在为我的控制器类编写测试。我得到以下异常... 查看详情
为啥我不能将此 Spring Boot 服务类注入 JUnit 测试类?预计至少有 1 个 bean 有资格作为 autowire 候选者
】为啥我不能将此SpringBoot服务类注入JUnit测试类?预计至少有1个bean有资格作为autowire候选者【英文标题】:WhyIcan\'tinjectthisSpringBootserviceclassintoaJUnittestclass?expectedatleast1beanwhichqualifiesasautowirecandidate为什么我不能将此SpringBoot服务类... 查看详情
如何在 Junit5 / spring boot 中命令执行控制器测试类?
】如何在Junit5/springboot中命令执行控制器测试类?【英文标题】:HowtoorderexecutionofcontrollertestclassesinJunit5/springboot?【发布时间】:2021-05-0806:43:54【问题描述】:在我的实体中,我有双向的一对多关系,我想用mock.mvc编写集成测试。... 查看详情
在spring boot中从属性文件中注入值数组
】在springboot中从属性文件中注入值数组【英文标题】:Injectingarrayofvaluesfrompropertiesfileinspringboot【发布时间】:2018-10-1505:17:33【问题描述】:好的,所以我有一个config.properties..:market.curpairs[0].name=EuroDollarmarket.curpairs[0].symbol=EURUSD... 查看详情
如何在用 Kotlin 编写的 JUnit 5 测试类中注入 Spring bean?
】如何在用Kotlin编写的JUnit5测试类中注入Springbean?【英文标题】:HowtoinjectaSpringbeaninaJUnit5testclasswritteninKotlin?【发布时间】:2019-06-0412:32:51【问题描述】:我尝试使用JUnit5和SpringBoot在Kotlin项目中测试某些内容,但我无法在我的... 查看详情