jhipster Java 测试失败 – 应用程序属性

huangapple go评论75阅读模式
英文:

jhipster java tests failing - application properties

问题

我已经生成了一个jhipster的单体应用程序。我创建了一个类来连接到AWS S3并将文件上传到那里。我在.yml文件中定义了属性。在这里一切都正常。

当我尝试运行提供的测试时,大多数测试都失败,错误信息如下:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 's3AutoConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'aws.endpoint.url' in value "${aws.endpoint.url}"

S3AutoConfig是使用这些属性的类。
我查看了jhipster的文档和一些帖子,比如下面这个:
https://stackoverflow.com/questions/42871453/adding-applicationproperties-in-jhipster

这些帖子提到你应该在ApplicationProperties类中提供属性(似乎有点多余)。

我也在Java类中定义了这些属性,但测试仍然以上面相同的错误失败。

我应该如何定义这些属性,以便测试能够使用它们?是否有必要像一些帖子建议的那样在Java类中提供它们?

jhipster Java 测试失败 – 应用程序属性

英文:

I have generated a jhipster monolithic app. I have created a class to connect with the AWS S3 and upload a file there. I defined the properties in .yml file. Here everything works fine.

When I am trying to run the provided tests, most of them are failing with the following error:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 's3AutoConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'aws.endpoint.url' in value "${aws.endpoint.url}"

S3AutoConfig is the class which uses the properties.
I checked jhipster's documenation and several posts, like the one below:
https://stackoverflow.com/questions/42871453/adding-applicationproperties-in-jhipster

which mention that you should provide the properties in the ApplicationProperties class (seems a bit redundant).

I defined the properties also in the java class, but the tests are still failing with the same error above.

How should I define the properties, so they are picked up by the tests? Is it necessary to provide them also in the java class as some posts suggest?

jhipster Java 测试失败 – 应用程序属性

答案1

得分: 1

你的实现无法正常工作,因为你在ApplicationProperties内部定义了Aws类,这意味着你的AWS属性将以application为前缀,例如application.aws.endpoint.url,这与你的application*.yml的结构不匹配,这就是你出现这个错误的原因。

你应该将Aws类及其内部类提取到自己的文件中(Aws.java),并使用前缀“aws”。
此外,它的名称可能更适合命名为AwsProperties

@ConfigurationProperties(prefix = "aws", ignoreUnknownFields = false)
public class Aws {
    // ...
}

然后关于测试的第二点是,它们使用的类路径与主类不同,因此你应确保你还在src/test/resources/config/application.yml中定义了这些属性。

英文:

Your implementation cannot work because you are defining Aws class within ApplicationProperties which means that your AWS properties will be prefixed by application, so for example application.aws.endpoint.url which does not match your application*.yml structure and this is why you get this error.

You should extract Aws class and its inner classes to its own file (Aws.java) and use prefix "aws".
Also, it would probably better named as AwsProperties.

@ConfigurationProperties(prefix = "aws", ignoreUnknownFields = false)
public class Aws {

Then the second point about tests is that they are using a different classpath than main class so you should ensure that you define these properties also in src/test/resources/config/application.yml

huangapple
  • 本文由 发表于 2020年10月27日 02:20:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64542855.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定