How do I test expected values with assertEquals()?

In JUnit, you use assertEquals() to test whether an actual value matches an expected value.

The basic syntax is:

assertEquals(expectedValue, actualValue);

The first argument is what you expect.
The second argument is what your code actually produced.

Basic Example

import org.junit.jupiter.api.Test;

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

class CalculatorTest {

    @Test
    void shouldAddTwoNumbers() {
        int result = 2 + 3;

        assertEquals(5, result);
    }
}

In this example:

assertEquals(5, result);

means:

I expect the result to be 5.

If result is 5, the test passes.
If result is anything else, the test fails.

Example with Strings

import org.junit.jupiter.api.Test;

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

class GreetingTest {

    @Test
    void shouldReturnGreetingMessage() {
        String message = "Hello, Java";

        assertEquals("Hello, Java", message);
    }
}

Using a Failure Message

You can add a custom message that appears when the test fails:

assertEquals(10, result, "The result should be 10");

Example:

import org.junit.jupiter.api.Test;

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

class MathTest {

    @Test
    void shouldMultiplyNumbers() {
        int result = 4 * 3;

        assertEquals(12, result, "4 multiplied by 3 should be 12");
    }
}

Common Mistake

Be careful with the order:

assertEquals(expected, actual);

Good:

assertEquals(100, total);

Less clear:

assertEquals(total, 100);

JUnit will still compare the values, but failure messages are easier to understand when the expected value comes first.

Example with a Method

Suppose you have this class:

class Calculator {

    int add(int a, int b) {
        return a + b;
    }
}

You can test it like this:

import org.junit.jupiter.api.Test;

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

class CalculatorTest {

    @Test
    void addShouldReturnSum() {
        Calculator calculator = new Calculator();

        int actual = calculator.add(2, 3);

        assertEquals(5, actual);
    }
}

Summary

Use assertEquals() like this:

assertEquals(expected, actual);

For example:

assertEquals(5, result);
assertEquals("Alice", name);
assertEquals(100.0, price);

assertEquals() is one of the most common JUnit assertions because it clearly checks whether your code returned the value you expected.

How do I use assertions in JUnit?

In JUnit, assertions are used to verify that your code produces the expected result. If an assertion fails, the test fails.

JUnit 5 assertions are provided by the org.junit.jupiter.api.Assertions class.

Basic Example

import org.junit.jupiter.api.Test;

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

class CalculatorTest {

    @Test
    void testAddition() {
        int result = 2 + 3;

        assertEquals(5, result);
    }
}

In this example, the test passes because 2 + 3 equals 5.

Common JUnit Assertions

assertEquals()

Use assertEquals() to check whether two values are equal.

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

assertEquals(10, 5 + 5);
assertEquals("Hello", "He" + "llo");

You can also provide a failure message:

assertEquals(10, 5 + 4, "The result should be 10");

assertNotEquals()

Use assertNotEquals() to check that two values are not equal.

import static org.junit.jupiter.api.Assertions.assertNotEquals;

assertNotEquals(10, 5 + 4);

assertTrue() and assertFalse()

Use assertTrue() when a condition should be true.

import static org.junit.jupiter.api.Assertions.assertTrue;

assertTrue(10 > 5);

Use assertFalse() when a condition should be false.

import static org.junit.jupiter.api.Assertions.assertFalse;

assertFalse(10 < 5);

assertNull() and assertNotNull()

Use assertNull() to check that a value is null.

import static org.junit.jupiter.api.Assertions.assertNull;

String name = null;

assertNull(name);

Use assertNotNull() to check that a value is not null.

import static org.junit.jupiter.api.Assertions.assertNotNull;

String name = "Kode Java";

assertNotNull(name);

assertSame() and assertNotSame()

Use assertSame() to check whether two references point to the same object.

import static org.junit.jupiter.api.Assertions.assertSame;

String text = "Java";
String sameText = text;

assertSame(text, sameText);

Use assertNotSame() when two references should not point to the same object.

import static org.junit.jupiter.api.Assertions.assertNotSame;

String first = new String("Java");
String second = new String("Java");

assertNotSame(first, second);

Note that assertEquals() checks object equality, while assertSame() checks object identity.

assertArrayEquals()

Use assertArrayEquals() to compare arrays.

import static org.junit.jupiter.api.Assertions.assertArrayEquals;

int[] expected = {1, 2, 3};
int[] actual = {1, 2, 3};

assertArrayEquals(expected, actual);

assertThrows()

Use assertThrows() to verify that a block of code throws an expected exception.

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;

class NumberParserTest {

    @Test
    void testInvalidNumber() {
        assertThrows(NumberFormatException.class, () -> {
            Integer.parseInt("abc");
        });
    }
}

You can also inspect the thrown exception:

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

NumberFormatException exception = assertThrows(
        NumberFormatException.class,
        () -> Integer.parseInt("abc")
);

assertEquals("For input string: \"abc\"", exception.getMessage());

assertAll()

Use assertAll() to group multiple assertions together. JUnit will run all assertions and report all failures.

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class UserTest {

    @Test
    void testUserDetails() {
        String name = "Alice";
        int age = 25;

        assertAll(
                () -> assertEquals("Alice", name),
                () -> assertEquals(25, age),
                () -> assertTrue(age >= 18)
        );
    }
}

Without assertAll(), the test stops at the first failed assertion.

fail()

Use fail() when a test should fail explicitly.

import static org.junit.jupiter.api.Assertions.fail;

fail("This test should not reach this point");

This is often useful inside conditional logic or exception handling.

Complete Example

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class StringUtilsTest {

    @Test
    void testStringAssertions() {
        String text = "JUnit";

        assertEquals("JUnit", text);
        assertNotEquals("TestNG", text);
        assertTrue(text.startsWith("J"));
        assertFalse(text.isEmpty());
        assertNotNull(text);
    }

    @Test
    void testArrayAssertions() {
        int[] numbers = {1, 2, 3};

        assertArrayEquals(new int[]{1, 2, 3}, numbers);
    }

    @Test
    void testExceptionAssertion() {
        assertThrows(NumberFormatException.class, () -> {
            Integer.parseInt("not-a-number");
        });
    }
}

Using Static Imports

Most JUnit tests use static imports so assertions can be written directly:

import static org.junit.jupiter.api.Assertions.*;

Then you can write:

assertEquals(5, result);
assertTrue(result > 0);
assertThrows(Exception.class, () -> someMethod());

Instead of:

Assertions.assertEquals(5, result);
Assertions.assertTrue(result > 0);
Assertions.assertThrows(Exception.class, () -> someMethod());

Summary

Common JUnit assertion methods include:

Assertion Purpose
assertEquals(expected, actual) Checks that two values are equal
assertNotEquals(unexpected, actual) Checks that two values are not equal
assertTrue(condition) Checks that a condition is true
assertFalse(condition) Checks that a condition is false
assertNull(value) Checks that a value is null
assertNotNull(value) Checks that a value is not null
assertSame(expected, actual) Checks that two references point to the same object
assertNotSame(unexpected, actual) Checks that two references do not point to the same object
assertArrayEquals(expected, actual) Checks that two arrays are equal
assertThrows(type, executable) Checks that an exception is thrown
assertAll(executables) Groups multiple assertions
fail(message) Fails the test explicitly

In short, assertions are the main way to express what your code is expected to do in a JUnit test.

How do I use @Test in JUnit?

Using @Test in JUnit

In JUnit, @Test is an annotation that marks a method as a test method. When you run your tests, JUnit looks for methods annotated with @Test and executes them automatically.


1. Add the Correct Import

For JUnit 5, use:

import org.junit.jupiter.api.Test;

You will usually also import assertions such as:

import static org.junit.jupiter.api.Assertions.*;

2. Basic Example

import org.junit.jupiter.api.Test;

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

class CalculatorTest {

    @Test
    void addTwoNumbers() {
        int result = 10 + 15;

        assertEquals(25, result);
    }
}

Here:

  • @Test tells JUnit this method should be run as a test.
  • assertEquals(25, result) checks that the actual result is 25.
  • If the assertion passes, the test passes.
  • If the assertion fails, the test fails.

3. Common JUnit 5 Assertions

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class ExampleTest {

    @Test
    void assertionsExample() {
        String message = "Hello JUnit";

        assertNotNull(message);
        assertTrue(message.contains("JUnit"));
        assertEquals("Hello JUnit", message);
    }
}

Common assertions include:

Assertion Purpose
assertEquals(expected, actual) Checks that two values are equal
assertTrue(condition) Checks that a condition is true
assertFalse(condition) Checks that a condition is false
assertNotNull(value) Checks that a value is not null
assertThrows(...) Checks that code throws an expected exception

4. Testing Exceptions

Use assertThrows() when you expect code to throw an exception:

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;

class DivisionTest {

    @Test
    void divideByZeroThrowsException() {
        assertThrows(ArithmeticException.class, () -> {
            int result = 10 / 0;
        });
    }
}

5. JUnit 5 Test Method Rules

In JUnit 5, test methods usually:

  • Are annotated with @Test
  • Return void
  • Do not need to be public
  • Should contain assertions
  • Should have descriptive names

Example:

@Test
void shouldReturnSumWhenAddingTwoNumbers() {
    assertEquals(5, 2 + 3);
}

6. JUnit 4 vs. JUnit 5 Import

Be careful: JUnit 4 and JUnit 5 use different @Test imports.

JUnit 5

import org.junit.jupiter.api.Test;

JUnit 4

import org.junit.Test;

For new projects, JUnit 5 is generally recommended.


7. Running the Test

You can run JUnit tests from:

  • Your IDE, such as IntelliJ IDEA or Eclipse
  • Maven
  • Gradle

With Maven:

mvn test

With Gradle:

gradle test

or:

./gradlew test

Summary

Use @Test above a method to tell JUnit, “this is a test.”

import org.junit.jupiter.api.Test;

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

class MyTest {

    @Test
    void simpleTest() {
        assertEquals(4, 2 + 2);
    }
}

That method will be discovered and run by JUnit as part of your test suite.

How do I understand the basic structure of a JUnit test class?

A basic JUnit test class is just a Java class that contains one or more test methods. Each test method checks whether a small piece of code behaves the way you expect.

Here is a simple JUnit 5 example:

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class CalculatorTest {

    @Test
    void shouldAddTwoNumbers() {
        int result = 2 + 3;

        assertEquals(5, result);
    }
}

1. The Test Class

class CalculatorTest {
    // test methods go here
}

A JUnit test class is usually named after the class being tested, followed by Test.

For example:

Class Being Tested Test Class
Calculator CalculatorTest
UserService UserServiceTest
OrderRepository OrderRepositoryTest

The test class does not need a main() method. JUnit runs the tests for you.

2. The @Test Annotation

@Test
void shouldAddTwoNumbers() {
    // test code
}

The @Test annotation tells JUnit:

This method is a test method. Run it as part of the test suite.

In JUnit 5, the annotation comes from:

import org.junit.jupiter.api.Test;

3. The Test Method

A test method usually:

  1. Creates some input or test data.
  2. Runs the code being tested.
  3. Checks the result.

Example:

@Test
void shouldAddTwoNumbers() {
    int result = 2 + 3;

    assertEquals(5, result);
}

The method name should describe the expected behavior. Common naming styles include:

void shouldAddTwoNumbers()
void returnsTrueWhenPasswordIsValid()
void throwsExceptionWhenEmailIsMissing()

4. Assertions

Assertions are checks that decide whether the test passes or fails.

Common JUnit 5 assertions include:

assertEquals(expected, actual);
assertTrue(condition);
assertFalse(condition);
assertNotNull(value);
assertNull(value);
assertThrows(Exception.class, () -> {
    // code expected to throw exception
});

Example:

@Test
void shouldCheckUserName() {
    String name = "Alice";

    assertNotNull(name);
    assertEquals("Alice", name);
    assertTrue(name.startsWith("A"));
}

If all assertions pass, the test passes. If any assertion fails, the test fails.

Assertions are usually imported like this:

import static org.junit.jupiter.api.Assertions.*;

5. Basic Arrange-Act-Assert Pattern

Many test methods follow this structure:

@Test
void shouldCalculateTotalPrice() {
    // Arrange
    int price = 100;
    int quantity = 3;

    // Act
    int total = price * quantity;

    // Assert
    assertEquals(300, total);
}

Arrange

Prepare the data or objects needed for the test.

int price = 100;
int quantity = 3;

Act

Run the code you want to test.

int total = price * quantity;

Assert

Check that the result is correct.

assertEquals(300, total);

6. A Complete Basic JUnit 5 Test Class

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class StringUtilsTest {

    @Test
    void shouldConvertTextToUpperCase() {
        // Arrange
        String text = "hello";

        // Act
        String result = text.toUpperCase();

        // Assert
        assertEquals("HELLO", result);
    }

    @Test
    void shouldCheckIfTextContainsWord() {
        // Arrange
        String text = "Learning JUnit is useful";

        // Act
        boolean containsJUnit = text.contains("JUnit");

        // Assert
        assertTrue(containsJUnit);
    }
}

7. Optional Setup Method

If several tests need the same object or data, you can use @BeforeEach.

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class CalculatorTest {

    private int baseNumber;

    @BeforeEach
    void setUp() {
        baseNumber = 10;
    }

    @Test
    void shouldAddNumber() {
        int result = baseNumber + 5;

        assertEquals(15, result);
    }

    @Test
    void shouldMultiplyNumber() {
        int result = baseNumber * 2;

        assertEquals(20, result);
    }
}

@BeforeEach runs before every test method.

8. JUnit 4 vs. JUnit 5 Structure

Older JUnit 3 or JUnit 4 tests may look different.

JUnit 5 style

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class AppTest {

    @Test
    void shouldWork() {
        assertTrue(true);
    }
}

Older JUnit 3 style

import junit.framework.TestCase;

public class AppTest extends TestCase {

    public void testApp() {
        assertTrue(true);
    }
}

In modern Java projects, you will usually prefer JUnit 5 unless you are maintaining older code.

9. Typical Folder Location

In a Maven or Gradle Java project, test classes usually go under:

src/test/java

Application code usually goes under:

src/main/java

Example:

src
├── main
│   └── java
│       └── org.kodejava
│           └── Calculator.java
└── test
    └── java
        └── org.kodejava
            └── CalculatorTest.java

Summary

A basic JUnit test class usually has:

  1. A class name ending in Test.
  2. One or more methods annotated with @Test.
  3. Assertions such as assertEquals() or assertTrue().
  4. A clear structure: Arrange, Act, Assert.
  5. Optional setup methods such as @BeforeEach.

In short:

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class ExampleTest {

    @Test
    void shouldDoSomething() {
        // Arrange
        String value = "JUnit";

        // Act
        boolean result = value.contains("Unit");

        // Assert
        assertTrue(result);
    }
}

How do I add JUnit to a Gradle project?

To add JUnit to a Gradle project, add the JUnit dependency to your build.gradle or build.gradle.kts file and configure Gradle to use the JUnit Platform.

If you use Groovy Gradle: build.gradle

For JUnit 5, add:

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.10.2'
}

test {
    useJUnitPlatform()
}

If you use Kotlin Gradle: build.gradle.kts

plugins {
    java
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
}

tasks.test {
    useJUnitPlatform()
}

Example JUnit 5 Test

import org.junit.jupiter.api.Test;

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

class CalculatorTest {

    @Test
    void addsNumbers() {
        assertEquals(4, 2 + 2);
    }
}

Place test files under:

src/test/java

For example:

src/test/java/org/kodejava/CalculatorTest.java

Run Tests

From the command line:

./gradlew test

On Windows:

gradlew test

If You Need JUnit 4 Instead

Use this dependency:

dependencies {
    testImplementation 'junit:junit:4.13.2'
}

For most new Gradle projects, prefer JUnit 5 with junit-jupiter.

How do I add JUnit to a Maven project?

To add JUnit to a Maven project, you add the JUnit dependency to your project’s pom.xml, create test classes under src/test/java, and run the tests with Maven.

1. Add JUnit to pom.xml

For modern Java projects, use JUnit 5.

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.13.4</version>
        <scope>test</scope>
    </dependency>
</dependencies>

If your pom.xml already has a <dependencies> section, add only the <dependency> block inside it.

2. Configure Maven Surefire Plugin

JUnit tests are usually run by the Maven Surefire Plugin. Add this inside the <build> section of your pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.5.3</version>
        </plugin>
    </plugins>
</build>

If your project already has a <build> or <plugins> section, merge the plugin into the existing structure instead of duplicating it.

3. Create a Test Class

Maven expects test classes to be placed under:

src/test/java

Example project structure:

my-project
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── org
    │           └── kodejava
    │               └── Calculator.java
    └── test
        └── java
            └── org
                └── kodejava
                    └── CalculatorTest.java

Example class to test:

package org.kodejava;

public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}

Example JUnit 5 test:

package org.kodejava;

import org.junit.jupiter.api.Test;

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

class CalculatorTest {

    @Test
    void addShouldReturnSum() {
        Calculator calculator = new Calculator();

        int result = calculator.add(2, 3);

        assertEquals(5, result);
    }
}

4. Run the Tests

From the project directory, run:

mvn test

Maven will compile your code, compile your tests, and run any matching test classes.

Common test class naming patterns include:

*Test.java
*Tests.java
*TestCase.java

Complete pom.xml Example

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>org.kodejava</groupId>
    <artifactId>junit-maven-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.release>25</maven.compiler.release>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.13.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.5.3</version>
            </plugin>
        </plugins>
    </build>

</project>

That’s it — after adding the dependency and plugin configuration, you can start writing JUnit tests and run them with mvn test.

How do I start unit testing in Java with JUnit?

Unit testing in Java means testing small pieces of code — usually one method or one class — in isolation. The most common testing framework for modern Java projects is JUnit 5, also known as JUnit Jupiter.

This guide shows the basic steps to start writing unit tests with JUnit.


1. Add JUnit to Your Project

If you use Maven, add JUnit 5 to your pom.xml:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.11.4</version>
    <scope>test</scope>
</dependency>

You should also make sure Maven Surefire can run JUnit 5 tests:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.5.2</version>
        </plugin>
    </plugins>
</build>

If you use Gradle, add:

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.11.4'
}

test {
    useJUnitPlatform()
}

2. Create a Class to Test

Suppose you have a simple calculator class:

package org.kodejava;

public class Calculator {

    public int add(int a, int b) {
        return a + b;
    }

    public int divide(int a, int b) {
        if (b == 0) {
            throw new IllegalArgumentException("Divider cannot be zero");
        }
        return a / b;
    }
}

This class has two methods:

  • add() returns the sum of two numbers.
  • divide() divides two numbers and rejects division by zero.

3. Create a Test Class

JUnit test classes are usually placed under:

src/test/java

For the Calculator class, create:

src/test/java/org/kodejava/CalculatorTest.java

Example test class:

package org.kodejava;

import org.junit.jupiter.api.Test;

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

class CalculatorTest {

    private final Calculator calculator = new Calculator();

    @Test
    void addReturnsSumOfTwoNumbers() {
        int result = calculator.add(2, 3);

        assertEquals(5, result);
    }

    @Test
    void divideReturnsQuotient() {
        int result = calculator.divide(10, 2);

        assertEquals(5, result);
    }

    @Test
    void divideThrowsExceptionWhenDividerIsZero() {
        IllegalArgumentException exception = assertThrows(
                IllegalArgumentException.class,
                () -> calculator.divide(10, 0)
        );

        assertEquals("Divider cannot be zero", exception.getMessage());
    }
}

4. Understand the Basic JUnit Annotations

The most important annotation is:

@Test

It marks a method as a test method.

Example:

@Test
void addReturnsCorrectResult() {
    assertEquals(4, 2 + 2);
}

Common JUnit 5 annotations include:

Annotation Purpose
@Test Marks a method as a test
@BeforeEach Runs before each test method
@AfterEach Runs after each test method
@BeforeAll Runs once before all tests
@AfterAll Runs once after all tests
@Disabled Temporarily disables a test

Example using @BeforeEach:

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

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

class CalculatorTest {

    private Calculator calculator;

    @BeforeEach
    void setUp() {
        calculator = new Calculator();
    }

    @Test
    void addReturnsSumOfTwoNumbers() {
        assertEquals(5, calculator.add(2, 3));
    }
}

5. Use Assertions

Assertions check whether the result is what you expect.

Common assertions:

assertEquals(expected, actual);
assertTrue(condition);
assertFalse(condition);
assertNull(value);
assertNotNull(value);
assertThrows(ExceptionType.class, executable);

Example:

package org.kodejava;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class StringTest {

    @Test
    void stringShouldContainText() {
        String message = "Hello JUnit";

        assertNotNull(message);
        assertTrue(message.contains("JUnit"));
        assertEquals(11, message.length());
    }
}

6. Follow the Arrange, Act, Assert Pattern

A common structure for unit tests is:

  1. Arrange — prepare input data and objects.
  2. Act — call the method being tested.
  3. Assert — verify the result.

Example:

@Test
void addReturnsSumOfTwoNumbers() {
    // Arrange
    Calculator calculator = new Calculator();

    // Act
    int result = calculator.add(2, 3);

    // Assert
    assertEquals(5, result);
}

This makes tests easier to read and maintain.


7. Run the Tests

With Maven:

mvn test

With Gradle:

./gradlew test

Most IDEs also let you right-click the test class or test method and choose Run Test.


8. Naming Test Methods

Use descriptive names, so it is clear what behavior is being tested.

Good examples:

void addReturnsSumOfTwoNumbers()
void divideThrowsExceptionWhenDividerIsZero()
void loginFailsWhenPasswordIsInvalid()

Avoid vague names like:

void test1()
void testAdd()
void shouldWork()

9. What Should You Test?

Good candidates for unit tests include:

  • Business rules
  • Calculations
  • Validation logic
  • Conditional logic
  • Exception handling
  • Data transformation methods

For example, test things like:

discount is applied correctly
invalid email is rejected
zero quantity throws an exception
user cannot withdraw more than their balance

You usually do not need to unit test:

  • Simple getters and setters
  • Framework-generated behavior
  • Code with no meaningful logic
  • External services directly

10. Example: Testing a Realistic Service Class

Class to test:

package org.kodejava.order;

public class DiscountService {

    public double applyDiscount(double price, double discountPercent) {
        if (price < 0) {
            throw new IllegalArgumentException("Price cannot be negative");
        }

        if (discountPercent < 0 || discountPercent > 100) {
            throw new IllegalArgumentException("Discount must be between 0 and 100");
        }

        return price - (price * discountPercent / 100);
    }
}

Test class:

package org.kodejava.order;

import org.junit.jupiter.api.Test;

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

class DiscountServiceTest {

    private final DiscountService discountService = new DiscountService();

    @Test
    void applyDiscountReturnsDiscountedPrice() {
        double result = discountService.applyDiscount(100.0, 10.0);

        assertEquals(90.0, result);
    }

    @Test
    void applyDiscountRejectsNegativePrice() {
        IllegalArgumentException exception = assertThrows(
                IllegalArgumentException.class,
                () -> discountService.applyDiscount(-100.0, 10.0)
        );

        assertEquals("Price cannot be negative", exception.getMessage());
    }

    @Test
    void applyDiscountRejectsInvalidDiscountPercent() {
        assertThrows(
                IllegalArgumentException.class,
                () -> discountService.applyDiscount(100.0, 120.0)
        );
    }
}

For floating-point values, you can also provide a delta:

assertEquals(90.0, result, 0.001);

Summary

To start unit testing in Java with JUnit:

  1. Add JUnit 5 to your project.
  2. Put test classes under src/test/java.
  3. Mark test methods with @Test.
  4. Use assertions such as assertEquals() and assertThrows().
  5. Follow the Arrange, Act, Assert pattern.
  6. Run tests with Maven, Gradle, or your IDE.

A simple JUnit test looks like this:

import org.junit.jupiter.api.Test;

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

class CalculatorTest {

    @Test
    void addReturnsSum() {
        Calculator calculator = new Calculator();

        assertEquals(5, calculator.add(2, 3));
    }
}

How do I containerize and deploy a Java application with Docker?

Containerizing and Deploying a Java Application with Docker

A typical Java Docker workflow is:

  1. Build the Java application
  2. Package it as a JAR
  3. Create a Docker image
  4. Run the container locally
  5. Push the image to a registry
  6. Deploy it to a server or cloud platform

1. Build Your Java Application

If your project uses Maven, build it with:

mvn clean package

This usually creates a JAR file under:

target/

For example:

target/my-application.jar

If this is a Spring Boot application, the generated JAR is often executable and can be run with:

java -jar target/my-application.jar

2. Create a Dockerfile

Create a file named Dockerfile in the root of your project.

Simple Dockerfile

FROM eclipse-temurin:25-jre

WORKDIR /app

COPY target/*.jar app.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "app.jar"]

What this does

  • FROM eclipse-temurin:25-jre uses a Java 25 runtime image
  • WORKDIR /app sets the working directory inside the container
  • COPY target/*.jar app.jar copies your packaged JAR into the image
  • EXPOSE 8080 documents that the app listens on port 8080
  • ENTRYPOINT starts the Java application

3. Add a .dockerignore File

Create a .dockerignore file to avoid copying unnecessary files into the Docker build context:

.git
.idea
*.iml
target
.DS_Store

If your Dockerfile copies from target/*.jar, you can still ignore most build artifacts carefully, but do not ignore the final JAR unless you use a multi-stage build.

A safer option is:

.git
.idea
*.iml
.DS_Store

4. Build the Docker Image

After running mvn clean package, build the image:

docker build -t my-java-app:1.0 .

You can also tag it as latest:

docker build -t my-java-app:latest .

5. Run the Container Locally

Run the container with:

docker run --name my-java-app -p 8080:8080 my-java-app:1.0

Then open:

http://localhost:8080

If your application uses a different internal port, change the second port value:

docker run -p 8080:9090 my-java-app:1.0

This maps:

host port 8080 -> container port 9090

6. Use Environment Variables

Most real applications need configuration such as database URLs, credentials, profiles, or API keys.

Example:

docker run \
  --name my-java-app \
  -p 8080:8080 \
  -e SPRING_PROFILES_ACTIVE=prod \
  -e DB_URL=jdbc:postgresql://db:5432/appdb \
  my-java-app:1.0

For Spring Boot, common environment variables include:

SPRING_PROFILES_ACTIVE=prod
SERVER_PORT=8080
SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/appdb
SPRING_DATASOURCE_USERNAME=appuser
SPRING_DATASOURCE_PASSWORD=secret

7. Multi-Stage Dockerfile

A better production approach is to build the application inside Docker.

FROM maven:3.9-eclipse-temurin-25 AS build

WORKDIR /app

COPY pom.xml .
COPY src ./src

RUN mvn clean package -DskipTests

FROM eclipse-temurin:25-jre

WORKDIR /app

COPY --from=build /app/target/*.jar app.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "app.jar"]

This gives you:

  • Reproducible builds
  • No need to install Maven locally
  • A smaller final image because Maven is not included in the runtime image

8. Docker Compose Example

If your Java app needs a database, use Docker Compose.

Create docker-compose.yml:

services:
  app:
    build: .
    container_name: my-java-app
    ports:
      - "8080:8080"
    environment:
      SPRING_PROFILES_ACTIVE: docker
      SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/appdb
      SPRING_DATASOURCE_USERNAME: appuser
      SPRING_DATASOURCE_PASSWORD: secret
    depends_on:
      - db

  db:
    image: postgres:17
    container_name: app-postgres
    environment:
      POSTGRES_DB: appdb
      POSTGRES_USER: appuser
      POSTGRES_PASSWORD: secret
    ports:
      - "5432:5432"
    volumes:
      - postgres-data:/var/lib/postgresql/data

volumes:
  postgres-data:

Run it with:

docker compose up --build

Stop it with:

docker compose down

Remove volumes too:

docker compose down -v

9. Push the Image to a Registry

Tag the image for Docker Hub:

docker tag my-java-app:1.0 your-dockerhub-username/my-java-app:1.0

Log in:

docker login

Push:

docker push your-dockerhub-username/my-java-app:1.0

For GitHub Container Registry:

docker tag my-java-app:1.0 ghcr.io/your-github-username/my-java-app:1.0
docker push ghcr.io/your-github-username/my-java-app:1.0

10. Deploy on a Server

On your server:

docker pull your-dockerhub-username/my-java-app:1.0

Run it:

docker run -d \
  --name my-java-app \
  --restart unless-stopped \
  -p 80:8080 \
  -e SPRING_PROFILES_ACTIVE=prod \
  your-dockerhub-username/my-java-app:1.0

Now your app is available on:

http://your-server-ip

11. Production-Friendly Dockerfile

For a more production-ready Java container, add memory options and a non-root user.

FROM eclipse-temurin:25-jre

WORKDIR /app

RUN addgroup --system appgroup && adduser --system --ingroup appgroup appuser

COPY target/*.jar app.jar

RUN chown appuser:appgroup app.jar

USER appuser

EXPOSE 8080

ENV JAVA_OPTS=""

ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar app.jar"]

Run with JVM options:

docker run \
  -p 8080:8080 \
  -e JAVA_OPTS="-Xms256m -Xmx512m" \
  my-java-app:1.0

12. Common Commands

List images

docker images

List running containers

docker ps

List all containers

docker ps -a

View logs

docker logs my-java-app

Follow logs:

docker logs -f my-java-app

Stop container

docker stop my-java-app

Remove container

docker rm my-java-app

Remove image

docker rmi my-java-app:1.0

Open shell in container

docker exec -it my-java-app sh

Recommended Minimal Setup

For most Java web applications, start with these two files.

Dockerfile

FROM eclipse-temurin:25-jre

WORKDIR /app

COPY target/*.jar app.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "app.jar"]

.dockerignore

.git
.idea
*.iml
.DS_Store

Then run:

mvn clean package
docker build -t my-java-app:1.0 .
docker run -p 8080:8080 my-java-app:1.0

That is the basic end-to-end flow for containerizing and deploying a Java application with Docker.

How to Visualize System Components with PlantUML Component Diagrams

To visualize system components using PlantUML Component Diagrams, you’ll need to follow these steps. Component diagrams allow you to model and delineate the architecture of a larger system by showing how components interact with each other.

Steps to Create a Component Diagram with PlantUML

  1. Set Up PlantUML
    To create component diagrams with PlantUML, you need:

    • Java installed
    • PlantUML jar file (or an IDE/plugin with integrated PlantUML support like IntelliJ or VSCode with the PlantUML extension)
    • A rendering tool such as Graphviz (dot).
  2. Start the Diagram
    Specify the start of the diagram using:

    @startuml
    
  3. Define Components
    Each component in the system can be represented with the component keyword. Give each component a meaningful name. Use square brackets or the as keyword to assign aliases/titles to the components:

    component [Component A]
    component "Database" as DB
    
  4. Show Relationships Between Components
    Use arrows (--> or --) to represent interfaces, dependencies, or flows between components:

    [Frontend] --> [Backend]
    [Backend] --> DB
    
  5. Group Components (Optional)
    Use package to group logically related components:

    package "User Interface" {
       [Frontend]
       component "Authentication Module" as AuthModule
    }
    
  6. Icons for Common Elements (Optional)
    You can use PlantUML’s built-in stereotypes to enhance clarity by showing commonly used icons:

    component [Cloud Service] <<cloud>>
    component [Database] <<database>>
    
  7. End the Diagram
    Close the diagram with:

    @enduml
    

Example: Simple Component Diagram

Here’s a complete example that shows an e-commerce system with a frontend, backend, and database:

@startuml
title E-Commerce System Architecture

package "User Interface" {
    [Frontend]
}

package "Business Logic" {
    component "Authentication Service" as AuthService
    component "Product Service" as ProductService
}

package "Data Layer" {
    [Database] <<database>>
}

[Frontend] --> AuthService : authenticate()
[Frontend] --> ProductService : fetch products
AuthService --> [Database] : verify credentials
ProductService --> [Database] : query data

@enduml

Render the Diagram

  • Run the PlantUML jar file or use an IDE plugin to generate the component diagram as an image (PNG, SVG, etc.).
  • Use online tools such as PlantUML Server or integrated plugins in IDEs.

Output

The diagram will illustrate:

  • Frontend interacting with services in the backend.
  • Backend services communicating with the database.
  • Logical groupings (packages) of components.

By following these steps, you can easily model and abstract complex systems to identify dependencies, cohesion, and interactions clearly.

How to Design Package Diagrams Using PlantUML for Modular Architecture

In a modular architecture, package diagrams are a powerful way to represent the dependencies and relationships between different modules or packages within a system. With PlantUML, you can easily create package diagrams to visually describe your architecture and ensure modularity principles like separation of concerns, low coupling, and high cohesion are maintained.
Here’s how you can design package diagrams using PlantUML for modular architecture:

1. Understanding the Components of Package Diagrams

Before creating the diagram, it’s important to understand the following key elements:

  • Packages: Represent logical groupings of classes, modules, or functionalities.
  • Dependencies: Links between packages show directional relationships (e.g., which package depends on another).
  • Hierarchies: You can nest packages inside others to show submodules or grouped components.

2. Basic PlantUML Syntax for Package Diagrams

PlantUML provides simple syntax for creating package diagrams using keywords like package, namespace, and component.

Example Syntax:

@startuml
package "Module 1" {
  [Class1]
  [Class2]
}

package "Module 2" {
  [Class3]
}

[Class1] --> [Class3] : Uses
@enduml

3. Steps for Designing Modular Architecture Package Diagram

Follow these steps to design a package diagram for modular architecture:

Step 1: Identify Modules or Layers

List all high-level modules or layers of your system (e.g., UI Layer, Business Logic Layer, Data Access Layer, etc.).

Step 2: Define Submodules

Break each module into its submodules or components (e.g., User Management Module inside Business Logic Layer).

Step 3: Show Dependencies

Draw directional relationships between modules. Ensure dependencies only flow in one direction to avoid circular links.

Step 4: Apply Abstractions

Use abstractions like interfaces and package hierarchy to reduce direct dependencies between modules.

4. PlantUML Example: Modular Architecture

Here’s an example of a modular architecture package diagram using PlantUML:

@startuml
title Modular Architecture Package Diagram

package "UI Layer" {
  [LoginScreen]
  [Dashboard]
}

package "Business Logic Layer" {
  [UserService]
  [OrderService]
}

package "Data Access Layer" {
  [UserRepository]
  [OrderRepository]
}

[LoginScreen] --> [UserService] : Uses
[Dashboard] --> [OrderService] : Displays Data
[UserService] --> [UserRepository] : Accesses Data
[OrderService] --> [OrderRepository] : Accesses Data

@enduml

This example demonstrates:

  1. Abstract layers to separate responsibilities (UI, Business Logic, Data Access).
  2. Directional dependencies to reduce coupling.
  3. Components grouped logically by their roles.

5. Advanced Features

PlantUML allows you to incorporate advanced features in package diagrams:

  • Nested Packages: Nest submodules within a parent module to show hierarchical relationships.
  @startuml
  package "Business Logic Layer" {
    package "User Management" {
      [UserService]
      [UserValidator]
    }

    package "Order Management" {
      [OrderService]
      [OrderValidator]
    }
  }
  @enduml
  • Styling Packages: You can customize the styles for better visuals.
  @startuml
  package "Module A" #LightBlue {
    [Component1]
    [Component2]
  }

  package "Module B" #LightGreen {
    [Component3]
  }

  [Component1] --> [Component3]
  [Component2] --> [Component3]
  @enduml
  • Interfaces in Packages: Use interface to show exposed functionality.
  @startuml
  package "Business Logic Layer" {
    interface IOrderService
    [OrderService]
    IOrderService <|.. [OrderService]
  }

  [UI] --> IOrderService
  @enduml

6. Best Practices for Modular Architecture

  • Minimize Coupling: Ensure packages communicate only via interfaces or well-defined dependencies.
  • High Cohesion: Group related functionalities together in the same package.
  • Avoid Circular Dependencies: Acyclic dependencies promote better maintainability.
  • Group by Layers: Prefer logical layers (e.g., presentation, domain, infrastructure).
  • Add Descriptions: Use notes for additional descriptions.

7. Tools for Generating Package Diagrams

You can generate diagrams directly from PlantUML-text files or integrate with tools like:

  • IntelliJ IDEA (with PlantUML plugin)
  • Visual Studio Code (with PlantUML extension)
  • Online tools like PlantUML Editor

By following these practices and using the examples, you can effectively design modular architecture package diagrams using PlantUML.