Introduction
The article covers the detailed setup and execution of nested classes. A class is termed as a nested class if it is put inside another class and arranged in the hierarchical order. Before discussing the setup and execution.
When do we need nested classes? There are situations when the test class can contain several complex tests logic which need to be tested in such cases nested classes are used.
How to implement nested classes? A non-specialist would answer that the complex and multiple test cases are broken so that each test could be smaller and modularized. The several tests can be grouped under a single test class in a nested fashion.
Getting Started
build.gradle
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter', version: '5.8.2'
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}Project Structure

Example of Nested class Test
Create Money
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;
public class Money {
private final CurrencyUnit currency;
private final BigDecimal amount;
private Money(CurrencyUnit currency, BigDecimal amount) {
this.currency = currency;
this.amount = amount;
}
public static Money of(CurrencyUnit currency, Double amount) {
return new Money(currency, new BigDecimal(amount).setScale(2, RoundingMode.HALF_UP));
}
public Money add(Money money) {
if (!currency.equals(money.currency)) {
throw new CurrencyMismatchException();
}
return new Money(currency, amount.add(money.amount));
}
public BigDecimal getAmount() {
return amount;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Money money = (Money) o;
return Objects.equals(currency, money.currency) && Objects.equals(amount, money.amount);
}
@Override
public int hashCode() {
return Objects.hash(currency, amount);
}
}Create CurrencyUnit
public class CurrencyUnit {
private final String unit;
private CurrencyUnit(String unit) {
this.unit = unit;
}
public static CurrencyUnit of(String unit) {
return new CurrencyUnit(unit);
}
public String getUnit() {
return unit;
}
}Create CurrencyMismatchException
public class CurrencyMismatchException extends RuntimeException {}
Create MoneyTest
import org.junit.jupiter.api.*;
import java.math.BigDecimal;
import static org.junit.jupiter.api.Assertions.*;
public class MoneyTest {
@Nested
@DisplayName("equality is based on values")
class Equality {
@Test
@DisplayName("monies with same amounts are equal")
void moniesWithSameAmountsAreEqual() {
CurrencyUnit eur = CurrencyUnit.of("EUR");
Money first = Money.of(eur, 3.99);
Money second = Money.of(eur, 3.99);
assertEquals(second, first);
}
@Test
@DisplayName("monies with different amounts are not equal")
void moniesWithDifferentAmountsAreNotEqual() {
CurrencyUnit eur = CurrencyUnit.of("EUR");
Money first = Money.of(eur, 3.99);
Money second = Money.of(eur, 3.89);
assertNotEquals(second, first);
}
@Test
@DisplayName("monies with different currencies are not equal")
void moniesWithDifferentCurrenciesAreNotEqual() {
CurrencyUnit eur = CurrencyUnit.of("EUR");
CurrencyUnit usd = CurrencyUnit.of("USD");
Money first = Money.of(eur, 3.99);
Money second = Money.of(usd, 3.99);
assertNotEquals(second, first);
}
}
@Nested
@DisplayName("adding monetary amounts")
class Addition {
@Test
@DisplayName("can add monies of same currency")
void addMoneyWithSameCurrency() {
CurrencyUnit eur = CurrencyUnit.of("EUR");
Money money = Money.of(eur, 4.25);
Money addend = Money.of(eur, 1.4);
Money sum = money.add(addend);
assertEquals(BigDecimal.valueOf(5.65), sum.getAmount());
}
@Test
@DisplayName("cannot add monies of different currency")
void addMoneyWithDifferentCurrency() {
CurrencyUnit eur = CurrencyUnit.of("EUR");
CurrencyUnit usd = CurrencyUnit.of("USD");
Money money = Money.of(eur, 4.25);
Money addend = Money.of(usd, 1.4);
assertThrows(CurrencyMismatchException.class, () -> money.add(addend));
}
}
}Now hit this button to test our application

Output Window






