[英]Spring bean from test profile in JUnit 5 test class is not injected
我想在我的jUnit 5測試課程中使用
@SpringBootTest
@RunWith(JUnitPlatform.class)
@ActiveProfiles("localtest")
class WorkreportDbRepositoryTest {
@Autowired
private SystemPriceSettingService systemPriceSettingService;
// the rest omitted ....
}
在用於測試環境的配置中創建的bean:
@Profile("localtest")
@Configuration
public class TestConfig {
@Bean
public SystemPriceSettingService systemPriceSettingService(){
return new MemoryPriceSettingService();
}
}
但是不會注入SystemPriceSettingService
bean。 我的設置有什么問題?
您不會使用支持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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.