有誰知道如何爲Liferay portlet運行單元測試嗎?我發現了很多關於它的帖子(例如http://agile-reflections.opnworks.com/2010/06/portlet-unit-testing-with-liferay-6.html),但沒有一個可以工作。Liferay中的單元測試
回答
You need to have some third party libraries on classpath。
關鍵是甚至在類路徑中有portal-impl.jar和其他入口依賴關係,並且有InitUtil.initWithSpring(boolean);
加載在spring.congigs屬性的spring-ext.properties中指定的core spring xml配置,只有您需要的那些服務。您可能不需要門戶服務,只需要portlet服務,但這是一個問題,因爲服務構建器生成的portlet服務使用門戶服務。
使用服務構建器只需要熟知spring和classloading。
但是在這之前你需要了解基礎設施。有相當多的黑客需要...像
BeanLocator beanLocator = new BeanLocatorImpl(PortalClassLoaderUtil.getClassLoader(), ac);
PortletBeanLocatorUtil.setBeanLocator("portlet", beanLocator);
我想了解這個代碼:)我會評論它後,得到的東西工作。 – brandizzi 2011-08-08 02:07:49
單元測試當使用ServiceBuilder時,Liferay portlet相當複雜。
原因是它會生成相當繁重的服務,這些服務不僅包含對Portlet中的Bean的引用,而且還包含對由ServiceBuilder生成的Portal Bean的引用。有如InitUtil.init();等工具。它可以讓你至少實例化和使用ServiceBuilder實體...不是EntityServices。爲此你必須使用SpringUtil.loadContext();需要
System.setProperty("external-properties", "testing.properties");
其中testing.properties包含:
spring.configs=META-INF/ext-spring.xml,\
META-INF/base-spring.xml,\
META-INF/dynamic-data-source-spring.xml,\
META-INF/infrastructure-spring.xml,\
META-INF/shard-data-source-spring.xml,\
META-INF/hibernate-spring.xml,\
META-INF/portlet-spring.xml
這些都是要加載測試應用程序上下文春天的定義。這一切都可以,但是來自portlet-spring.xml的bean是那些包含像ResourceService,UserLocalService,CounterLocalService等Portal bean定義引用的大型服務,你甚至不得不加載並相信我,這不是那麼容易, d必須加載相當多的其他東西。
答案:
事實是,你很可能不會有單元測試的portlet SB服務,從來沒有。它們代表具有持久性和服務層的實體。一些不被測試的東西。你只需要嘲笑他們並存根他們的方法,對吧?
對於模擬junit和集成測試的最佳方式是不在應用程序中使用* LocalServiceUtil靜態類,因爲它幾乎不可調整。
你只需要創建一個的FactoryBean:此Portlet
public class PortalFactoryBean implements FactoryBean {
private Class type;
public void setType(final Class type) {
this.type = type;
}
@Override
public Object getObject() throws Exception {
return PortalBeanLocatorUtil.locate(type.getName());
}
@Override
public Class getObjectType() {
return type;
}
}
public class PortletFactoryBean implements FactoryBean {
private Class type;
public void setType(final Class type) {
this.type = type;
}
@Override
public Object getObject() throws Exception {
return PortletBeanLocatorUtil.locate(type.getName());
}
@Override
public Class getObjectType() {
return type;
}
}
<bean id="somePortalBean" class="example.spring.PortalFactoryBean" lazy-init="true">
<property name="type" value="com.liferay.some.util.SomeService"/>
</bean>
<bean id="somePortletBean" class="example.spring.PortletFactoryBean" lazy-init="true">
<property name="type" value="com.example.SomeService"/>
</bean>
@Autowired
private SomeService somePortalBean;
寫作單元/集成測試是很容易的,對不對?您只需要創建一個用於測試的Spring上下文,你嘲笑這些服務:
使用Service Builder是值得的,但你必須有一些春天的知識和它玩了一段時間。然後它節省了很多時間,因爲它很容易維護。
這可能是矯枉過正,但如果你正在尋找具有持續集成測試企業的做法,這個博客提供了一個很好的例子:Continuous integration on Liferay: running your Selenium 2 tests on the Tomcat 6 bundle
- 1. 單元測試中的性能測試
- 2. Angular中的單元測試
- 3. xcode5中的單元測試
- 4. 單元測試中的SerializationException
- 5. Python中的單元測試
- 6. 單元測試中的HashSet
- 7. PyCharm中的單元測試
- 8. clojure中的單元測試
- 9. IOS中的單元測試
- 10. 單元測試中的System.Security.VerificationException
- 11. Web2py中的單元測試
- 12. JavaScript中的單元測試
- 13. XCode中的單元測試
- 14. golang中的單元測試
- 15. C#中的單元測試#
- 16. Lightswitch中的單元測試
- 17. MVC中的單元測試
- 18. C++中的單元測試
- 19. Angular中的單元測試
- 20. TDD中的單元測試
- 21. Liferay Junit-Mockito測試
- 22. 單元測試測試
- 23. CakePHP測試 - 單元測試
- 24. 單元測試
- 25. 單元測試
- 26. 單元測試
- 27. 單元測試
- 28. 單元測試
- 29. 單元測試
- 30. 單元測試
你想單元測試你自己的portlet或Liferay的內置的portlet ? – blank 2011-01-20 19:57:53
我想單元測試我自己的portlet,使用我通過Service Builder創建的服務。 – brandizzi 2011-02-01 11:58:29