java 在 spring-boot 测试中使用属性文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39825525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:40:12  来源:igfitidea点击:

Using properties files in spring-boot tests

javaspringtestingspring-boot

提问by Bublik

I have simple spring boot web service, where for configuration I use .propertiesfiles. As example for spring-mail configuration, I have separate file mailing.propertieslocated in src/main/resources/config/folder.

我有简单的 Spring Boot Web 服务,我在其中使用.properties文件进行配置。作为 spring-mail 配置的示例,我mailing.propertiessrc/main/resources/config/文件夹中有单独的文件。

in main application I include it using:

在主应用程序中,我使用以下方法将其包含在内:

@PropertySource(value = { "config/mailing.properties" })

The problem appears when it comes to tests, I would like to use the same properties from this file, but when i try to use it, I get fileNotFaundExeption.

测试时出现问题,我想使用该文件中的相同属性,但是当我尝试使用它时,我得到fileNotFaundExeption.

Question is:

问题是:

  • Should I have separate resources in my src/testfolder, or it is possible to access resources from src/mainfolder, if yes, how?
  • 我的src/test文件夹中应该有单独的资源,还是可以从src/main文件夹访问资源,如果是,如何访问?

UPDATE added sources

更新添加的来源

test class:

测试类:

    @RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource("classpath:config/mailing.properties")
public class DemoApplicationTests {

    @Autowired
    private TestService testService;

    @Test
    public void contextLoads() {
        testService.printing();
    }

}

service class:

服务等级:

    @Service
public class TestService
{
    @Value("${str.pt}")
    private int pt;

    public void printing()
    {
        System.out.println(pt);
    }
}

main app class:

主要应用程序类:

@SpringBootApplication
@PropertySource(value = { "config/mailing.properties" })
public class DemoApplication {

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

structure

结构体

回答by Patrick

You can use @TestPropertySourceannotation in your test class.

您可以@TestPropertySource在测试类中使用注释。

For example you have this attribute in your mailing.propertiesfile:

例如,您的mailing.properties文件中有此属性:

[email protected]

[email protected]

Just annotate @TestPropertySource("classpath:config/mailing.properties")on your test class.

只需@TestPropertySource("classpath:config/mailing.properties")在您的测试类上注释即可。

You should be able to read out the property for example with the @Valueannotation.

例如,您应该能够使用@Value注释读出该属性。

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

To avoid annotate this annotation on multiple test classes you can implement a superclass or meta-annotations.

为了避免在多个测试类上注释此注释,您可以实现超类或元注释



EDIT1:

编辑1:

@SpringBootApplication
@PropertySource("classpath:config/mailing.properties")
public class DemoApplication implements CommandLineRunner {

@Autowired
private MailService mailService;

public static void main(String[] args) throws Exception {
    SpringApplication.run(DemoApplication.class, args);
}

@Override
public void run(String... arg0) throws Exception {
    String s = mailService.getMailFrom();
    System.out.println(s);
}

MailService:

邮件服务:

@Service
public class MailService {

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

    public String getMailFrom() {
        return mailFrom;
    }

    public void setMailFrom(String mailFrom) {
        this.mailFrom = mailFrom;
    }
}

DemoTestFile:

演示测试文件:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = DemoApplication.class)
@TestPropertySource("classpath:config/mailing.properties")
public class DemoApplicationTests {

    @Autowired
    MailService mailService;

    @Test
    public void contextLoads() {
        String s = mailService.getMailFrom();
        System.out.println(s);
    }
}

enter image description here

在此处输入图片说明