W
W
web_dev2013-01-17 13:13:08
Java
web_dev, 2013-01-17 13:13:08

Working example for: Unit testing with JUnit for Hibernate using HSQLDB (In-Memory)?

Hello, can you tell me a working example for "Unit testing with JUnit for Hibernate using HSQLDB (In-Memory)"?
Did some digging,
vageeshhoskere.wordpress.com/2011/06/16/unit-test-... - org.hibernate.cfg.AnnotationConfiguration is deprecated and the example is not very clear.
tshikatshikaaa.blogspot.de/2012/09/junit-testing-s... - here it's also not very clear what to write where ...
Thank you. Only please, who wants to answer, treat with understanding. I am doing this for the first time.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
ivnik, 2013-01-17
@ivnik

1. About deprecated:

@deprecated All functionality has been moved to { link Configuration}

those. just change AnnotationConfiguration to Configuration (Configuration is in the same package).
2. Regarding the second example, jpa is used there, which has some differences from working with hibernate directly. And also there is the configuration of the spring context carried out through “java config”, or are there no questions about this?
If you need a working example, then I can give it, but I use a) TestNG b) JPA + hibernate and spring-managed transactions. Wouldn't that make it even more confusing?

W
web_dev, 2013-01-17
@web_dev

Thanks, I seem to have figured it out. ))
That's how it works, vott ... maybe someone will come in handy.
persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
  version="1.0">
  <persistence-unit name="in.memory.test"
    transaction-type="RESOURCE_LOCAL">
    <description>
      In memory example using Hibernate and HSQLDB
    </description>
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>de.htw_berlin.f4.ai.jobboerse.domain.UserJob</class>
    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
      <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
      <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:test" />
      <property name="hibernate.connection.username" value="sa" />
      <property name="hibernate.connection.password" value="" />
      <property name="hibernate.show_sql" value="true" />
      <property name="hibernate.hbm2ddl.auto" value="create" />
    </properties>
  </persistence-unit>
</persistence>

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

public class EntityManagerProvider {

  public static EntityManagerFactory emf;
    
    @PersistenceUnit
    public void setEntityManagerFactory(EntityManagerFactory emf) {
        this.emf = emf;
    }
    
    public static EntityManager createEntityManager(){
    	return emf.createEntityManager();
    }
}


import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserJobTest {

  private UserJob userJob = new UserJob();

  @BeforeClass
  public static void setUp() {
    ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/application-context.xml");
  }

  @Before
  public void init() {
    Profile profile = new Profile();
    userJob.setProfile(profile);
    userJob.setFirstname("Oleg");
    userJob.setLastname("Lastname");
    userJob.setPostalcode("10318");
    userJob.setStreet("Street");
    userJob.setCity("Berlin");
    userJob.setEmail("E-Mail");
    userJob.setIsActive(true);
    userJob.setPassword("pass");
  }

  @Test
  public void saveAndLoadUserJobTest() {

    userJob.save();

    UserJob loadedUserJob = userJob.load("E-Mail", "pass");

    Assert.assertEquals(loadedUserJob.getFirstname(), "Oleg");
    Assert.assertTrue(loadedUserJob.getId() > 0);
  }

  @Test
  public void updateUserJobTest() {

    userJob.setFirstname("OlegUpd");
    userJob.setLastname("LastnameUpd");
    userJob.setPostalcode("10318Upd");
    userJob.setStreet("StreetUpd");
    userJob.setCity("BerlinUpd");
    userJob.setEmail("E-MailUpd");
    userJob.setIsActive(false);
    userJob.setPassword("passUpd");

    userJob.update();

    UserJob loadedUserJob = userJob.load("E-MailUpd", "passUpd");
    System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>"+ loadedUserJob.toString());
    Assert.assertEquals(loadedUserJob.getFirstname(), "OlegUpd");
    Assert.assertEquals(loadedUserJob.getLastname(), "LastnameUpd");
    Assert.assertEquals(loadedUserJob.getPostalcode(), "10318Upd");
    Assert.assertEquals(loadedUserJob.getStreet(), "StreetUpd");
    Assert.assertEquals(loadedUserJob.getCity(), "BerlinUpd");
    Assert.assertEquals(loadedUserJob.getEmail(), "E-MailUpd");
    Assert.assertEquals(loadedUserJob.getIsActive(), false);
    Assert.assertEquals(loadedUserJob.getPassword(), "passUpd");

    Assert.assertTrue(loadedUserJob.getId() > 0);
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question