Failed to parse configuration class, nested exception is java.io.FileNotFoundException: Could not open resource [/test.properties]

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

Failed to parse configuration class, nested exception is java.io.FileNotFoundException: Could not open resource [/test.properties]

问题

我正在尝试为测试配置新属性,因此我创建了一个测试配置类:

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.scheduling.annotation.EnableAsync;

@Configuration
@EnableAsync
@ComponentScan("ar.com.yo")
@PropertySource("test.properties")
public class TestConfig {

}

属性文件位于 src/test/resources/test.properties

在测试类中:

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = TestConfig.class)
public class InsumoServiceTest {
...
}

当我执行测试时,出现以下错误:
无法解析配置类 [ar.com.yo.myproject.main.TestConfig];嵌套异常为 java.io.FileNotFoundException: 无法打开 ServletContext 资源 [/test.properties]

英文:

I´m trying to configurate new properties for the test, so I created a test config class:

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.scheduling.annotation.EnableAsync;
@Configuration
@EnableAsync
@ComponentScan("ar.com.yo")
@PropertySource("test.properties")

public class TestConfig {

}

Properties file is in src/test/resources/test.properties

and in the test class :

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = TestConfig.class)
public class InsumoServiceTest {
...
}

when I execute the tests the error is:
Failed to parse configuration class [ar.com.yo.myproject.main.TestConfig]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/test.properties]

答案1

得分: 1

似乎找不到所请求的属性。我建议这样做:
解决你的问题的方法是:

@PropertySource("classpath:test.properties")
英文:

It seems that the requested properties can't be found. I would recommend doing this:
is that resolve your problem :

@PropertySource("classpath:test.properties")

huangapple
  • 本文由 发表于 2020年9月25日 22:31:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64066130.html
匿名

发表评论

匿名网友

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

确定