亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

未注入 JUnit 5 測試類中測試配置文件中的 Spring bean

未注入 JUnit 5 測試類中測試配置文件中的 Spring bean

智慧大石 2022-01-06 17:01:30
我想在我的jUnit 5測試類中使用@SpringBootTest@RunWith(JUnitPlatform.class)@ActiveProfiles("localtest")class WorkreportDbRepositoryTest {  @Autowired  private SystemPriceSettingService systemPriceSettingService;// the rest omitted .... }在用于測試環境的配置中創建的 bean:@Profile("localtest")@Configurationpublic class TestConfig {  @Bean  public SystemPriceSettingService systemPriceSettingService(){    return new MemoryPriceSettingService();  }}但是SystemPriceSettingServicebean沒有被注入。我的設置有什么問題?
查看完整描述

1 回答

?
長風秋雁

TA貢獻1757條經驗 獲得超7個贊

您不使用支持 Spring 的 JUnit Runner。所以沒有創建 Spring 上下文。

您應該將其替換為測試類上的注釋,例如:


import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.test.context.junit.jupiter.SpringExtension;


@SpringBootTest

@ExtendWith(SpringExtension.class)

@ActiveProfiles("localtest")

class WorkreportDbRepositoryTest { ...}

并添加此依賴項以便能夠使用SpringExtension:


<dependency>

    <groupId>org.mockito</groupId>

    <artifactId>mockito-junit-jupiter</artifactId>

    <version>2.18.0</version> <!-- your mockito version-->

    <scope>test</scope>

</dependency>


查看完整回答
反對 回復 2022-01-06
  • 1 回答
  • 0 關注
  • 240 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號