Nguji Kode Hibernate

All lectures for JV purposes
tingkat , wulangan
kasedhiya

Base ing memori lan testing

Lan saiki sing paling menarik. Nalika nyoba kode Hibernate, asring banget sampeyan pengin kerja ora nganggo basis nyata, nanging kanthi sawetara jinis rintisan sing nindakake fungsi minimal.

Apa sampeyan bisa mbayangno rintisan sing ngetrapake sebagian besar standar SQL Server? Aku ora. Nanging, database ing memori apik banget. Kerjane kira-kira kaya iki:

  • Ing metode @BeforeAll, kita miwiti sambungan database ing memori.
  • Ing metode @BeforeEach, kita entuk sesi lan mbukak transaksi.
  • Ing metode @Test, kita nggarap sesi lan transaksi saiki.
  • Ing metode @AfterEach, kita nindakake transaksi kasebut.
  • Lan pungkasane, ing metode AfterAll, kita nutup sambungan menyang database.

Mangkene persiapan kanggo tes:

@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();
  }

Lan iki kaya tes kasebut karo karya 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());
  }
}

data tes

Sampeyan uga bisa isi database test karo data test.

Biasane rong file sql digawe kanggo iki:

  • schema.sql ngandhut script sing nggawe tabel ing database
  • test-data.sql ngandhut script sing populates tabel karo data test

Data nggawe tabel lan tes biasane dipisahake dadi file sing beda-beda, amarga klompok data tes dhewe kanggo klompok tes sing beda meh katon.

Eksekusi file kasebut katon kaya iki:

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()
}

Lan cara persiyapan sampeyan bakal rada owah:

@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);
  }
}
Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION