【发布时间】:2021-12-21 12:02:39
【问题描述】:
假设以下类及其存储库:
import javax.persistence.*;
import java.io.Serializable;
@Entity
public class Student implements Serializable {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String lastName;
//getters , setters, Contructors
}
存储库是:
import org.springframework.data.jpa.repository.JpaRepository;
public interface StudentRepository extends JpaRepository<Student, Long> {}
我所做的测试是:为 H2 数据库配置 application.properties 并创建以下类:
import com.ndongoel.myDHL.repositories.AdresseRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class LoadDB {
private static final Logger log = LoggerFactory.getLogger(LoadDB.class);
@Bean
CommandLineRunner initDatabase(AdresseRepository adresseRepository) {
return args -> {
Student stu = new Student(null,"Mike","Smith");
log.info("Preloading " + studentRepository.save(stu));
}
}
只需检查控制台和 H2 数据库!
【问题讨论】:
-
您可以使用测试容器进行集成测试。
标签: java spring spring-boot jpa spring-data-jpa