![](/img/trans.png)
[英]Spring Batch JUnit test not loading application.properties for JobLauncherTestUtils
[英]Junit 5 with test application.properties
我正在學習 Junit 5 和測試用例。 我正在使用 spring 引導版本 '2.2.6.RELEASE 和 JUnit 5,在我的應用程序中,我有一個基於文件中的 Z84E2C64F38F78BA3EA5C905AB5A2DA2 標志的方法。
\src\main\resources\application.properties
#data base connection properties
spring.app.datasource.url=jdbc:mysql://localhost:3306/student_db
spring.app.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.app.datasource.username=root
spring.datasource.password=root
spring.app.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
#additional properties
spring.property.name=shrikant
spring.property.enable=false
數據庫連接屬性用於創建數據庫連接
數據源.java
@Value("${spring.app.datasource.url}")
private String url;
@Value("${spring.app.datasource.driver-class-name}")
private String className;
@Value("${spring.app.datasource.username}")
private String userName;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.app.jpa.properties.hibernate.dialect}")
private String dialect;
controller class
@RestController
public class Controller {
@Value("${spring.property.name}")
private String name;
@Value("${spring.property.enable}")
private boolean status;
public void validateObject(String surName) {
if (status) { # if this flag is true then only process
System.out.println("name= " + name);
System.out.println("surName= " + surName);
}
}
控制器Test.java
@SpringBootTest
class ControllerTest {
@Autowired
private Controller controller;
@Test
void show() {
controller.validateObject("sharma");
}
默認情況下,該標志為 false,因此每次運行測試用例時它都不會處理 object。 所以我嘗試在測試文件夾中創建 aplication.properties
\src\test\resources\application.properties
spring.property.name=vishal
spring.property.enable=true
但現在它給了我一個錯誤
Could not resolve placeholder 'spring.app.datasource.url'
但我不想提供數據庫連接 URL,我在測試時沒有連接到數據庫。
Q1 - 如何僅更改測試用例的屬性文件的值。
Q2 - 是否必須提供 \src\main\resources\application.properties 的所有鍵是 \src\test\resources\application.properties?
我是測試用例的新手,所以很少有解釋的答案會受到歡迎。
更新:-我發現
@SpringBootTest
@TestPropertySource(properties = {"spring.property.name=vishal", " spring.property.status=true"})
class ControllerTest {
將通過提供鍵和值來暫時解決問題,但是我有很多鍵,無法以這種方式提供。
如果您使用@SpringBootTest
,那么您的測試將加載整個 Spring 上下文。 這意味着它將創建您所有的 bean 並嘗試將它們連接在一起。 如果將屬性值注入到 bean,則必須為此類測試指定所有屬性值,否則將無法啟動應用程序。
在這種情況下可能對您有所幫助的是使用@WebMvcTest
或@DataJpaTest
類的測試注釋來專注於測試應用程序的片段。 使用@WebMvcTest
,您將獲得一個應用程序上下文,其中僅包含控制器以及與您的 web 層相關的所有內容。 可以使用@MockedBean
其他 bean(如服務類)。
接下來,為了測試服務類中的業務邏輯,盡量不要使用@SpringBootTest
,而是依靠普通的 JUnit 5 和 Mockito 來驗證您的代碼。
您仍然可能希望進行一些使用@SpringBootTest
的集成測試,以確保一切正常工作。 在這種情況下,您可以在src/test/resources/
中的application.properties
中對任何 static 屬性進行硬編碼,或者使用您已經做過的注釋: @TestPropertySource(properties = {"spring.property.name=vishal", " spring.property.status=true"})
。
在提供數據庫時,您可以配置嵌入式數據庫(我會盡量避免)或使用與生產中相同的數據庫。 在為您的測試提供外部基礎設施(如數據庫)時, Testcontainers可以為您提供很多幫助。
Spring Boot >= 2.2.6 和 JUnit 的示例設置可能如下所示:
@Testcontainers
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ApplicationIT {
@Container
public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer()
.withPassword("password")
.withUsername("username");
@DynamicPropertySource
static void postgresqlProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl);
registry.add("spring.datasource.password", postgreSQLContainer::getPassword);
registry.add("spring.datasource.username", postgreSQLContainer::getUsername);
}
@Test
public void contextLoads() {
}
}
對於 Junit5,在您的src/main/resources
中使用application.properties
的路徑注釋您的測試 class :
@ExtendWith(SpringExtension.class)
@TestPropertySource("classpath:application.properties")
public class MyTest {
@Value("${property.name}")
private int myProperty;
tests...
}
屬性被加載
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.