【问题标题】:Using properties files in spring-boot tests在 spring-boot 测试中使用属性文件
【发布时间】:2016-10-03 06:18:49
【问题描述】:

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

在主应用程序中我使用:

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

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

问题是:

  • 我应该在我的src/test 文件夹中有单独的资源,还是可以从src/main 文件夹中访问资源,如果是,如何?

更新添加的来源

测试类:

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

    @Autowired
    private TestService testService;

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

}

服务等级:

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

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

主应用类:

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

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

【问题讨论】:

    标签: java spring testing spring-boot


    【解决方案1】:

    你可以在你的测试类中使用@TestPropertySource注解。

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

    mailFrom=fromMe@mail.com

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

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

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

    为避免在多个测试类上注释此注释,您可以实现一个超类或meta-annotations


    编辑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);
    }
    

    邮件服务:

    @Service
    public class MailService {
    
        @Value("${mailFrom}")
        private String mailFrom;
    
        public String getMailFrom() {
            return mailFrom;
        }
    
        public void setMailFrom(String mailFrom) {
            this.mailFrom = mailFrom;
        }
    }
    

    演示测试文件:

    @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);
        }
    }
    

    【讨论】:

    • 但是文件.properties 可以留在src/main/resources... 中不需要制作src/test/resources...
    • 是的,正确的。我想你有这样的配置src/main/resources/config/mailing.properties。如果是,它应该可以工作
    • 仍然得到:Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/config/mailing.properties] 还添加了简单的资源,我在上面测试了你的解决方案
    • @Bublik 刚刚尝试在您的代码上构建一个新项目,它可以工作。你能展示你的项目结构吗?用我的测试代码更新了我的答案
    • 刚刚添加的结构
    猜你喜欢
    • 1970-01-01
    • 2018-03-01
    • 2019-03-20
    • 2017-07-12
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 2018-08-03
    • 1970-01-01
    相关资源
    最近更新 更多