我用于JUnit测试mockito框架并通过PortalBeanLocatorUtil.setBeanLocator(...)
-methode注入服务。我认为这显然是用弹簧配置来做到这一点。在这里你有完整的例子如何使用它。这个例子是拍那就是好,所以,因为方法简单易懂。
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 [email protected]
*/
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;
}
}
在这里,你发现PowerMock 1.4.12用的Mockito和JUnit包括依赖http://code.google.com/p/powermock/downloads/detail?name=powermock-mockito-junit-1.4.12.zip&can=2&q=
人在那里谁知道一些关于如何交易的Liferay在测试用例工作不一定,甚至一个小提示将是有用的或URL或电子书。谢谢 – 2012-05-04 05:49:47