Failed to config warp-persist for junit test

17 views
Skip to first unread message

Hubert Yang

unread,
Sep 27, 2009, 12:34:20 PM9/27/09
to warp-core
Hi there:

Things are all fine when I use WP in web environment, together with
Guice injection:

Injector injector = Guice.createInjector(

// persistence service module
PersistenceService.usingHibernate().across
(UnitOfWork.REQUEST).forAll(Matchers..any()).buildModule(),

// hibernate module
new HibernateModule(), // for annotation
configuration, current_session_context_class=managed

// custom module
new CustomModule()); // custom module for our
application

However, when I try to run the code in JUnit test, problem occured. I
attempt to use WorkManager but failed:

public class JUnitWorkManager implements WorkManager {

@Inject
private PersistenceService service;

public void beginWork() {
service.start();
}

public void endWork() {
service.shutdown();
}

}

in the JUint test class:

public class ILGroupServiceTest extends TestCase {

private JUnitWorkManager unitOfWork;
private MyDomainObject domainObj;
private MyService service;

public static Injector getInjector() {
Injector injector = Guice.createInjector(

PersistenceService.usingHibernate().across(UnitOfWork.REQUEST).forAll
(Matchers..any()).buildModule(),
new HibernateModule
()
);
return injector;
}

protected void setUp() throws Exception {
super.setUp();

Injector injector = getInjector();

unitOfWork = injector.getInstance(JUnitWorkManager.class);
service = TestUtils.getInjector().getInstance
(ILGroupService.class);
group = TestUtils.randomILGroup();

unitOfWork.beginWork();

}

protected void tearDown() throws Exception {
super.tearDown();
unitOfWork.endWork();
}

public void testMyMethod() {
//use the service here
}
}

When running the above junit test, ClassNotFoundException is thrown
for javax.servlet.Filter class. After adding the dependency (This is
also weird, why do I need this ?), I got exception which says:

org.hibernate.HibernateException: No session currently bound to
execution context

I do read the documents on WorkManager and custom unit of work, but
none of them give examples together with guice. Maybe some lines of
key code is much more helpful.

Any ideas for my problems? thanks.

jordi

unread,
Sep 27, 2009, 6:04:09 PM9/27/09
to warp...@googlegroups.com
I use a module and a separate properties for hibernate. Since i'm running this tests out of any container, i tell hibernate to use "thread" for session context management, and use HSQLDB for faster testing. I got the same no session bound error before this.

Here is part of my hibernate.test.properties: 

   hibernate.transaction.factory_class=org.hibernate.transaction.JDBCTransactionFactory
   hibernate.current_session_context_class=thread

And this is how i configure guice and warp: 

public class TransactionalTestModule extends AbstractModule {

@Override
protected void configure() {
Properties properties = loadTestProperties();
bind(Configuration.class).toInstance(new AnnotationConfiguration().setProperties(properties).configure());
install(PersistenceService.usingHibernate().across(UnitOfWork.REQUEST)
.forAll(Matchers.annotatedWith(Transactional.class),Matchers.any()).buildModule());
}

private Properties loadTestProperties() {
Properties properties = new Properties();
InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream ("hibernate.test.properties");
try {
properties.load(resourceAsStream);
} catch (IOException e) {}
return properties;
}

}

hope this helps!

jordi

Hubert Yang

unread,
Sep 27, 2009, 10:15:01 PM9/27/09
to warp-core
Thanks for the hint, jordi.

So the only way to solve this is to use two different hibernate config
files for web and testing, huh?

I believe there must be some way to work this out without giving two
configs so hibernate stay consistent during both testing and runtime.
Mabye the WorkManager is a good place to start, I just can't figure it
out :(

Dhanji R. Prasanna

unread,
Sep 27, 2009, 10:24:23 PM9/27/09
to warp...@googlegroups.com
No you don't need to. The only change you need to make to your original config to make it work is inject WorkManager.class into JUnitWorkManager and beginWork()/endWork() in addition to start() and shutdown().

Dhanji.

Hubert Yang

unread,
Sep 27, 2009, 11:23:48 PM9/27/09
to warp-core
Thanks for the reply, Dhanji, it's always a pleasure to hear the voice
from developer.

It looks like I'm one step to success, but always not an easy one.

I followed according to your advice and the test are all fine,
however, the console throws exception:

78546 WARN [main] org.hibernate.impl.SessionFactoryObjectFactory
- Could not unbind factory from JNDI
javax.naming.NoInitialContextException: Need to specify class name in
environment or system property, or as an applet parameter, or in an
application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext
(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:
247)
at javax.naming.InitialContext.getURLOrDefaultInitCtx
(InitialContext.java:284)
at javax.naming.InitialContext.unbind(InitialContext.java:375)
at org.hibernate.impl.SessionFactoryObjectFactory.removeInstance
(SessionFactoryObjectFactory.java:139)
at org.hibernate.impl.SessionFactoryImpl.close
(SessionFactoryImpl.java:894)
at
com.wideplay.warp.persist.hibernate.HibernatePersistenceService.shutdown
(HibernatePersistenceService.java:52)
at com.novionic.delphIL.ext.JUnitWorkManager.endWork
(JUnitWorkManager.java:22)
at com.novionic.delphIL.service.ILGroupServiceTest.tearDown
(ILGroupServiceTest.java:32)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at
org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run
(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run
(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run
(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main
(RemoteTestRunner.java:197)

Any particular reason for this error?




On Sep 28, 10:24 am, "Dhanji R. Prasanna" <dha...@gmail.com> wrote:
> No you don't need to. The only change you need to make to your original
> config to make it work is inject WorkManager.class into JUnitWorkManager and
> beginWork()/endWork() in addition to start() and shutdown().
> Dhanji.
>

Hubert Yang

unread,
Sep 27, 2009, 11:34:00 PM9/27/09
to warp-core
Never mind, I figure it out by removing the "name" attribute of
"session-factory" tag in hibernate.cfg.xml file.

Thanks a lot for the help.
Reply all
Reply to author
Forward
0 new messages