简体   繁体   English

Spring 引导“TestPropertySource”不工作

[英]Spring Boot `TestPropertySource` not Working

I have a simple unit test, and I'd like for the unit tests to load up a test property file that I've placed in src/test/resources/test.properties instead of the property file in src/main/resources/application.properties .我有一个简单的单元测试,我希望单元测试加载我放在src/test/resources/test.properties中的测试属性文件,而不是src/main/resources/application.properties中的属性文件src/main/resources/application.properties

My test class is set up as follows:我的测试class设置如下:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@TestPropertySource(locations = "classpath:src/test/resources/properties-test.yml")
public class SecurityConfigurationTest {
    @MockBean
    KafkaConfiguration kafkaConfiguration;

    @MockBean
    KafkaMonitor kafkaMonitor;

    @MockBean
    BuildProperties buildProperties;

    @Autowired
    MockMvc mockMvc;

    @Test
    public void unauthorizedUser_shouldNotAccessHomePage() throws Exception{
        mockMvc
                .perform(get("/"))
                .andExpect(status().isUnauthorized());
    }

}

My project file structure is as shown below:我的工程文件结构如下图: 在此处输入图像描述

But I am getting the following error, suggesting I'm not specifying this property file correctly:但是我收到以下错误,提示我没有正确指定此属性文件:

Caused by: java.io.FileNotFoundException: class path resource [src/test/resources/test.properties] cannot be opened because it does not exist

How do can I manage to get the test.properties file to be used instead of the application.properties file for my unit tests?我怎样才能设法让我的单元测试使用test.properties文件而不是application.properties文件?

In pom.xml add a profiles tag which specifies which properties file should the program choose when it runs.在 pom.xml 中添加一个profiles 标签,指定程序在运行时应该选择哪个属性文件。

 <profiles>
    <profile>
        <id>test</id>
        <properties>
            <activatedProperties>test</activatedProperties>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
</profiles>

Note: name of your properties file must be application-test.properties .注意:您的属性文件的名称必须是application-test.properties

This will make your test properties active but to access it you need to configure your test resource folder and let the intellij know 'use this when running.这将使您的测试属性处于活动状态,但要访问它,您需要配置测试资源文件夹并让 intellij 知道“在运行时使用它”。

In Intellij, you can change your resource folder with which you run your project, so you need to....在 Intellij 中,您可以更改用于运行项目的资源文件夹,因此您需要......

  1. Select test resource folder. Select 测试资源文件夹。
  2. Right click on it.右键单击它。 In the opened menu, select Modify Run Configuration and create a new configuration.在打开的菜单中,select Modify Run Configuration新建一个配置。

This means a new Run Configuration is created for running test resource folder.这意味着为运行测试资源文件夹创建了一个新的运行配置

  1. Run the configuration.运行配置。

Maven by default will package all files in the src/test/resources/ folder to the classpath. Maven 默认会将 package src/test/resources/文件夹下的所有文件放到类路径下。 So to refer it by the classpath, it should be:所以要通过类路径引用它,它应该是:

@TestPropertySource(locations = "classpath:test.properties") 

Alternatively, if you want to refer it by the system file path, you can change it to:或者,如果你想通过系统文件路径引用它,你可以将其更改为:

@TestPropertySource(locations = "file:src/test/resources/test.properties") 

Under the cover, its uses ResourceLoader to load the file.在幕后,它使用ResourceLoader来加载文件。 See this for more details.有关更多详细信息,请参阅内容。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM