Book Image

Test-Driven Java Development

Book Image

Test-Driven Java Development

Overview of this book

Table of Contents (17 chapters)
Test-Driven Java Development
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
8
Refactoring Legacy Code – Making it Young Again
Index

TestNG


JUnit and TestNG are two major Java testing frameworks. You already wrote tests with JUnit in the previous chapter and, hopefully, got a good understanding of how it works. How about TestNG? It was born out of a desire to make JUnit better. Indeed, it contains some functionalities that JUnit doesn't have.

The following subchapters summarize some of the differences between the two of them. We'll try not only to provide an explanation of differences, but also their evaluation in the context of unit testing with TDD.

The @Test annotation

Both JUnit and TestNG use the @Test annotation to specify which method is considered to be a test. Unlike JUnit, which requires every method to be annotated with @Test, TestNG allows us to use this annotation on a class level, as well. When used in this way, all public methods are considered tests unless specified otherwise:

@Test
public class DirectionSpec {

    public void whenGetFromShortNameNThenReturnDirectionN() {
        Direction direction = Direction...