Cơ sở trong bộ nhớ và thử nghiệm

Và bây giờ là thú vị nhất. Khi kiểm tra mã Hibernate, bạn thường không muốn làm việc với cơ sở thực mà với một số loại sơ khai thực hiện chức năng tối thiểu.

Bạn có thể tưởng tượng một sơ khai thực hiện hầu hết các tiêu chuẩn SQL Server không? Tôi không. Tuy nhiên, cơ sở dữ liệu trong bộ nhớ là tuyệt vời như vậy. Nó hoạt động đại khái như thế này:

  • Trong phương thức @B BeforeAll, chúng tôi khởi tạo kết nối cơ sở dữ liệu trong bộ nhớ.
  • Trong phương thức @B BeforeEach, chúng tôi nhận phiên và mở giao dịch.
  • Trong phương thức @Test, chúng tôi làm việc với phiên và giao dịch hiện tại.
  • Trong phương thức @AfterEach, chúng tôi cam kết giao dịch.
  • Và cuối cùng, trong phương thức AfterAll, chúng tôi đóng kết nối với cơ sở dữ liệu.

Đây là cách chuẩn bị cho bài kiểm tra:

@Test
public class HelloTest {
  private static SessionFactory sessionFactory = null;
  private Session session = null;

  @BeforeAll
  static void setup(){
    try {
  	StandardServiceRegistry standardRegistry  = new StandardServiceRegistryBuilder()
      	.configure("hibernate-test.cfg.xml").build();

  	Metadata metadata = new MetadataSources(standardRegistry)
      	.addAnnotatedClass(Employee.class)
      	.getMetadataBuilder()
      	.build();

      sessionFactory = metadata.getSessionFactoryBuilder().build();

    } catch (Throwable ex) {
        throw new ExceptionInInitializerError(ex);
    }
  }

  @BeforeEach
  void setupThis(){
      session = sessionFactory.openSession();
      session.beginTransaction();
  }

  @AfterEach
  void tearThis(){
      session.getTransaction().commit();
  }

  @AfterAll
  static void tear(){
      sessionFactory.close();
  }

Và đây là cách thử nghiệm trông như thế nào với công việc của Hibernate:

@Test
public class HelloTest {

  @Test
  void createSessionFactoryWithXML() {
       Employee emp = new Employee();
       emp.setEmail("demo-user@mail.com");
       emp.setFirstName("demo");
       emp.setLastName("user");

       Assertions.assertNull(emp.getEmployeeId());
       session.persist(emp);
       Assertions.assertNotNull(emp.getEmployeeId());
  }
}

dữ liệu thử nghiệm

Bạn cũng có thể điền dữ liệu thử nghiệm vào cơ sở dữ liệu thử nghiệm của mình.

Thông thường hai tệp sql được tạo cho việc này:

  • schema.sql chứa đoạn mã tạo bảng trong cơ sở dữ liệu
  • test-data.sql chứa một tập lệnh điền vào các bảng có dữ liệu thử nghiệm

Dữ liệu kiểm tra và tạo bảng thường được tách thành các tệp khác nhau, vì các nhóm dữ liệu kiểm tra riêng của chúng cho các nhóm kiểm tra khác nhau hầu như luôn xuất hiện.

Thực thi các tệp này trông như thế này:

void runSqlScriptFile(String filePath){
  	String sqlScript = new String( Files.readAllBytes(Paths.get(filePath)) );
  	Session session = sessionFactory.openSession();
      Query query = session.createNativeQuery("BEGIN " + sqlScript + " END;");
      query.executeUpdate()
}

Và phương pháp thiết lập của bạn sẽ thay đổi một chút:

@BeforeAll
static void setup(){
  try {
	StandardServiceRegistry standardRegistry  = new StandardServiceRegistryBuilder()
    	.configure("hibernate-test.cfg.xml").build();

	Metadata metadata = new MetadataSources(standardRegistry)
    	.addAnnotatedClass(Employee.class)
    	.getMetadataBuilder()
    	.build();

	sessionFactory = metadata.getSessionFactoryBuilder().build();
	runSqlScriptFile(“scema.sql”);
	runSqlScriptFile(“test-data.sql”);

  } catch (Throwable ex) {
      throw new ExceptionInInitializerError(ex);
  }
}