我想在我的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>
添加回答
舉報
0/150
提交
取消