My Java EE application contains EJBs which handles intermodular transactions. I need to test all these EJBs using Unit Tests. I can test the functions without intermodular dependencies. Is there a way to test EJBs without deploying it into any container?
We are having a look at arquillian. It seems to be the most common solution.
Well, another option is to use some sort of embedded EJB-Container like OpenEJB or Glassfish.
Nonetheless i would prefer the arquillian approach too.
Testing intermodular ejb-interactions looks more like some sort of integration test to me.
Therefore i would rather stick with a setup which resembles the runtime environment your EJBs will be deployed too instead of 'rebuilding' it with an embedded EJB-Container.
You might want to have a look at:
Best current framework for unit testing EJB3 / JPA
too.
Related
I recently managed to convince my mates in the project that we need testing (!). Due to the highly dynamic and flexible structure of our web application, with behavior depending of lots of parameters and permission relationships, they had rejected testing altogether, for the usual reasons (time consuming, test maintenance, etc.).
We will introduce testing at the service layer:
Web Browser -> GWT/RPC -> GWT Servlet -> RMI -> SessionEJB -> RMI -> Spring beans
Thus after the GWT Servlet.
Do people recommend to use junit? Or are there other test frameworks better suited? Any other general suggestions? Thanks
You can indeed use plain JUnit or TestNG with a mock framework to test your SessionEJB and individual Spring beans in isolation, i.e. proper Unit testing.
But since there is already a lot of code written, you'll probably find more bugs with less code using system testing or integration testing, i.e. test your complete SessionEJB and spring beans roundtrip in a test application context, with even a real database behind.
For integration and system testing, you can use DBUnit to have a fixture of test data in a database. And Spring also has a lot of test support utils. All of this things work with both JUnit and TestNG.
You should be able to JUnit your Servlets & EJBs. I suggest using some kind of mock framework (e.g. EasyMock) for your servlet context and if you are using any kind of JNDI resource or dependency injection.
As for a testing framework, I highly recommend TestNG (http://testng.org), with Mockito (code.google.com/p/mockito/). I love using both due to their ease of use. #DataProvider in TestNG helps me a lot, as well as other annotations for setting up a test before/after running. I was using JUnit before until I met TestNG at work and don't think I'll be going back anytime soon :)
Check them out, TestNG is definitely picking up some steam and gaining reputation.
I am currently having issues because the WebServiceContext is not initialised, obviously because the unit test is not within the EJB container. Is there anyway to manually create a WebServiceContext for testing purposed?
I found that actually testing the EJB within a container and making it run properly across platforms and tools (e.g. code coverage) is quite difficult at best and really slow in terms of execution performance.
Rather than executing the unit test in a container, you can focus more on the business logoc side and manually inject the needed components and run the #PostConstruct manually as part of your unit test harness then test your business logic.
You can use OpenEJB for that. Take a look at this stackoverflow thread:
Test #Webservice EJBs with WebServiceContext (using OpenEJB?)
You can also unit test in GlassFish Server 3 (it's backwards compatible with Java EE 5).
I am a little confused about integration testing of a simple EJB. If I want to test the EJB's local interface/no-interface do I need to use Arquillian? I stumbled upon Arquillian but I have never used it. I have a Maven directory structure/Glassfish and Eclipse Indigo
If I want to test the EJB's local interface/no-interface do I need to use Arquillian?
It is not necessary to use Arquillian, but there are certain things made easier when you do so.
Ordinarily, you would merely use the EJBContainer API available in EJB 3.1 for testing of EJBs in an embedded container (that runs in the same JVM as the tests). In the case of embedded Glassfish, this typically results in deployment of EJBs that are found in the classpath of the application.
Arquillian allows you to do a lot more than execute tests in a container. It manages the lifecycle of the container, thus not requiring any writing of code beyond setting the properties in the arquillian.xml file. It allows you to manage deployments to a container in a far more easier manner; using the ShrinkWrap API, one can programmatically perform different context-sensitive deployments to a container. Furthermore, injection of dependencies (test enrichment) can also be performed, so long as they're supported by Arquillian.
It would suffice to know that the embedded Glassfish container support for Arquillian, uses the same APIs that are exposed by the embedded Glassfish API; usually you might end up duplicating the work of Arquillian, except in certain unique scenarios.
If you're interested in taking a look at examples using Arquillian, this GitHub project would help.
If you use j2ee 6, you can use EJBContainer to create full ejb instanse.
http://download.oracle.com/javaee/6/api/javax/ejb/embeddable/EJBContainer.html
http://download.oracle.com/javaee/6/tutorial/doc/gkcrr.html
When you are not a fan of mocking (just like me), then you could either have a look at ejb3unit (http://ejb3unit.sourceforge.net/), or try Arquillian.
I must say I had very good experiences with "ejb3unit".
But it seems that "EJB3unit" peoject was not maintenance since 2-3 years. But supprisingly, weeks ago, there are again some activities on the ejb3unit site.
Arquillian is not so easy start with. I would say this mainly lies in the documentation, missing running examples, and good turorials.
But so long as you have made your firt Arquillian test run, Arquillian begins to shine!
Under the following link, you could find a tutorial serial on step by step setting up Arquillian:
http://milestonenext.blogspot.de/2012/12/ejb3-integration-test-with-arquillian.html
I read article saying
Testing support baked-in : Testing is
a priority and first-class citizen in
Grails. Grails promotes testing; and
provides utilities to make testing
easier — from low level unit tests to
high level functional tests. In
addition, Grails is not married to a
particular testing framework. You can
use JUnit, Spock, EasyB, Geb,
Selenium, Canoo etc. Any testing
framework can be made to work with
Grails (by writing a plugin that hooks
testing framework with Grails testing
infrastructure).
Does this mean that I can test Grails just like any other Java EE framework? Is that block of text saying nothing(like Grails have integration with jUnit) or is there anything special about Grails testing?
EDIT:
How does it compare to SeamTest?
I would say that Grails supports testing by means of a folder structure that already contains folders for unit and integration tests, and its commands help out with test writing. When you create a domain class or controller, for instance, it automatically creates test stubs for you. It also has commands to run all tests, run unit/integration tests only or run individual tests - these create reports for you automatically in the test folder.
You can also find a lot of plugins that support testing - there is a good functional test plugin that uses HtmlUnit to test actual requests. There is also a Selenium plugin.
My overall experience with Grails has been very positive and I highly recommend it as a framework.
I hope this helps.
As Matthew pointed out, the testing infrastructure is all set up. The directory layout is defined and tests can be run through the grails script.
Overall, the testing environment of grails and SeamTest aren't that different. They both have unit tests sans database, and integration tests that has the whole stack. The differences are mostly of a java vs. groovy nature.
Just like SeamTest provides a layer over TestNG, grails has a layer over JUnit, that provides similar support. grails.test.GrailsUnitTestCase and groovy.util.GroovyTestCase are good starting points to see how they compare.
In my opinion, where grails really stands out is in its mocking support. It uses groovy to provide very flexible mocking. In particular, you can dynamically override methods with mock versions directly on classes and objects, so there's no need to create mock classes. The framework provides shortcuts for mocking out the whole ORM layer, which allows you easily test higher level components without the overhead of the database.
Take a look at the manual's chapter on testing for some concrete examples.
I need to create unit tests for J2EE components like Servlets, Filters and JPA code which would call it's life cycle methods automatically and would integrate with Maven2 and Continuum and can be run from IDE itself.
Can you suggest something?
Thanks,
Vaibhav
for servlets you can use Jakarta Cactus
for database testing you can use unitils or DbUnit
Also, you can use a mocking framework like mockito or JMockIt