Answer the question
In order to leave comments, you need to log in
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
1. About deprecated:
@deprecated All functionality has been moved to { link Configuration}
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 questionAsk a Question
731 491 924 answers to any question