Skip to content
IT glossary
TestingUpdated December 3, 2026

JUnit

JUnit helps developers write and run automated tests for Java and other JVM languages. It finds test methods, runs them, and reports which checks passed or failed.

Also known as: Mockito

What JUnit does

A JUnit test turns an expectation into a check you can run again after changing the code. If a shipping rule promises free delivery from a certain order total, a test can call the shipping calculator at that threshold and compare the returned fee with zero. JUnit runs the check and reports the outcome; it doesn't decide what the correct fee should be or which inputs are worth testing.

How tests run

In modern JUnit projects, Jupiter supplies the annotations and assertions you write in test code, as well as the engine that runs those tests. The JUnit Platform provides the infrastructure that IDEs and build tools such as Maven and Gradle use to discover and launch tests through compatible engines.

A regular test method is marked with @Test. You can use @BeforeEach to prepare fresh data before each test and @AfterEach to clean up afterward. With @ParameterizedTest, you write the check once and run it separately for several inputs. Assertions describe what should happen: assertEquals(expected, actual) compares values, and assertThrows(...) checks that the supplied code throws an exception of the expected type or a subtype.

Older codebases may use JUnit 4. If a test isn't being discovered, check its import and the configured engine: JUnit 4 uses org.junit.Test, whereas Jupiter uses org.junit.jupiter.api.Test.

A boundary test

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

class ShippingTest {
    @Test
    void chargesForOrdersJustBelowFreeShippingThreshold() {
        int fee = Shipping.feeFor(2_999);
        assertEquals(500, fee);
    }
}

Suppose Shipping.feeFor takes an order total in cents, charges 500 cents below 3,000 cents, and returns zero from 3,000 cents onward. This test checks the last total that should incur a fee. A companion test at exactly 3_000 helps catch an off-by-one error that a test with a much larger order would miss.

Unit tests, integration tests, and Mockito

JUnit can run a fast unit test for a calculation or an integration test that queries a real test database. What makes a test an integration test is the set of components it exercises together, not the annotations it uses. Teams typically run individual tests in the IDE and run suites through their build tool in CI to catch regressions before merging.

Mockito is a separate library, not another name for JUnit. It can stand in for a dependency, such as a billing client, return a response you choose, and verify whether it was called. Use it when isolating that dependency makes the behavior easier to test. Avoid verifying every internal call: a refactor can change those calls without changing the behavior the test is meant to protect.

What to expect in interviews

You may be asked to test an invalid input with assertThrows, choose boundary cases, or explain how you would keep tests independent. A good answer makes the expected behavior explicit and shows how each test sets up its own data rather than depending on another test to run first.