Tag Archives: j2ee

Tools For Unit Testing Java Web Applications

There are a lot of ways to do unit tests, but sometimes unit testing becomes difficult. This could be because we need to test a Struts action or we do not want the unit tests to hit the database. So, the question is what tools can we use for testing java web applications? This is not meant as a complete and thorough overview, just some of the tools we know of or have looked into at some time. In many cases, you will find that the testing code is very repetitive, so we may end up building frameworks on top of these frameworks. I am sure I missed something or maybe even your favorite, so just leave me a comment.

Basic Java Testing – JUnit and TestNG are the most popular tools for basic java unit testing. JUnit typically has a small advantage as it has been around longer and is included with most packages of Eclipse. However, I do not think one is really better than the other, they are just different.

Mock Objects – EasyMock and jMock are popular mock objects frameworks. Sometimes you have to simulate the way another system works, and mock objects are perfect for this. You can even create your own mock object, but the frameworks make it easy to avoid extra code.

Database Interaction – DBUnit is really the only decent framework I have seen. It allows you to insert data before the test is run and cleans up after the test is complete. However, capturing the appropriate data for a test is not simple for anything but the most trivial applications.

User Interface Testing – UI testing is notoriously difficult and many applications skip it entirely. If you are testing your generated html, you will want to look at the following tools and determine which best suits your needs. This is a special case, as there is no “one true way” to test the user interface. These are all free tools as I am a fan of keeping things as cheap as possible if the tools are good enough.

* Selenium – Maybe the most complete package of the bunch and supports testing in IE, FireFox and Safari. Your unit tests are written in java, so you just need to learn the API. They also have interesting server options where you can run your tests across servers. You can even record tests using a FireFox add-on.
* Canoo WebTest – This is another framework that can record tests with a FireFox add-on. The tests for WebTest are typically written in xml files. This gives you the option to build a library of fragments of functionality to include in various tests. You can also write your tests in Groovy or use the Java API directly. These can also be run directly from within Ant.
* Watij – This is a port of the popular Ruby testing framework, Watir. The tests are written in Java using their API. They also include BeanShell so that you can interactively test your code. This is a really interesting option if you are not sure what a unit test should look like initially.
* JWebUnit – This is more of a meta-framework, where it can build upon HtmlUnit or Selenium. HtmlUnit is fairly good by itself, but sometimes a framework is just too low-level for some people or people find that they keep doing the same things and decide to build upon the framework. There is one problem with JWebUnit, the documentation is very sparse, so the learning curve ciould be problematic.
* Cactus – This is another meta-framework, but this is from Apache. It is meant as a wrapper for testing an entire application from basic java testing, servlet testing and down to the user interface testing. Due to this meta concept, the documentation can be a bit vague. However, I have heard good things from those people that spent the time to learn more. Disappointingly, I have not had the time to dive into it much.

There are a lot of other tools out there as well. FitNesse is very popular, but it takes a very distinct approach to how unit tests are built. For Struts applications, there is a framework called StrutsTestCase that is immensely helpful as well. For a big list of testing and related software, take a look at XProgramming.com. They list testing frameworks for almost every language. Lastly, it is always a good idea to know how well your software is tested. This is not a question of whether all of your tests pass, but how much of the code is actually tested. My favorite test code coverage tool is Cobertura. It is fairly simple to use and the reports are generated in a similar manner to how JUnit generates reports.

So, if you know of a good free java testing tool, drop me a note in the comments. Sometimes a good tool may not be getting enough publicity or I may not have had time to look at it. In any case, keep testing until you have good test coverage. And remember the testing mantra, “Red, Green, Refactor”


10 Questions to Ask When Choosing a Framework

When do you need a framework and when do you decide to roll your own? Ask yourself the following questions.

  1. What is my target compile and deploy version? In the java world it is very common to have a target version of 1.2. This reduces the number of java frameworks that you may use. This is quite typical of many corporate environments as they take much longer to upgrade various parts of their IT infrastructure.
  2. What functionality do I need at the minimum? If you do not need much functionality, it may not be worth the time to even research frameworks. As a consultant, I saw some firms do a full month-long framework evaluation when they only needed a servlet filter.
  3. Are there “bonus” features that a framework may give us? Some frameworks are large enough to have many more features than you are immediately looking for. Spring is a perfect example of this. You may be looking at Spring for the dependency injection portion, and as a bonus you pick up the MVC features in the framework.
  4. Do we have any knowledge of the framework? In some shops, this is the most important question to ask. If there is no knowledge of the framework, you will spend a lot of time researching its capabilities instead of actually using it. The amount of time spent maintaining code written to use the framework is also much larger than normal.
  5. What is the cost of knowledge gathering and research for the framework? This is a question for the managers reading this post. If the developer needs a month to research the frameworks and determine their suitability, then that is very expensive. This cost is increased when you realize that mostly senior level developers are going to do the evaluations.
  6. What is the cost of developing an internal framework that does the minimal feature set? If you do not need a lot of functionality, is it “simpler” and “cheaper” to roll your own? In many cases you can have a senior level developer design the framework and get help from junior developers for implementation. This also adds to the internal technical knowledge of the team.
  7. How active is the framework community? If you have a question on how to use something, the community forums or blogs of interest are invaluable. If there is a framework user mailing list, look at how many messages are posted and how quickly responses appear. This is your development support team, so you need to have fairly quick response.
  8. Are there consultants readily available if you hit a roadblock? Sometimes companies will have specific expertise in a framework. If the framework is popular, then many consulting firms will have someone with some level of knowledge.
  9. Are there time constraints on the framework decision? Do you need a choice yesterday? If there is little time to make a decision, popularity of the framework becomes important. Popularity will typically give you the answers to a few of the other questions in this list. Also, smaller frameworks are normally simpler and quicker to integrate into your code.
  10. Would multiple smaller frameworks or various best of breed fit better than one monolithic framework? Some people get concerned with a monolithic choice like Spring or .Net. There are a wealth of frameworks available and some have very targeted functionality. For example, SiteMesh is a small framework that allows you to “skin” your website very easily. You can setup a SiteMesh example site and understand what it does within an hour.

What did I miss? Are there any other questions that you think are important?

JAVA Coding Standards

Code conventions are important to programmers for a number of reasons:

  • 80% of the lifetime cost of a piece of software goes to maintenance.
  • Hardly any software is maintained for its whole life by the original author.
  • Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly.
  • If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create.

We can find a lot of information about the coding standards at the link below.