shr*_*rma 6 java junit unit-testing junit5
我正在学习 Junit 5 和测试用例。我正在使用 spring boot 版本'2.2.6.RELEASE 和 JUnit 5,在我的应用程序中,我有一个基于属性文件中的布尔标志进行处理的方法。
\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
Run Code Online (Sandbox Code Playgroud)
数据库连接属性用于创建数据库连接
数据源.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;
Run Code Online (Sandbox Code Playgroud)
控制器类
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
控制器测试.java
@SpringBootTest
class ControllerTest {
@Autowired
private Controller controller;
@Test
void show() {
controller.validateObject("sharma");
}
Run Code Online (Sandbox Code Playgroud)
默认情况下,该标志为 false,因此每次测试用例运行时,它都不会处理该对象。所以我尝试在测试文件夹中创建 aplication.properties
\src\test\resources\application.properties
spring.property.name=vishal
spring.property.enable=true
Run Code Online (Sandbox Code Playgroud)
但现在它给了我一个错误
Could not resolve placeholder 'spring.app.datasource.url'
Run Code Online (Sandbox Code Playgroud)
但我不想提供数据库连接 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 {
Run Code Online (Sandbox Code Playgroud)
通过提供键和值来暂时解决问题,但我有很多键,无法以这种方式提供。
如果你使用@SpringBootTest,那么你的测试将加载整个 Spring 上下文。这意味着它将创建所有的 beans 并尝试将它们连接在一起。如果您将属性值注入到您的 bean 中,则必须为此类测试指定所有属性值,否则您将无法启动应用程序。
在这种情况下,可能对您有帮助的是使用测试注释,例如@WebMvcTest或@DataJpaTest专注于仅测试应用程序的一部分。使用@WebMvcTest您将获得一个仅包含控制器和与 Web 层相关的所有内容的应用程序上下文。其他 bean(如服务类)可以用@MockedBean.
接下来,为了测试服务类中的业务逻辑,请尽量不要使用@SpringBootTest而是依赖普通的 JUnit 5 和 Mockito 来验证您的代码。
您可能仍然希望进行一些集成测试,@SpringBootTest以确保一切都能协同工作。在这种情况下,您可以对内部的任何静态属性进行硬编码application.properties或src/test/resources/使用注释,就像您已经做的那样:@TestPropertySource(properties = {"spring.property.name=vishal", " spring.property.status=true"})。
在提供数据库时,您可以配置嵌入式数据库(我会尽量避免)或使用与生产中相同的数据库。在为测试提供外部基础设施(例如数据库)时,测试容器可以为您提供很大帮助。
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() {
}
}
Run Code Online (Sandbox Code Playgroud)
对于 Junit5,用以下路径注释您的测试application.properties类src/main/resources:
@ExtendWith(SpringExtension.class)
@TestPropertySource("classpath:application.properties")
public class MyTest {
@Value("${property.name}")
private int myProperty;
tests...
}
Run Code Online (Sandbox Code Playgroud)
属性被加载