我有一个SpringBoot应用程序,我有一个配置包
@Configuration
@EnableJpaAuditing
public class PersistenceConfig {
}
Run Code Online (Sandbox Code Playgroud)
但PersistenceConfig不会在PersonRepositoryTest中被捕获
@RunWith( SpringRunner.class )
@DataJpaTest
public class PersonRepositoryTest {
// Tests ...
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我改变@DataJpaTest to @SpringBootTest,PersonRepositoryTest将获取配置.
我的包结构是
- main
- java
- config
PersistenceConfig.java
- domain
Person.java
- persistence
PersonRepository.java
Application.java // @SpringBootApplication
- test
- java
- persistence
PersonRepositoryTest.java
Run Code Online (Sandbox Code Playgroud)
Spring Boot 1.4中的测试改进建议使用@DataJpaTest测试持久层
观察: 在Test类上执行两个注释仍然不导入config @SpringBootTest @DataJpaTest
问题1: 使用@DataJpaTest测试持久层时如何正确(Spring Boot中的最佳实践方式)将配置包导入我的测试?
问题2: 使用@SpringBootTest是否可以接受?我知道@DataJpaTest也是一个元注释,我的数据库有合理的自动配置,包括事务管理.但如果我不需要呢?
configuration spring unit-testing spring-data-jpa spring-boot
我正在研究Spring Data JPA 和 Postgres示例。在此示例中,我Auditing通过以下链接实现:https://www.baeldung.com/database-auditing-jpa和Spring Boot JPA@CreatedDate @LastModifiedDate not being populated when saving the object。审核工作非常好当我执行repository.save时,在这种情况下,两个带有注释的字段@CreatedDate都@LastModifiedDate正确保存。
但当我尝试更新该方法时,并没有发生同样的情况。
我开发了以下方法。
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EntityListeners(AuditingEntityListener.class)
@Entity
@Table(uniqueConstraints = {
@UniqueConstraint(name="student_name_key",columnNames = {"studentName"})
})
public class Student {
....
....
@Column(name="lastUpdateUser")
private String lastUpdateUser;
@LastModifiedDate
@Column(name="lastUpdateDate", nullable = false)
private LocalDateTime lastUpdateDate;
}
Run Code Online (Sandbox Code Playgroud)
主应用程序
@SpringBootApplication
@EnableJpaAuditing
@EnableJpaRepositories(basePackages = {"com.xxx.xxx.repository"})
@ComponentScan(basePackages = {"com.xxx.yyy","com.xxx.xxx.studentportfolio"})
@EnableCaching
@EnableAsync
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class, SecurityAutoConfiguration.class})
public …Run Code Online (Sandbox Code Playgroud)