Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8345985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:51:45+00:00 2026-06-09T06:51:45+00:00

My Problems: I can test successfully for CRUD services operation. I was doing an

  • 0

My Problems:

  1. 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.

  2. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:51:46+00:00Added an answer on June 9, 2026 at 6:51 am

    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.

    package mst.unittest.example;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.junit.Before;
    import org.junit.Test;
    
    import com.liferay.portal.kernel.bean.BeanLocator;
    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
    import com.liferay.portal.kernel.exception.PortalException;
    import com.liferay.portal.kernel.exception.SystemException;
    import com.liferay.portal.model.User;
    import com.liferay.portal.service.UserLocalService;
    import com.liferay.portal.service.UserLocalServiceUtil;
    
    import static org.junit.Assert.*;
    
    import static org.mockito.Mockito.*;
    
    /**
     * @author mark.stein.ms@gmail.com
     */
    public class MyUserUtilTest {
    
    
        private BeanLocator mockBeanLocator;
    
        @Before
        public void init()  {
            //create mock for BeanLocator, BeanLocator is responsible for loading of Services
            mockBeanLocator = mock(BeanLocator.class);
            //... and insert it in Liferay loading infrastructure (instead of Spring configuration)
            PortalBeanLocatorUtil.setBeanLocator(mockBeanLocator);
        }
    
        @Test
        public void testIsUserFullAge() throws PortalException, SystemException, ParseException {
            //setup
            SimpleDateFormat format = new SimpleDateFormat("yyyy_MM_dd");
            Date D2000_01_01 = format.parse("2000_01_01");
            Date D1990_06_30 = format.parse("1990_06_30");
            UserLocalService mockUserLocalService = mock(UserLocalService.class);
            User mockUserThatIsFullAge = mock(User.class);
            when(mockUserThatIsFullAge.getBirthday()).thenReturn(D1990_06_30);
            User mockUserThatIsNotFullAge = mock(User.class);
            when(mockUserThatIsNotFullAge.getBirthday()).thenReturn(D2000_01_01);
            //overwrite getUser(...) methode so that wir get mock user-object with mocked behavior
            when(mockUserLocalService.getUser(1234567)).thenReturn(mockUserThatIsFullAge);
            when(mockUserLocalService.getUser(7654321)).thenReturn(mockUserThatIsNotFullAge);
    
            //load our mock-object instead of default UserLocalService
            when(mockBeanLocator.locate("com.liferay.portal.service.UserLocalService")).thenReturn(mockUserLocalService);
    
    
            //run
            User userFullAge = UserLocalServiceUtil.getUser(1234567);
            boolean fullAge = MyUserUtil.isUserFullAge(userFullAge);
    
            //verify
            assertTrue(fullAge);
    
            //run
            User userNotFullAge = UserLocalServiceUtil.getUser(7654321);
            boolean notfullAge = MyUserUtil.isUserFullAge(userNotFullAge);
    
            //verify
            assertFalse(notfullAge);
        }
    
    }
    
    class MyUserUtil {
    
        public static boolean isUserFullAge(User user) throws PortalException, SystemException {
            Date birthday = user.getBirthday();
            long years = (System.currentTimeMillis() - birthday.getTime()) / ((long)365*24*60*60*1000);
            return years > 18;
        }
    
    }
    

    You can use this approach also without mockito framework, then you must create the mock-classes like MockBeanLocator manually.

    Approach with PowerMock

    With PowerMock you can to abdicate BeanLocator because PowerMock allows to override static methods. Here the same example with PowerMock:

    package mst.unittest.example;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.powermock.api.mockito.PowerMockito;
    import org.powermock.core.classloader.annotations.PrepareForTest;
    import org.powermock.modules.junit4.PowerMockRunner;
    
    import com.liferay.portal.kernel.exception.PortalException;
    import com.liferay.portal.kernel.exception.SystemException;
    import com.liferay.portal.model.User;
    import com.liferay.portal.service.UserLocalServiceUtil;
    
    import static org.junit.Assert.*;
    
    import static org.mockito.Mockito.*;
    
    /**
     * @author Mark Stein
     *
     */
    
    @RunWith(PowerMockRunner.class)
    @PrepareForTest(UserLocalServiceUtil.class)
    public class LiferayAndPowerMockTest {
    
        @Test
        public void testIsUserFullAge() throws PortalException, SystemException, ParseException {
            //setup
            SimpleDateFormat format = new SimpleDateFormat("yyyy_MM_dd");
            Date D2000_01_01 = format.parse("2000_01_01");
            Date D1990_06_30 = format.parse("1990_06_30");
            User mockUserThatIsFullAge = mock(User.class);
            when(mockUserThatIsFullAge.getBirthday()).thenReturn(D1990_06_30);
            User mockUserThatIsNotFullAge = mock(User.class);
            when(mockUserThatIsNotFullAge.getBirthday()).thenReturn(D2000_01_01);
    
            //overwrite getUser(...) by UserLocalServiceUtil  methode so that wir get mock user-object with mocked behavior
            PowerMockito.mockStatic(UserLocalServiceUtil.class);
            when(UserLocalServiceUtil.getUser(1234567)).thenReturn(mockUserThatIsFullAge);
            when(UserLocalServiceUtil.getUser(7654321)).thenReturn(mockUserThatIsNotFullAge);
    
            //run
            boolean fullAge = MySecUserUtil.isUserFullAge(1234567);
    
            //verify
            assertTrue(fullAge);
    
            //run
    
            boolean notfullAge = MySecUserUtil.isUserFullAge(7654321);
    
            //verify
            assertFalse(notfullAge);
        }
    
    }
    
    class MySecUserUtil {
    
        public static boolean isUserFullAge(long userId) throws PortalException, SystemException {
            User user = UserLocalServiceUtil.getUser(userId);
            Date birthday = user.getBirthday();
            long years = (System.currentTimeMillis() - birthday.getTime()) / ((long)365*24*60*60*1000);
            return years > 18;
        }
    
    }
    

    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=

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a recurring problem in templating projects. I can't really test my work
I'm having a problem getting this test case to work. Can anyone point me
we're considering exposing some C# types to C++ clients via COM. What problems can
I have my program that can draw rectangles. I have two problems I can't
I realized that I can have problems with single quotes in php arrays: <?php
Do you know where I can find some practice SQL problems where I can
I have a few thoughts on this but I can see problems with both.
Eclipse can be configured to ignore unavoidable generic type problems when setting the preferences
Can anyone explain me how to overpass these problems? Description Resource Path Location Type
Can anybody put lime light on what possible problems might emerge if an application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.