簡體   English   中英

Spring Boot - Junit 無法在 junit 測試中加載配置屬性

[英]Spring Boot - Junit unable to load configuration properties in junit test

我在src/main/resources有一個包含以下內容的consumer.properties文件,以及一個隨附的 Configuration 類,該類將文件的內容加載並存儲到類成員變量中:

// src/main/resources consumer.properties文件:

com.training.consumer.hostname=myhost
com.training.consumer.username=myusername
com.training.consumer.password=mypassword

//ConsumerConfig.java

@Configuration
@PropertySource(
        value= {"classpath:consumer.properties"}
        )
@ConfigurationProperties(prefix="com.training.consumer")
public class ConsumerConfig {

    private String hostname;
    private String username;
    private String password;

    public ConsumerConfig() { }

    public String getHostname() {
        return hostname;
    }
    public void setHostname(String hostname) {
        this.hostname = hostname;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "ConsumerConfig [hostname=" + hostname + ", username=" + username + ", password=" + password + "]";
    }
}

我還有一個ConfigsService類,它自動連接ConsumerConfig類以檢索各個屬性:

@Component
public class ConfigsService {

    @Autowired
    ConsumerConfig consumerConfig;

    public ConsumerConfig getConsumerConfig() {
        return consumerConfig;
    }

    public void showConfig() {
        consumerConfig.toString();
    }

    public ConsumerConfig getConfig() {
        return consumerConfig;
    }
}

運行 ConfigsService 的方法時,這些屬性加載得很好。 問題出在單元測試中,其中調用configService.getConfig().getHostname()返回空值——即使在創建了src/test/resources目錄並在其中添加了我的consumer.properties文件之后:

@TestPropertySource("classpath:consumer.properties")
public class ConfigsServiceTest {

    @Mock
    ConsumerConfig consumerConfig;

    @InjectMocks
    ConfigsService configService;


    @Before
    public void beforeEach() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void someTest() {
        System.out.println(configService.getConfig().getHostname()); //outputs null here -- wth!
        Assert.assertTrue(true);
    }
}

由於模擬對象,您將獲得空值。 我會建議使用帶有上下文配置的 spring runner,它將加載配置類中的屬性並創建 bean。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ConsumerConfig.class, ConfigsService.class})
@TestPropertySource(locations = "classpath:consumer.properties")
public class ConfigsServiceTest {

  @Autowired
  private ConfigsService configsService;

  @Test
  public void someTest() {
    Assert.assertNotNull(configService.getConfig().getHostname());
  }
}

該錯誤可能是因為在 ConfigsServiceTest 類中,您正在確定您的 ConsumerConfig 是一個 Mock,這就是為什么如果您刪除它應該可以工作的代碼,它不會加載您的配置

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM