My Problems:
-
I can test successfully for CRUD services operation. I was doing
an insert on @Before [setUp()] and delete of same data on @After
[tearDown()] but going forward I would need to support Transactions
rather than writing code for insert and delete. -
I am successful in fetching single records of my entity but when I fire a search query or try to fetch more than one of my entities I get:
com.liferay.portal.kernel.bean.BeanLocatorException: BeanLocator has not been set for servlet context MyCustom-portlet
I have followed some of the following links to set-up Junit with Liferay:
- Liferay wiki – How to use Junit to test Service in Portlets
- SO – Unit Testing in Liferay
- SO – Junit Testing DAOs rollback or Delete
My Enviroment
-
Liferay 6.0.5 EE bundled with Tomcat
-
Eclipse Helios with Liferay IDE 1.4 using Junit4
-
I am running my tests with "ant" command in eclipse itself but not
through typing Alt+Shift+X, T.
It would be really helpful if I can get some idea as to how to go about using Transactions with JUnit (or at least some ideas as to how it works in liferay) and how to resolve the BeanLocatorException (or at least why would it be thrown)
Any help will be greatly appreciated.
I use for JUnit testing mockito framework and inject the services over
PortalBeanLocatorUtil.setBeanLocator(...)-methode. I think that is clearly as to do this with spring configuration. Here you have full example how it can be used. The example is shot and that is good so, because the approach is simple and understandable.You can use this approach also without mockito framework, then you must create the mock-classes like
MockBeanLocatormanually.Approach with PowerMock
With PowerMock you can to abdicate
BeanLocatorbecause PowerMock allows to override static methods. Here the same example with PowerMock:Here you found PowerMock 1.4.12 with Mockito and JUnit including dependencies http://code.google.com/p/powermock/downloads/detail?name=powermock-mockito-junit-1.4.12.zip&can=2&q=