Test Driven Development

Test Driven Development (TDD) Introduction using Java in Eclipse/JUnit

Overview

Students will follow and implement a beginner level java-based Test Driven Development (TDD) approach with JUnit in the Eclipse IDE. First, they create a test case that fails, then they start creating mock code, and finally a simple piece of code that allows their test to pass.

Prerequisites

Students should be familiar with:

Programming simple Java

Developing object-oriented software (classes, methods, instances)

Test Driven Development (TDD) workflow and theory

Learning Objectives

After successfully completing this activity, the learner should be able to:

The method of how developers go about implementing code is varied. Test driven development is one such method stemming in the 2000's that has gained momentum as an accepted and research-proven method for increasing software quality, readability, and reliability. Testing software to assure that it works within particular constraints is an extension of quality assurance (QA) efforts that most professional software products enact to increase their products value. With TDD, this testing is built-in even before students write their first line of code. This means that students will have a better grasp on what they are programming and why. It means that students will have code that not only works when they first implement it, but that will continue to work when others develop next to it and run the same tests.

Directions

This activity follows a tutorial that steps you through the TDD development process in the Eclipse IDE using its integrated JUnit module. Follow the below tutorials while implementing the TDD test code in Eclipse on your own computer. Finally, you will create standard functional code that gets the tests to succeed (green JUnit bar). You will hand in this code, the associated test code, and two screenshots.

Step 1: Setup Eclipse and JUnit

First, you will need to setup and understand your coding environment. If you have not setup the Eclipse IDE for java before, read through some of the below links. You need to be able to run code and tests with the IDE. There can be some unexpected "gotchas".

It is an illustrated step-by-step walk through of creating a test and then creating the associated System Under Test (SUT) code (the code which implements the functionality that you actually want).

Create a file called "NumberRangeCounterTest.java" to put the test code into.

Create a separate file called "NumberRangeCounter.java" to put the skeleton SUT code into.

Implement the test class code and the skeleton SUT code found in the tutorial and get a failing test.

You will be marked up for commenting your code.

Take a screenshot of the failing test (red JUnit bar) and code.

Step 3: Write the code

You should now have a test case that fails and a stub/skeleton for the System Under Test (SUT) code. Now, you need to make the test succeed. This means implementing the SUT code (the code that actually implements the functionality wanted) to make the test go green in Eclipse.

Create working SUT code in the "NumberRangeCounter.java" file.

You will be marked up for commenting your code.

Take a screenshot of the succeeding test (green JUnit bar), alongside the code implemented to attain it.

(Optional self-learning) For tips and tricks on using JUnit in Eclipse, see the following:

Suggestions for Open Source Community

Suggestions for an open source community member who is working in conjunction with the instructor.

Show students any unit testing or TDD-based development you may already have in place. A real life example, with an explanation from a community developer would be a perfect way to solidify the understanding that testing is necessary for real world software.

Appendix

The below appendix includes information found while researching and creating the above Learning Activity. Instructors may find it useful if they are wanting to create their own follow-on activities or want more info.

Background Reading

The below bullets are additional background readings, books and study materials that can be used in the learning of TDD:

TDD Tutorials

Below lists 3rd party tutorials, how-to's, and learning resources that explain TDD and JUnit. They may be useful as alternative or harder secondary learning activities so that students can dive in a bit deeper.

It gives you test cases, already written, and you code to satisfy the tests

License says "This material may NOT be used as course material without prior written agreement."

Examples of OSS projects that were implemented with TDD

The below list shows actual open source projects that were created with TDD methodologies. These could be used as examples of TDD in-practice. Students could learn how TDD is done in real life, learn best practices, etc. They are divided below into the two best examples, and then a number of other weaker examples.