Unit Testing

In the last chapter, we learned how to create a web application that uses a database. In this chapter, we will learn how to write and execute unit tests in Eclipse for JEE applications. We will cover the following topics in this chapter:

  • Creating and executing unit tests using Eclipse JEE
  • Executing unit tests from Eclipse IDE
  • Mocking external dependencies for unit tests
  • Calculating unit test coverage

Testing the software that you develop is a very important part of the overall software development cycle. There are many types of testing; each one has a specific purpose, and each one varies in scope. Some examples of testing are functional testing, integration testing, scenario testing, and unit testing.

Of all these types, unit tests are the narrowest in scope and are typically coded and executed by developers. Each unit test is meant to test a specific and small piece of functionality (typically, a method in a class), and is expected to execute without any external dependencies. Here are some of the reasons why you should write efficient unit tests:

  • To catch bugs early. If you find a bug in functional or integration testing, which have a much wider scope of testing, then it might be difficult to isolate the code that caused the bug. It is much easier to catch and fix bugs in unit testing, because unit tests, by definition, work in a narrower scope, and if a test fails, you will find out exactly where to go to fix the issue.
  • Unit tests can help you catch any regression that you might have introduced when editing the code. There are good tools and libraries available for automating the execution of unit tests. For example, using build tools such as Ant and Maven, you can execute unit tests at the end of a successful build, so that you will immediately find out if the changes you have made have broken any previously working code.

As mentioned previously, writing unit tests and executing them is typically the responsibility of the developer. Therefore, most IDEs have good built-in support for writing and executing unit tests. Eclipse JEE is no exception. It has built-in support for JUnit, which is a popular unit testing framework for Java.

In this chapter, we will see how to write and execute JUnit tests for the Course Management web application that we built in Chapter 4, Creating JEE Database Applications. However, first, here is a quick introduction to JUnit.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.85.135