Search⌘ K
AI Features

Introduction to Building JUnit Test

Explore foundational JUnit concepts for unit testing Java applications. Understand the use of the @Test annotation, the fail method, and standard naming conventions to build meaningful tests.

JUnit is a unit testing framework for the Java programming language. JUnit has been important in the evolution of test-driven development (TDD).

Understanding the JUnit Test Bits

Here is a ready-to-run template test. Press the Run button below. The output will be shown in the terminal.

package iloveyouboss;
import static org.junit.Assert.*;
import org.junit.*;

public class ScoreCollectionTest {

	@Test
	public void test() {
		fail("Not yet Implemented");
	}
}
JUnit template test

Oh no! It’s a failure!

Not yet Implemented

Let’s look at what happened.

Important steps:

  1. The fail static method comes from the org.junit.Assert class.

  2. The @Test annotation comes from org.junit.Assert. JUnit knows to execute the method as a test because it’s marked with the @Test annotation. We can have other methods in the test class that are not tested, and as such JUnit doesn’t try to execute them.

  3. The test-class name is ScoreCollectionTest. Many teams adopt the standard of appending Test to the name of the class being tested (for now, the target class is ScoreCollection) to derive the test-class name.

Now that we are familiar with the basics, let’s write some meaningful tests in the ScoreCollectionTest!