我有一个 Spring Boot 应用程序。现在我尝试用集成测试来覆盖我的用户服务。我检查在数据库中添加了哪些用户的方法不起作用。我在我的 pom.xml 文件中添加了 h2 数据库并为此数据库创建了测试属性。当我尝试运行测试时出现以下错误
错误
Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven- 4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>by.yankavets</groupId>
<artifactId>TypingTrainer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>TypingTrainer</name>
<description>TypingTrainer</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.11.5</version>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.24.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
测试班
@SpringBootTest
@ContextConfiguration(classes = {UserServiceImpl.class})
@TestPropertySource("/application-test.properties")
public class UserServiceImplTest {
private static final Long TEST_ID = 123L;
public static final User TEST_USER;
public static final String TEST_USER_NAME = "Ivan";
public static final String TEST_USER_EMAIL = "test@email.com";
public static final String TEST_PASSWORD = "1234";
public static final Role TEST_USER_ROLE;
public static final Set<Role> TEST_ROLES;
private static final List<User> TEST_USER_LIST;
private List<User> userList;
static {
TEST_USER_LIST = new ArrayList<>();
TEST_USER_ROLE = Role.builder()
.name("ROLE_USER")
.build();
TEST_ROLES = Set.of(TEST_USER_ROLE);
TEST_USER = User.builder()
.id(TEST_ID)
.name(TEST_USER_NAME)
.email(TEST_USER_EMAIL)
.password(TEST_PASSWORD)
.roles(TEST_ROLES)
.build();
TEST_USER_LIST.add(TEST_USER);
}
@MockBean
private UserRepository userRepository;
@Autowired
private UserService userService;
@Test
@DisplayName("Add user in database test")
void addUserInDatabaseTest() {
userService.save(TEST_USER);
Optional<User> savedUser = userRepository.findByEmail(TEST_USER.getEmail());
assertThat(savedUser).isPresent();
assertThat(savedUser).isEqualTo(TEST_USER);
}
属性文件
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:jpa_jbd
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.H2Dialect
spring.jpa.show-sql=true
我尝试移动属性文件、更改用户名和密码、删除 h2 依赖项、更改范围、编写事务注释。