Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 29 ยท Getting started

Why Test Automatically: JUnit, TestNG and the Java Testing Landscape

Why automated tests pay off, how the test pyramid works, where JUnit 6 and TestNG fit in the Java ecosystem, and how this track mixes real code with stand-ins.

Every program you ship is tested by someone. The only choice is who and when: you, in five seconds, while you are still thinking about the change, or a customer, in production, three weeks later. Automated tests are small programs that check your program. You write them once, and from then on a machine re-runs them after every change, in seconds, without getting bored or forgetting a case.

In the Java world, two frameworks dominate that job: JUnit and TestNG. This track teaches both. This first lesson is the map: what kinds of tests exist, where each tool fits, and how to read the rest of the track (some examples are real, some are honest stand-ins, and you will always be told which).

What an automated test buys you

A good test suite is a safety net with four benefits:

  • Fast feedback. A failing test points at the exact behavior that broke, minutes after you broke it.
  • Fearless change. You can rename, restructure and upgrade libraries because the tests tell you whether behavior survived.
  • Living documentation. A test named rejectsPercentAboveOneHundred states a rule more reliably than a comment, because it fails when the rule changes.
  • Cheaper bugs. A bug found while writing the code costs minutes. The same bug found by a user costs a support ticket, an incident review and trust.

Tests are not free. They need writing, they need maintaining, and a bad suite (slow, flaky, over-specified) can cost more than it returns. Much of this track is about writing the good kind.

The test pyramid

Not all tests are alike. They differ in how much of the system they involve, and that decides how fast, stable and informative they are.

Three layers, three trade-offs

Unit One class or function. Milliseconds. Thousands of them.
Integration Several parts together, such as code plus a database.
End to end The whole app through its real interface. Slow, few.

Wide base, narrow top.

1 / 2

The pyramid says: write many unit tests, a moderate number of integration tests, and a handful of end-to-end tests. Unit tests are cheap and precise; end-to-end tests are expensive but catch what the small tests cannot see, such as a misconfigured database URL.

Definitions you will use throughout this track:

  • A unit test checks one small piece (a class, a method) in isolation. Collaborators that are slow or unpredictable (databases, clocks, networks) are replaced by test doubles. See mocks and spies with Mockito.
  • An integration test checks that several real pieces work together, for example a repository against a real in-memory database. The Advanced Java integration testing lesson shows the Spring flavor.
  • An end-to-end (E2E) test drives the finished application the way a user would, through a browser or an HTTP API.

JUnit and TestNG are not limited to unit tests. They are general-purpose test runners: the same annotations structure a unit test, an integration test or a browser-automation script. That is why TestNG, in particular, is common in QA automation, and JUnit in application code.

Where JUnit and TestNG fit

Both frameworks do the same core job. You mark methods as tests, the framework finds and runs them, isolates failures, and reports the results to your IDE or build tool. Their personalities differ:

JUnit (Jupiter, version 6) TestNG
Origin The original Java unit-testing framework (1997 onward) Created in the mid-2000s to add features JUnit then lacked
Typical home Application and library code, Spring Boot QA and automation suites, large data-driven runs
Configuration Annotations and a build tool Annotations plus an optional testng.xml suite file
Strengths Huge ecosystem, extension model, great IDE support Groups, dependencies between tests, built-in parallel runs and data providers

JUnit 6, the current major version, needs Java 17 or later. The "Jupiter" name still describes the API you code against (org.junit.jupiter.api), and code written for JUnit 5 almost always runs unchanged on JUnit 6. TestNG keeps its own package (org.testng) and its own runner.

flowchart LR
  B["Build tool or IDE<br/>(Maven, Gradle, IntelliJ)"] --> L["JUnit Platform<br/>Launcher"]
  L --> J["Jupiter engine<br/>your @Test code"]
  L --> V["Vintage engine<br/>old JUnit 4 tests"]
  B --> T["TestNG runner<br/>(its own)"]

Build tools hand tests to the JUnit Platform, which delegates to engines; TestNG brings its own runner.

Later lessons compare the two in depth: JUnit vs TestNG and Migrating from JUnit 4.

What is real and what is a stand-in

This site's runner can execute many languages, but it cannot start Maven, install TestNG or run a browser. So this track follows a strict honesty rule. Every lesson tells you which of these three situations applies:

  1. Real, runnable. The JUnit Platform and the Jupiter API are installed on our Java runner. When a lesson runs JUnit code, you are running the real framework. The small run(...) helper at the bottom of those examples replaces what your IDE or build tool does (find test classes, call the launcher, print results).
  2. Real, read-only. TestNG, Mockito, Maven and Gradle files are shown in plain code blocks. They are accurate, but you cannot press Run on them.
  3. Stand-in. For concepts that cannot run, a short JavaScript or Node program re-implements the idea so you can see it work: how a runner discovers tests, how groups filter, how a dependency scheduler orders work. A stand-in is never the real framework. It is a model, and each lesson says so.

The rest of the lesson uses a stand-in right away.

A test runner in 25 lines

Strip the frameworks away and every test runner is the same loop: take a list of named functions, call each in a try/catch, record pass or fail, print a report. A failed assertion is just a thrown error. Run it, then change a number in a test and run again.

JS The smallest possible test runner

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Three of four pass. The failure message has the same expected ... but was ... shape you will see from real assertions, and the last line is the number a build server turns into a green or red build. Real frameworks add discovery (finding tests without a manual list), lifecycle hooks, parameterization, parallelism and good reports, but the loop at their heart is exactly this one.

Why the pyramid has that shape

The pyramid is not tradition. It is arithmetic. Suppose a unit test takes 5 ms, an integration test 400 ms and an end-to-end test 8 seconds. The example computes the wait for two suites with the same 600 tests: one shaped like a pyramid, one upside-down (many slow tests, few fast ones, sometimes called the "ice-cream cone").

JS Why test suites should be shaped like a pyramid

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The pyramid suite finishes in about two minutes; the cone takes close to an hour. Developers run a two-minute suite before every commit and an hour-long one rarely, so slow suites quietly stop protecting anything. (The timings are invented for illustration; your numbers will differ, but the ratio between layers is typical.)

How to read this track

The 29 lessons are grouped in chapters, and you can read them in order or jump to what you need:

  • Getting started (this chapter): the landscape, your first JUnit test and your first TestNG test.
  • JUnit core and advanced: assertions, lifecycle, conditions, parameterized tests, extensions, async code. All runnable.
  • TestNG core and advanced: annotations, groups, dependencies, data providers, XML suites, listeners. Real code is read-only; stand-ins make each idea runnable.
  • Comparing, tooling and quality: JUnit vs TestNG, migration, Mockito, Maven and Gradle, coverage, test design, flaky tests.
  • Projects and practice: build real test suites, write your own test framework, exercises, interview questions and a cheat sheet.

Prerequisites are modest: you should be comfortable reading Java classes and methods, and know what an exception is (see Exceptions in Java). You do not need a build tool to follow the lessons, although Running Tests with Maven and Gradle explains how real projects run them.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the test pyramid, which kind of test should you have the most of?

    Unit tests are the fastest and most precise, so they form the wide base of the pyramid. Higher layers are slower and less stable, so you keep fewer of them.
  2. 2. Which statement about JUnit and TestNG is correct?

    Both frameworks mark, find and run tests. JUnit is the standard for application code; TestNG is popular in QA automation and adds groups, dependencies and built-in parallel features.
  3. 3. What is a unit test's relationship with a database in the usual definition?

    A unit test isolates one piece. Slow or unpredictable collaborators are faked so the test stays fast and repeatable.
  4. 4. On this site, what does a JavaScript 'stand-in' for a TestNG feature represent?

    TestNG cannot run on the site's runner, so lessons use small programs that show how the concept works inside, and always say they are stand-ins.

Summary

  • Automated tests give fast feedback, make change safe, document behavior and make bugs cheaper; bad tests cost more than they give.
  • The pyramid favors many fast unit tests, fewer integration tests and very few end-to-end tests.
  • JUnit (Jupiter on the JUnit Platform) and TestNG are both test runners; JUnit dominates application code, TestNG is common in QA automation.
  • JUnit examples in this track run for real on the site's Java runner; TestNG, Mockito and build files are real but read-only, with labelled stand-ins that model the ideas.
  • Every runner is, at heart, a loop that calls test functions in a try/catch and reports.

Next: Your First JUnit Test, where you write and run real JUnit 6 tests.