-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Problems with db when doing testing
PostPosted: Wed Jun 02, 2010 5:17 am 
Newbie

Joined: Wed Jun 02, 2010 5:10 am
Posts: 3
Hello, i'm new in hibernate. I'm doing some tests using testng.
I got a problem because the information is created in the db, and it may not happened.

I use:
@BeforeClass
public void beforeClass() {

AnnotationConfiguration configuration = new AnnotationConfiguration();
...
sessionFactory = configuration.buildSessionFactory();
}
@BeforeMethod
public void beforeTest() {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}

@AfterMethod
public void afterTest() {
if (transaction != null && transaction.isActive()) transaction.rollback();
}

public void setupRegistry(Class<?>... moduleClasses) {
RegistryBuilder builder = new RegistryBuilder();
builder.add(moduleClasses);
registry = builder.build();
registry.performRegistryStartup();
}

public final void shutdown() {
throw new UnsupportedOperationException("No registry shutdown until @AfterSuite.");
}

@AfterClass
public final void shutdownRegistry() {
if (registry != null) {
registry.cleanupThread();
registry.shutdown();
registry = null;
}
}

where is the problem? with the rollback()?

thanks to read people!


Top
 Profile  
 
 Post subject: Re: Problems with db when doing testing
PostPosted: Wed Jun 02, 2010 5:53 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
I got a problem because the information is created in the db, and it may not happened.

Sorry I don't understand you. Of course as you're using rollback nothing will be saved on the database.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Problems with db when doing testing
PostPosted: Wed Jun 02, 2010 6:26 am 
Newbie

Joined: Wed Jun 02, 2010 5:10 am
Posts: 3
ok, I insert all the test code:

public class ClubBaseTest extends Assert {
protected Session session;
protected Transaction transaction;
protected static SessionFactory sessionFactory;
protected static Registry registry;

@BeforeClass
public void beforeClass() {
// setupRegistry(TagCategoryTestModule.class);
// sessionFactory = registry.getService(SessionFactory.class);
AnnotationConfiguration configuration = new AnnotationConfiguration();
configuration.configure("hibernate.cfg.xml");
configuration.addAnnotatedClass(ClubTipoC.class);
configuration.addAnnotatedClass(ClubTipoB.class);
configuration.addAnnotatedClass(ClubTipoA.class);
configuration.addAnnotatedClass(ClubBase.class);
configuration.addAnnotatedClass(User.class);
configuration.addAnnotatedClass(Lista.class);
configuration.addAnnotatedClass(Reserva.class);
configuration.addAnnotatedClass(Foto.class);
configuration.addAnnotatedClass(Comentari.class);
sessionFactory = configuration.buildSessionFactory();
}

@BeforeMethod
public void beforeTest() {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}

@AfterMethod
public void afterTest() {
if (transaction != null && transaction.isActive()) transaction.rollback();
}

public void setupRegistry(Class<?>... moduleClasses) {
RegistryBuilder builder = new RegistryBuilder();
builder.add(moduleClasses);
registry = builder.build();
registry.performRegistryStartup();
}

public final void shutdown() {
throw new UnsupportedOperationException("No registry shutdown until @AfterSuite.");
}

@AfterClass
public final void shutdownRegistry() {
if (registry != null) {
registry.cleanupThread();
registry.shutdown();
registry = null;
}
}

@Test
public void testCreate() {
ClubBase myClubBase = new ClubBase();
myClubBase.setName("some name of club base");
// myTagCategory = persistenceService.save(myTagCategory);
session.save(myClubBase);

assertEquals(myClubBase.getName(), "some name of club base");
assertNotNull(myClubBase.getId());
}

@Test
public void testRetrieve() {
ClubBase myClubBase = new ClubBase();
myClubBase.setName("some name of club base");
// myTagCategory = persistenceService.save(myTagCategory);
session.save(myClubBase);
assertEquals(myClubBase.getName(), "some name of club base");
assertNotNull(myClubBase.getId());

ClubBase myReturnedClubBase;
myReturnedClubBase = (ClubBase) session.get(ClubBase.class, myClubBase.getId());

assertEquals(myClubBase, myReturnedClubBase);
assertEquals(myClubBase.getName(), myReturnedClubBase.getName());
}

@Test
public void testUpdate() {
ClubBase myClubBase = new ClubBase();
myClubBase.setName("some name of club base");
session.saveOrUpdate(myClubBase);

assertEquals(myClubBase.getName(), "some name of club base");
assertNotNull(myClubBase.getId());

ClubBase myReturnedClubBase;
myReturnedClubBase = (ClubBase) session.get(ClubBase.class, myClubBase.getId());

assertEquals(myClubBase, myReturnedClubBase);
assertEquals(myClubBase.getName(), myReturnedClubBase.getName());

myReturnedClubBase.setName("some other name of club base");
Long id = myReturnedClubBase.getId();

session.saveOrUpdate(myReturnedClubBase);

assertEquals(id, myReturnedClubBase.getId());
assertEquals("some other name of club base", myReturnedClubBase.getName());
}

@Test
public void testDelete() {
ClubBase myClubBase = new ClubBase();
myClubBase.setName("some name of club base");

session.save(myClubBase);
assertEquals(myClubBase.getName(), "some name of club base");
assertNotNull(myClubBase.getId());

Long id = myClubBase.getId();

session.delete(myClubBase);

ClubBase myReturnedClubBase = null;
try {
myReturnedClubBase = (ClubBase) session.get(ClubBase.class, id);
} catch (RuntimeException e) {
}

assertNull(myReturnedClubBase);
}

@Test
public void testGetAllInstances() {
ClubBase firstDomainObject = new ClubBase();
firstDomainObject.setName("this is the first one");
session.save(firstDomainObject);

ClubBase secondDomainObject = new ClubBase();
secondDomainObject.setName("this is the second object");
session.save(secondDomainObject);

List<ClubBase> objectList = session.createCriteria(ClubBase.class).list();

assertFalse(objectList.isEmpty());
int i = objectList.indexOf(firstDomainObject);
assertTrue(i >= 0);

assertFalse(objectList.isEmpty());
int j = objectList.indexOf(secondDomainObject);
assertTrue(j >= 0);

ClubBase myReturnedClubBase;

myReturnedClubBase = objectList.get(i);

assertEquals(firstDomainObject, myReturnedClubBase);
assertEquals(firstDomainObject.getName(), myReturnedClubBase.getName());
}

Can find something wrong?


Top
 Profile  
 
 Post subject: Re: Problems with db when doing testing
PostPosted: Wed Jun 02, 2010 7:03 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
what are you asking? what is failing? what would you expect?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Problems with db when doing testing
PostPosted: Thu Jun 03, 2010 3:16 am 
Newbie

Joined: Wed Jun 02, 2010 5:10 am
Posts: 3
The problem i hace is everytime I execute the test, create a new object in the db.. and it may not.. why? i'm not using correctly the rollback?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.