About Alvin Reyes

Alvin has an Information Technology Degree from Mapua Institute of Technology. During his studies, he was already heavily involved in a number of small to large projects where he primarily contributes by doing programming, analysis design. After graduating, he continued to do side projects on Mobile, Desktop and Web Applications.

JUnit Tutorial for Beginners

1. Introduction

In this post, we will discus the basics of setting up your JUnit Test cases. We’ll go step by step in creating test cases as we go along with creating our application. Before we dive into it though, why do we even need to create test cases? Isn’t it enough to just create the implementation since it’s what we are delivering anyway?

Although the actual implementation is part of the package, the JUnit Test case is a bullet proof evidence that what we wrote is what the actual requirements or functions will do. It is the concrete basis of a specific unit/function which does what it needs to do.

Knowing the impact in the stability of the application. The JUnit Test cases defines the stability of an application even after several extensions to it. If done correctly, it guarantees that the extension made to the system will not break the entire system as a whole. How does it prevent it? If the developers write clean unit and integration tests, it will report any side effects via the reporting plugins that the application uses.

Regression and Integration Testing. The effort of testing is relative to the applications size and changes done. By creating JUnit Test cases, regression and integration tests can be automated and can definitely save time and effort.

Overall, creating JUnit Test cases are definitely a must do by all developers, sadly there are still who don’t uses it’s power to it’s full extent and some just doesn’t do it. It’s sometimes a shame to think that one of the purest way of developing bullet proof code is not done by the developers. It can be because of the lack of training, experience or just pressure of not delivering the actual value (which is a problem within itself since although not part of the implementation, it’s a most valuable component of your code) but thats not an excuse especially that software are now globally taking over most of the major systems (medical, auto, planes, buildings) in the world. The stability of these systems rely on the stability of the unit test cases.

So as a precursor to being a skilled full blown developer that loves to do unit test, let’s dive into some of the beginners guide into doing it.

2. Tools

For this example, I’ll be using Java as the platform, Eclipse as the IDE, and Maven as the project management tool. If you’re not yet familiar with these tools, please visit the Java, Eclipse IDE and Maven site.

3. Step by Step Guide

3.1 Create your project

Let’s create a project first.

Figure 1.0 New Maven Project

After creating the project, you’ll be shown a project like the one below:

3.2 Create the Service Class

Majority of the developers I know starts first with creating the implementation rather than the JUnit Test case, it isn’t a bad practice at all but wouldn’t it be more concrete if we create the JUnit Test cases first based on the design then create the implementation to pass all JUnit Test case? This is the case in the TDD, one of the most successful schemes of actual software development.

Assuming we are creating a service to manage an account. We need to introduce the following service methods:

Create a new Account

Update an Account

Remove an Account

List All Account Transactions

We have an OOP design that will handle this service and so we introduce the following classes (Class Diagram).

Figure 3.0 Class Diagram

Here is the actual class that doesn’t have any implementation yet. We will create the implementation after creating the test cases for this class.

3.3 Create JUnit Test Cases

Now that we have the service placeholder, let’s create the Junit Test case for the AccountServiceImpl class. The Test cases will be the basis of your class functional aspect so you as a developer should write a solid and good test case (and not just fake it to pass).

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Receive Java & Developer job alerts in your Area

Leave this field empty if you're human:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.