I want to ask for your prefered way to test Java EE code?
I found only three project, that are trying to help to code unit tests in Java EE environment:
http://jakarta.apache.org/cactus/ : Last Published: 2009-01-18
http://www.junitee.org/ : Last Release: 2004-12-11
http://ejb3unit.sourceforge.net/ : Last Release: 2008-05-17
So I wonder,
is there any framework helping to write (j) unit test for Java EE code?
do you use embedded Java EE servers like jboss or glassfish v3?
do you mockup and inject by yourself?
Thanks a lot...
If by Unit Testing you mean... unit testing (testing a unit in isolation), then you actually don't need any particular framework since EJB3.0 are nothing more than annotated POJOs and thus can be relatively easily tested without any special fixture.
Now, if you mean something else - like Integration Testing or Functional Testing - then, yes, tools can help and simplify things (but you should really start to use the right terminology :) I'll assume that this is what you have in mind.
First, JUnitEE seems dead and obsolete and I'm not even sure it has anything for EJB3.x. Second, I'm not impressed by the Java EE 5 support of Cactus and having to deploy Cactus tests is painful (I think that Cactus was nice for J2EE 1.4 but is a bit outdated now). So this leaves us with Ejb3Unit which is in my opinion the best option, especially if you want to run out of container tests i.e. without really deploying the application (much faster).
If you want to run in container tests, then you could indeed use an embedded container and my current preference goes to GlassFish v3, even for Java EE 5 (I may be wrong but I'm pretty disappointed by the starting time of the latest JBoss releases so it isn't getting much of my attention). See the post GlassFish Embedded Reloaded, an appserver in your pocket for sample code (that you could use from your tests) or Using maven plugin for v3 embedded glassfish (if you are using maven).
Another option would be to package and deploy your application with Cargo and then run some tests against the deployed application (with Selenium or a BDD tool for example). This could be useful if you want to run end-to-end tests with a container that doesn't provide any embedded API.
So, to answer your last question, I would indeed use available tools, maybe a combination of them, for tests that are not unit tests and wouldn't mock/inject stuff myself, except if they don't cover some needs that I can't think of right now.
As you are interested in unit testing, I recommend JUnit. You can unit test the methods in the core classes. If you have difficulty in writing unit test cases using JUnit, then probably the design is not modular and it is highly coupled. First focus on your core functionality and test it using JUnit.
I've been facing the same problem of running integration tests based on JUnit in a Java EE 6 container (Glassfish v3, to be precise), and after a lot of browsing and searching, I could not find a solution that really suited me needs, so I wrote my own, now published as jeeunit on Google Code.
I wouldn't call it a test framework, it is really just a handful of classes providing the glue between JUnit and Embedded Glassfish.
The general idea is similar to Cactus, your tests run in the container and get triggered by a servlet from outside.
jeeunit supports JUnit 4, Glassfish v3, CDI and generates the standard XML JUnit reports just like Ant or Maven Surefire (in fact, I reused some code from Ant for generating the reports).
I had a requirement to test a CDI application and wrote a custom JUnit runner that runs everything outside of the web container.
http://jglue.org/cdi-unit/
It is suitable for Java SE and also supports dummy Request, Session and Conversation scopes for testing web apps.
It's small and fast, which is great when you have lots of unit tests.
Related
I have an existing java application which is developed in Netbeans this is my first major development project so i didn't think about the use of log4j & junit in first place(A good lesson learnt). since now i am at the end of the project i miss these two . is there is any tool or jar which can create automated testing & logging with minimum effort ? I guess Adding log4j is easy but what about junit ?
There is nothing to say that you cannot use Junit after you have created a project. It means that you are not making use of test driven development, but there is no reason why that is an issue once you have already created your project.
I would recommend the netbeans tutorial on exactly how to do that:
https://netbeans.org/kb/docs/java/junit-intro.html
I've recently begun working with a large, legacy enterprise Java application. It's primarily built on Websphere Commerce 6. It contains a mix of EJB 1.x and 2.x along with quite a bit of code that hooks directly into the Commerce API.
I've introduced the first unit tests while attempting to break dependancies and carefully refactor small portions of the code. We've been exploring the idea of using an integration testing framework to make the process of creating tests less fragile and time consuming.
Arquillian has been suggested as a very good option for integration testing. However, it looks geared towards more "modern" applications; most of the examples make use of Java EE 5+ and Maven. We're using J2EE and Ant. We're also currently tied to Java 1.4, and while it may be possible for us to move to Java 5, we won't be upgrading to EJB 3.x any time soon. We're also likely to stick with Ant.
With these constraints in mind, is it possible to use Arquillian? Or do better alternatives exist for integration testing legacy enterprise Java applications?
Arquillian has been suggested as a very good option for integration testing. However, it looks geared towards more "modern" applications; most of the examples make use of Java EE 5+ and Maven. We're using J2EE and Ant. We're also currently tied to Java 1.4, and while it may be possible for us to move to Java 5, we won't be upgrading to EJB 3.x any time soon. We're also likely to stick with Ant.
With these constraints in mind, is it possible to use Arquillian?
Note: I'm an Arquillian contributor. I've tried to be unbiased in my answer.
This would really depend on how your tests are executed. If you are attempting to use Arquillian's support for in-container tests, then you're unlikely to find a solution. WebSphere Commerce 6 uses WebSphere 6.0 as the underlying container, which is not supported by Arquillian at the moment. If you can hypothetically use a version of WebSphere Commerce that uses WAS 7.0 or 8.0 as the foundation, then most of my answer can be ignored, since these containers are supported.
You can attempt to run tests from the client using the #RunAsClient annotation, instead of the container, and this is more likely to succeed. Note that, you'll need to perform the deployment in some manner without a #Deployment annotated method, because of the afore-mentioned absence of support for WAS 6 in Arquillian.
If you intend to use Ant instead of Maven, then the only requirement is that all dependencies be present in the classpath. Unfortunately, there is no uber JAR or distribution for Arquillian, so for now, you'll need to know all the dependencies upfront.
Note - Building in WebSphere 6.0 support for Arquillian may not be a trivial activity, as compared to other more recent containers:
Firstly, there have to be means for deploying the archive. I'm not sure if the mechanism used in WebSphere 7 and 8 container support can be ported over.
Supporting in-container tests in Arquillian for WAS 6.0 may require supporting the Servlet 2.4 protocol for running tests. Currently, Arquillian supports Servlet Spec 2.5 and 3.0 for packaging it's ServletTestRunner. This is of course, necessary if the JMX protocol and the accompanying JMXTestRunner cannot be used.
Or do better alternatives exist for integration testing legacy enterprise Java applications?
I would normally advise folks to use a mix of Cargo and JUnit for functional testing legacy apps, but even Cargo does not appear to support WebSphere 6.0.
You might find JUnitEE to be a better fit for your needs if you are willing to package the JUnitEE TestRunner in your archive; note that JUnitEE's last release was in 2004, and the mailing list is a bit inactive, so YMMV.
I'm doing Java development in eclipse and using JUnit. My application uses an old version of a library because of platform restrictions. Is there any way I can run my unit tests with a new version of the library? How do you configure a different build path for unit testing?
To clarify for everyone below:
Here is the problem. Our platform requires a really old version of the java Servlet library. But we want to use ServletUnit (a library for testing servlets in a unit testing framework). This library will only work with newer versions for the java servlet lib. I don't care that we test with a different version of the servlet library, it outweighs the negative.
You can do this by having your unit tests in a different project, so the projects will have different build paths.
The better question is WHY you want to do this. It's a bad idea to run your tests against something other than the production code. Why not either update the library in the application, or use the old library for the tests?
In response to your edit:
If you don't care about testing with the same libraries you use in production, then you don't care about code quality or correctness. There's no point in answering this because no answer will be a good fix to your problem. Your time would be better spent upgrading your platform to use the newest version of servlets.
What apps would you use? Are there auto testing suites like autotest for ruby? What do you use and why? To be honest, I don't even know how to write tests, when, or why. I'd like to learn though, I know that it will make me a better developer.
Our team uses Netbeans, not eclipse, although I'm going to still google eclipse responses to see if they are implemented as a Netbeans solution as well.
There are 2 most popular frameworks for unit tests: JUnit and TestNG. Both are annotation based. To create test you have to create class and mark each method that performs test using annotation #Test.
JUnit is older and have more extensions (DBUnit, Cactus etc). TestNG has much more annotations. Very important feature of TestNG is ability to create test groups using annotations.
Yet another group of tools you will probably need is mocking tools (EasyMock, EasyMock etc.)
There are a bunch of testing frameworks that are popular. JUnit is pretty good and comes by default with Eclipse. It provides an API for defining tests and doing assertions, as well as a Testrunner to execute the tests. EasyMock and Mockito work well with JUnit to provide mocking functionality so you can test components in isolation.
For continuous integration, there is Jenkins, which is free.
There are others as well.
I would use junit and possibly a mocking library like jmock.
Most of the automatic "tests" which can be done use the compiler or a code analysis tool like FindBugs.
In addition to what has already been said (JUnit, EasyMock, ...) you may also have a look at Fitnesse: it may be a good tool for full integration and acceptance tests!
Don't forget TestNG. It's the "next generation" beyond JUnit. It handles threaded tests better.
SOAP UI is the right tool for testing SOAP web services.
JMeter or Grinder for load testing.
As JUnit and Mockito was already mentioned, You can look into Infinitest or JUnit Max for autotesting.
http://infinitest.github.com/
http://junitmax.com/
If you are looking for something that implements continuous testing I can recommend two free products:
For a developer during work in Eclipse/IntelliJ IDE:
http://infinitest.github.com/
Infinitest is an Eclipse/IntelliJ plugin that runs your test continuously in the background while you are developing your code.
For a team:
http://hudson-ci.org/
or
http://jenkins-ci.org/
are great continuous integration servers that can do builds and run tests continuously.
Been writing junits for over 7 years now and I highly recommend spock for all your testing needs: unit and integration testing, mocking, end-to-end testing, data driven testing etc
I need to do an integration tests on my web application on each build.
Currently I have a set of JUnit tests, which tests various parts of an application before it constructed into war. What I need to do now is to test if application is in good shape after it is deployed into Web container.
The application is written using Spring framework, I've read a lot of docs about Spring integration testing, but all they talk is about testing of integration with databases etc. I've read about Cactus, but the info is pretty scarse as well.
Any pointers for a good tutorials and source code are appreciated.
Details of a software used by app: Spring 2.5, Hibernate 3.2, Maven 2.2, Tomcat 6.0
Many thanks!
You have two tasks to solve: A) Get the container running in an automated (maven based) process and B) run some tests against it.
We use selenium and maven-jetty-plugin for testing a fairly similar application setup. Works like a charm. You might find the same kind of plugins for tomcat, or just run in jetty.
The advantage of selenium is that it allows you to test the application in a very real environment with a real browser. You may find that tools that skip the "real browser" part are simpler to start with.