[opencms-dev] Integration of DI solution and testing
Florian Hopf
mailinglists at florian-hopf.de
Fri Oct 4 10:26:26 CEST 2013
Hi,
better late than never.
On 21.09.2013 20:11, Jakub Ječmínek wrote:
> I was trying to figure out the best way how to test my OpenCMS modules
> for a while. One of the most problematic things that I have encountered
> is the fact that
> the CmsObject class is final and has no interface(but you need for
> almost everything), therefore standard mocking tools such as EasyMock or
> Mockito cannot be used.
> The only one mocking framework that is capable of this is PowerMock, but
> it is not optimal. Therefore I would like to ask if anybody tried to
> solve this problem, whether you
> have some custom mocking framework customized for OpenCMS or if there
> are plans in OpenCms development to make some architectural changes,
> that would allow
> better testability.
What I have been doing is to add the interfaces myself, not to the
CmsObject but to a custom implementation that uses the CmsObject and
provides the operations you need in your application with the additional
benefit that you can group methods in CmsObject that are called
together. This is similar to the anti corruption layer in the DDD world
which also enables you to have a lot of the interaction with OpenCms in
a central place:
http://www.markhneedham.com/blog/2009/07/07/domain-driven-design-anti-corruption-layer/
The class that implements the access to OpenCms can then be mocked using
Mockito. The implementation itself can be tested using the OpenCms
integration tests. I haven't used the newer versions of OpenCms but I
wrote something about them a while ago:
http://blog.florian-hopf.de/2010/04/using-opencmstestcase.html. I hope
nowadays it's easier to use them.
>
> Another question I have is about dependency injection. Do you use some
> library, or do you recommend some DI framework for integration wht
> OpenCms? I am used to separating my layers, for example I have DAO
> object that just
> accesses the db and on top of that I have service layer where some
> business code lives(in these days sort of standard layout).
> One problem that I have is the fact that by default OpenCms is not
> shipped with any DI framework.
I have used Spring in several OpenCms projects and there is nothing
special about it. I still use the JSPs of OpenCms to take advantage of
the FlexCache. ElFunctions are the central entry point to the Java Code,
in the function you can get hold of the application context and read
your beans. For the layer I described above we also had one project
where we declared the OpenCms access layer to be a request scoped bean
and inject the CmsObject of the user on every request. On other projects
it was passed in to the methods.
There is also a Spring MVC integration available but I didn't have a
look at it yet:
http://blog.shinetech.com/2013/04/09/integrating-springmvc-with-opencms/
When I tried to integrate Spring MVC I always struggled with the correct
way to do the forwarding in OpenCms so I can still use the FlexCache.
Regards
Florian
--
Florian Hopf
Freelance Software Developer
http://blog.florian-hopf.de
More information about the opencms-dev
mailing list