GVKun编程网logo

在Spring Boot测试中@Value“无法解析占位符”(无法解析springframework)

21

本文将介绍在SpringBoot测试中@Value“无法解析占位符”的详细情况,特别是关于无法解析springframework的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这

本文将介绍在Spring Boot测试中@Value“无法解析占位符”的详细情况,特别是关于无法解析springframework的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于idea 无法识别 SpringBoot @占位符、java – Spring引导无法解析字符串中的占位符、java-我无法通过两步定义在黄瓜Spring Boot测试中使用@Spy对象、Kotlin Spring Boot批注处理“无法解析配置处理”的知识。

本文目录一览:

在Spring Boot测试中@Value“无法解析占位符”(无法解析springframework)

在Spring Boot测试中@Value“无法解析占位符”(无法解析springframework)

我要对Spring-boot进行Junit测试,如下所示:

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = {ApplicationTest.class})public class TestOnSpring {    @Value("${app.name}")    private String appName;    @Test    public void testValue(){        System.out.println(appName);    }}

和ApplicationTest.java这样

@ComponentScan("org.nerve.jiepu")@EnableAutoConfiguration()public class ApplicationTest {    public static void main(String[] args) {        SpringApplication.run(ApplicationTest.class, args);    }}

和我的POM像这样:

<parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.3.0.BUILD-SNAPSHOT</version>    </parent>

运行测试时,出现以下错误信息

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder ''app.name'' in string value "${app.name}"    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172)    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:807)    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1027)    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:543)    ... 31 more

但是当我将此应用程序作为普通Java应用程序运行时

@SpringBootApplicationpublic class Application {    public static void main(String[] args){        SpringApplication.run(Application.class, args);    }}

运行良好!

它出什么问题了 ?我应该如何通过Spring-boot进行junit测试?非常感谢!

答案1

小编典典

您需要添加

@PropertySource(“ classpath:application.properties”)

上课,这样它将选择您的常规配置。

如果您需要其他配置进行测试,则可以添加

@TestPropertySource(locations =“ classpath:test.properties”)

如果不只是复制,将您的配置文件粘贴到 test/resources folder ,那么启动将从那里选择。

idea 无法识别 SpringBoot @占位符

idea 无法识别 SpringBoot @占位符

背景

idea 下启动 SpringBoot 项目

配置文件中使用了 @@占位符获取 maven 中的配置项

idea 启动时报错

''@'' that cannot start any token. (Do not use @ for indentation)

解决方案

pom.xml 中添加如下内容

   <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

plugins 中添加如下内容


 <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <delimiters>
                        <delimiter>@</delimiter>
                    </delimiters>
                    <useDefaultDelimiters>false</useDefaultDelimiters>
                </configuration>
            </plugin>



如果上述方案不奏效,可以尝试执行 mvn spring-boot:run 之后就不会出错了

或者可以直接修改 target/classes/application.yml 文件

参考资料

原来你不是这样的 BUG (1):found character ''@'' that cannot start any token. (Do not use @ for indentation)

java – Spring引导无法解析字符串中的占位符

java – Spring引导无法解析字符串中的占位符

我通过maven与mvn clean install spring-boot:run在嵌入式tomcat服务器上运行spring-boot.但每次我运行它我都会收到此错误:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder ‘language’ in string value “${language}”
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) ~[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236) ~[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.core.env.AbstractPropertyResolver.resolverequiredPlaceholders(AbstractPropertyResolver.java:210) ~[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.Abstractbeanfactory.resolveEmbeddedValue(Abstractbeanfactory.java:831) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListablebeanfactory.doResolveDependency(DefaultListablebeanfactory.java:1086) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListablebeanfactory.resolveDependency(DefaultListablebeanfactory.java:1066) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcesspropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
… 35 common frames omitted

该错误与这两行代码有关:

@Value("${language}")
private String language;

那个语言标志在我的application.properties中指定,如下所示:

application.properties

language=java
logging.level.org.springframework=TRACE

这是一个令人困惑的部分:当我在没有spring-boot:run命令的情况下运行构建时,它构建正确,我可以运行构建的jar而不会出现任何问题.只有当我尝试在嵌入式tomcat服务器上运行时,我才遇到这个问题.

我可以通过在我的代码中执行此操作来绕过这个:

@Value("${language:java}")
private String language;

但这对我来说没有意义,因为spring应该自动从application.properties文件中读取默认值.

编辑:正如人们所指出的那样,在嵌入式tomcat服务器上运行时根本不会读取application.properties.有什么方法可以强迫它读取文件或者它可能没有读取它的原因?部署到外部应用服务器而不是嵌入式应用服务器时,它可以正常工作.

在此先感谢您的帮助.

解决方法

通过在< resources>下将这些行添加到pom来修复部分

<resource>
     <directory>src/main/resources</directory>
     <filtering>true</filtering>
     <includes>
          <include>**/*.properties</include>
     </includes>
</resource>

我不完全理解的是需要这样做.

a)我可以在外部应用服务器上运行它,而无需添加此行,应用程序读取application.properties就好了.

b)我可以在eclipse中将应用程序作为独立的java应用程序运行(即,无需通过maven构建应用程序)并且它读取application.properties就好了

c)不是弹簧启动应该默认读取它吗? (如上面的两个案例所示?)

谢谢大家的帮助.希望这会有助于其他人.

java-我无法通过两步定义在黄瓜Spring Boot测试中使用@Spy对象

java-我无法通过两步定义在黄瓜Spring Boot测试中使用@Spy对象

我在使用Spring Boot 2运行黄瓜测试时遇到问题.

我有两个步骤的定义,在这两个方面中,我都试图用
Mockito试图捕获传递给此类的一个方法的参数.

关键是,由于Cucumber仅允许进行一个Spring Application Context Configuration,所以我创建了一个抽象类来对其进行配置,并为此类扩展了步骤定义之一.

@ActiveProfiles("INTEGRATION_TEST")
@SpringBoottest
@ContextConfiguration(classes = ServiceApplication.class)
@TestPropertySource(properties = 
      {"spring.config.location=classpath:application-test.yml"})
public abstract class BaseTestIntegration {}


@Ignore
public class OfferEventsGenerationStep extends BaseTestIntegration {

@Autowired private LoanOfferBatchController loanOfferBatchController;
@SpyBean private SendEventOfferServiceImpl sendEventService;
@Captor private ArgumentCaptor<CreateOfferToUserEvent> createOfferEventCaptor;
@Autowired private GenericWebApplicationContext context;
.........
 @Then("^events will be created$")
public void eventsWillBeCreated() throws Throwable {
    Mockito.verify(sendEventService,Mockito.times(5)).sendEvent(createOfferEventCaptor.capture());
 }
}


@Ignore
public class SecondEventsGenerationStep  {

@Autowired private LoanOfferBatchController loanOfferBatchController;
@SpyBean private SendEventSencondServiceImpl sendEventService;
@Captor private ArgumentCaptor<CreateOfferToUserEvent> createOfferEventCaptor;
@Autowired private GenericWebApplicationContext context;
.........
 @Then("^events will be created$")
public void eventsWillBeCreated() throws Throwable {
    Mockito.verify(sendEventService,Mockito.times(2)).sendEvent(createOfferEventCaptor.capture());
 }
}

一切正常,除了sendEventService仅在扩展BaseTestIntegration类的类中被识别为间谍bean之外,另一个抛出此异常:

 org.mockito.exceptions.misusing.NotAMockException: 
 Argument passed to verify() is of type SendEventSencondServiceImpl and is not a mock!
最佳答案
当前无法使用@MockBean或@SpyBean,因为cucumber-spring将步骤定义转换为Bean,并且不使用TestContextManager. Support @MockBean in cucumber-spring #1470有一个问题.如果有人愿意接受,那就可以争取.

Kotlin Spring Boot批注处理“无法解析配置处理”

Kotlin Spring Boot批注处理“无法解析配置处理”

您需要将以下内容声明为annoatationProcessor

implementation("org.springframework.boot:spring-boot-configuration-processor:2.3.3.RELEASE")

annotationProcessor("org.springframework.boot:spring-boot-configuration-processor:2.3.3.RELEASE")
,

如果您的 application.yml(或 application.properties)如下所示:

spring:
    datasource:
        username: postgres
        password: postgres
        url: jdbc:postgresql://localhost:5433/company
        driver-class-name: org.postgresql.Driver

然后尝试将每个属性重写为每个属性的全名格式:

spring.datasource.username: postgres
spring.datasource.password: postgres
spring.datasource.url: jdbc:postgresql://localhost:5433/company
spring.datasource.driver-class-name: org.postgresql.Driver

关于在Spring Boot测试中@Value“无法解析占位符”无法解析springframework的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于idea 无法识别 SpringBoot @占位符、java – Spring引导无法解析字符串中的占位符、java-我无法通过两步定义在黄瓜Spring Boot测试中使用@Spy对象、Kotlin Spring Boot批注处理“无法解析配置处理”等相关知识的信息别忘了在本站进行查找喔。

本文标签: