【问题标题】:How to using function postgres in UnitTest when using spring boot使用spring boot时如何在UnitTest中使用函数postgres
【发布时间】:2019-05-05 02:54:51
【问题描述】:

我有一个使用 PostgreSQL 自动生成 student_no 的函数。当我在 Postgres 和 Spring boot 中使用它时,它工作得很好。但是当我在单元测试中使用它时遇到了问题。在 Postgres 中,当我使用以下命令时:

Insert into students(firstName,lastName,age) 
values ('Nguyen','Tran',12)

在数据库 student_no 自动生成器中看起来像“28372”。与 Spring Boot 相同。当我调用方法时:studentRepository.save(student) 它会自动生成 student_no。但是当我进行以下单元测试时:

@Slf4j
public class StudentTest {

      @Mock
      private StudentRepository studentRepository;

      @Test
      public void testGenerateStudentBooking_validRequest() {
         Student student = new Student("Nguyen","Tran",23);
         Student student = studentRepository.save(student);        
      }           
} 

当我保存和调试时,student_no 没有生成。我不明白为什么?

班级学生:

@Entity
@NoArgsConstructor
@RequiredArgsConstructor
public class Student {

  @Id
  private String id;
  private String firstName;
  private String lastName;
  private Integer age;
  private String student_no;
}

学生资料库

public inteface StudentRepository extends CrudRepository(Student,Integer) {
     Student findStudentByStudentNo(String StudentNo);
}

学生考试

@Slf4j
public class StudentTest {

      @Mock
      private StudentRepository studentRepository;

      @Test
      public void testGenerateStudentBooking_validRequest() {
         Student student = new Student("Nguyen","Tran",23);
         Student student = studentRepository.save(student);  
         //Student.studentNo is null :(

         Student student1 = studentRepository.findStudentByStudentNo(student.getStudentNo);   
      }           
}

如果可能的话,我如何告诉hibernate booking_no 将被自动生成并且hibernate 不会对它做任何事情。或者换句话说,如何在使用 UnitTest 时使用 Postgres 中的函数自动生成 booking_no。

【问题讨论】:

  • 为什么需要student_no?断言声明?还有你为什么不在内存数据库中使用H2 来测试用例
  • 我无法使用 H2,因为我的项目使用了 postgresSql。我使用 student_no 因为我有方法 find Student by student_no
  • 我建议将H2 数据库仅用于测试用例
  • 感谢您的建议。但是函数写入 Postgres 并由 Postgres 触发。它工作成功,只有单元测试不能工作。我不认为改变 Db 是个好主意。
  • 检查我的答案

标签: java spring postgresql hibernate unit-testing


【解决方案1】:

有两种方法可以解决您的问题

  1. 你可以在内存数据库中使用H2进行集成测试,所以你不需要模拟StudentRepository
  2. 第二种方法是嘲笑你现在关注的StudentRepository

第二种方法的问题是您需要为模拟对象定义存根,即每当调用StudentRepository.save() 方法时,返回随机生成的id 的学生对象

 @Slf4j
 public class StudentTest {

  @Mock
  private StudentRepository studentRepository;

  @Test
  public void testGenerateStudentBooking_validRequest() {
     Random random = new Random();
     when(this.studentRepository(ArgumentMatchers.any()))
          .thenReturn(new Student("Nguyen","Tran",23,random.nextInt(900) + 100 ));
   //random.nextInt(900) + 100   will generate a random integer from 0 to 899, then add 100
     Student student = new Student("Nguyen","Tran",23);
     Student student = studentRepository.save(student);

您也可以使用ThreadLocalRandom 生成100(包括)和1000(不包括)之间的随机int值

ThreadLocalRandom.current().nextInt(100, 1000);

【讨论】:

    猜你喜欢
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 2019-09-12
    • 2021-08-18
    • 2019-03-23
    • 1970-01-01
    相关资源
    最近更新 更多