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.
We'll cover the following...
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");
}
}Oh no! It’s a failure!
Not yet Implemented
Let’s look at what happened.
Important steps:
-
The
failstatic method comes from theorg.junit.Assertclass. -
The
@Testannotation comes fromorg.junit.Assert. JUnit knows to execute the method as a test because it’s marked with the@Testannotation. We can have other methods in the test class that are not tested, and as such JUnit doesn’t try to execute them. -
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 isScoreCollection) to derive the test-class name.
Now that we are familiar with the basics, let’s write some meaningful tests in the ScoreCollectionTest!