- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的问题:
我可以成功测试 CRUD 服务操作。我在做在@Before [setUp()] 上插入并在@After 上删除相同的数据[tearDown()] 但今后我需要支持交易而不是编写用于插入和删除的代码。
我成功获取了我的实体的单个记录,但是当我触发搜索查询或尝试获取多个实体时,我得到:
com.liferay.portal.kernel.bean.BeanLocatorException: BeanLocator has not been set for servlet context MyCustom-portlet
我已经按照以下一些链接使用 Liferay 设置 Junit:
我的环境
Liferay 6.0.5 EE 与 Tomcat 捆绑在一起
使用 Junit4 的 Liferay IDE 1.4 和 Eclipse Helios
我在 eclipse 本身中使用“ant”命令运行我的测试,但没有通过键入 Alt+Shift+X, T。
如果我能了解如何使用 JUnit 事务(或至少了解它在 liferay 中的工作方式)以及如何解决 BeanLocatorException
(或者至少为什么会被抛出)
任何帮助将不胜感激。
最佳答案
我使用 JUnit 测试 mockito 框架并通过 PortalBeanLocatorUtil.setBeanLocator(...)
方法注入(inject)服务。我认为这显然是通过 Spring 配置来做到这一点。 Here你有完整的例子如何使用它。示例已拍摄,这样很好,因为该方法简单易懂。
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;
}
}
你也可以在没有 mockito 框架的情况下使用这种方法,然后你必须手动创建像 MockBeanLocator
这样的模拟类。
使用 PowerMock 的方法
使用 PowerMock,您可以放弃 BeanLocator
,因为 PowerMock 允许覆盖静态方法。这是 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;
}
}
在这里您可以找到带有 Mockito 和 JUnit 的 PowerMock 1.4.12,包括依赖项 http://code.google.com/p/powermock/downloads/detail?name=powermock-mockito-junit-1.4.12.zip&can=2&q=
关于java - 测试自定义插件 portlet : BeanLocatorException and Transaction roll-back for services testing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9701539/
在今天的一次采访中,我提到我在 portlet 中做了一些 jsf 工作。面试官询问 portlet-bridge 的具体功能。我从来没有研究过它,只是使用它,因为我被告知它会使 jsf 在 port
我是 Portlet 应用程序开发的新手,我对 PortletContext 对象有一个疑问。如果在单个 Portlet 应用程序中配置了多个 Portlet,则每个 PortletApllicati
我正在尝试修复分配给自定义 portlet 管理器的日历 portlet 的月份导航。这个管理器是从一个特定的浏览器页面模板调用的: 不幸的是,经理没有为我呈现带有散列的包装器,因此我尝试手动将 k
使用 documentation在 plone.org 和论坛中的一些网站上,我能够在 Plone 4.0.8 中的内容下方获得一个自定义 portlet 管理器。实际上,目标是让 4 个自定义管理器
是否可以在一个 portlet 中创建几个 portlet?我的意思是多级 portlet。 最佳答案 是的。您可以使用 Nested Portlet为此目的。这是其他 portlet 的布局容器。您
当我在 tomcat 服务器上的 liferay 6.0.6 中一个接一个地部署两个 portlet 时,第二个部署的 portlet 正在取消部署第一个部署的 portlet,反之亦然: 堆栈跟踪:
我正在使用 Spring MVC Portlet 开发一个 portlet 并部署在 Liferay 中。我对这个 portlet 的意图是显示一个包含一些重要信息的树。为了实现这个目标,我需要请求服
所以我用谷歌搜索了一整天,得到的答案很少,但他们都失败了。更不用说所有的解决方案都 > 3 岁了。我正在使用 LR 6.1 CE。将不胜感激简单的工作示例,因为其他答案总是让我获得空值。 最佳答案 有
我正在使用portlet function of JQuery UI创建一个基本的门户页面。 JQuery UI 页面上的示例显示了一个非常简单的示例,其中包含 3 列和适合这些列的固定大小的 por
对于由 Liferay EE 6.0 SP1 提供支持的网站,有时某些页面需要“共享”某些 portlet 的同一实例,但其他页面将拥有自己的实例。 例如(人为设计,但希望能够说明问题),考虑一个带有
我是 Liferay portlet 的新手。谁能告诉我如何根据另一个 portlet 的事件显示或隐藏一个 portlet。我只想在ice:spaces 中编码,而不是任何其他方式。如果有人知道这个
父 portlet 有一个表单,我填写但尚未提交。 然后,我从这个父 portlet 中单击一个链接来打开一个弹出 portlet,在弹出窗口中填写表单并提交值。 该值现在应该在父 portlet 中
我有一个页面,其中包含一个 portlet(portlet A)和一个用于打开弹出窗口的按钮。 Liferay.Util.openWindow(..) 在弹出窗口中,我有另一个 portlet(por
我的问题是如何从一个 portlet 转到另一个。 我的场景是这样的: 用户可以查看特定 portlet 的 view.jsp。当他单击按钮时,页面将显示另一个 portlet 的 view.jsp。
例如,我有三个 portlet:portletA、portletB 和 portletC。 portletA 显示在左侧,它有两个链接 linkB 和 linkC。 如果我点击 linkB 然后 po
为什么我要在 tomcat 和 gwt 之上使用 java portlet?portlet 是否会减少或不需要我使用 jsp 和 jsf?Jboss 是否已成为 portlet 进化文化的一部分? J
我需要自定义哪个 portlet 出现在“添加更多 portlet 菜单”中,如下图所示: 我需要根据登录的用户或站点(社区)限制此菜单中显示的 portlet 数量。这样,站点所有者或站点管理员将能
我如何重新排序 portlet,以便继承的父 portlet 排在当前项的 portlet 之前? 最佳答案 Mathias 的解决方案似乎更简单;无论如何,如果您不想安装第三方产品,我想保留此文档。
在 Plone 4.1 中禁用站点范围内的 portlet 类型的最佳方法是什么?默认设置提供约 10 个 portlet 类型,但站点用户仅拥有少数几种用例(静态文本、新闻)。 最佳答案 Portl
我正在将最基本的 Portlet 部署到 Liferay: public class FirstPortlet extends GenericPortlet { @RenderMode(nam
我是一名优秀的程序员,十分优秀!