我正在学习 Junit 5 和测试用例。 我正在使用 spring boot 版本 '2.2.6.RELEASE 和 JUnit 5, 在我的应用程序中,我有一个基于属性文件中的 boolean 标志进行处理的方法。
\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 类
@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);
}
}
ControllerTest.java
@SpringBootTest
class ControllerTest {
@Autowired
private Controller controller;
@Test
void show() {
controller.validateObject("sharma");
}
默认情况下该标志为 false,因此每次运行测试用例时它都不会处理该对象。 所以我尝试在测试文件夹中创建 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 - how to change the value of properties file for test case only.
Q2 - is it mandatory to provide all the keys of \src\main\resources\application.properties is \src\test\resources\application.properties?
我是测试用例的新手,所以欢迎解释一下答案。
更新:- 我发现
@SpringBootTest
@TestPropertySource(properties = {"spring.property.name=vishal", " spring.property.status=true"})
class ControllerTest {
将通过提供键和值来暂时解决问题,但我有很多键,不能以这种方式提供。
最佳答案
如果您使用 @SpringBootTest
那么您的测试将加载整个 Spring 上下文。这意味着它将创建您所有的 bean 并尝试将它们连接在一起。如果您将属性值注入(inject) bean,则必须为此类测试指定它们all,否则,您将无法启动应用程序。
在这种情况下可能对您有所帮助的是使用像 @WebMvcTest
或 @DataJpaTest
这样的测试注释来专注于测试应用程序的片段。使用 @WebMvcTest
您将获得一个应用程序上下文,其中仅包含 Controller 以及与您的 Web 层相关的所有内容。可以使用 @MockedBean
模拟其他 bean(如服务类)。
接下来,为了测试服务类中的业务逻辑,尽量不要使用 @SpringBootTest
而是依靠普通的 JUnit 5 和 Mockito 来验证您的代码。
您仍然可能希望进行一些使用 @SpringBootTest
的集成测试,以确保一切正常工作。在这种情况下,您可以在 src/test/resources/
中的 application.properties
内对任何静态属性进行硬编码,或者使用您已经做过的注释:@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() {
}
}
关于java - 带有测试 application.properties 的 Junit 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62703535/