【问题标题】:Spring boot test, can not load properties file for junit test春季启动测试,无法加载junit测试的属性文件
【发布时间】:2018-08-27 02:48:53
【问题描述】:

这是我的项目结构

src/main/java
            /main/ApplicationContext.java
src/main/resource/
            /application-context.xml
            /conf/soap.properties
src/test/java
            /main/TestApplicationContext.java           
src/test/resource/
            /application-context-test.xml
            /confTest/soapTest.properties

然后TestApplicationContext.java

@SpringBootApplication
@ImportResource("classpath:application-context-test.xml")
@ActiveProfiles("test")
public class TestApplicationContext {

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

}

以及我如何加载特定于 Junit 测试的属性

@ActiveProfiles("test")
@SpringBootTest
@ContextConfiguration(classes = {TestApplicationContext.class})
@AutoConfigureWireMock(port = 8081, httpsPort = 443)
@TestPropertySource(locations = {"classpath:confTest/soapTest.properties"})
@RunWith(SpringJUnit4ClassRunner.class)

public class ApplicationTest {
    @Test
    void testABC(){
        //test here
    }
}

但似乎confTest/soapTest.properties文件中的属性无法在运行时加载

它显示消息 Could not resolve placeholder 'service.test.url' in value "${service.test.url}"

事实上service.test.url 在soapTest.properties 中可用

我知道这可能是重复的问题,但我已经用谷歌搜索并尝试了许多解决方案,但对我不起作用

我的配置有问题吗

【问题讨论】:

    标签: spring-boot junit


    【解决方案1】:

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

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

    mailFrom=fromMe@mail.com
    

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

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

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

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

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

    是的@Ryo....你的正确 用这个@Order(Ordered.HIGHEST_PRECEDENCE + 99)

    【讨论】:

    • 谢谢,我已经在使用@TestPropertyResource。但它仍然不起作用,我将order="1" 添加到<context:place-holder .../> 然后它的工作
    • @Order(Ordered.HIGHEST_PRECEDENCE + 99)
    • 嗯,好像又出了问题。添加order 属性后,当我在没有运行测试的情况下运行应用程序时,它不再从/src/main/resources 加载配置
    猜你喜欢
    • 1970-01-01
    • 2018-01-21
    • 2011-09-26
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    • 2012-06-18
    相关资源
    最近更新 更多