Display Name
Below is the AccountTest2 solution, with methods for setup and a single test.
package com.example.junit5.solutions;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.example.junit5.drills.Account;
class AccountTests2 {
private Account account;
@BeforeEach
void setUp() {
account = new Account(100);
}
@AfterEach
void tearDown() {
account = null;
}
@Test
void test() {
fail("Not yet implemented");
}
}
We will add a descriptive @Test method to test that the deposit method adds to the Account's balance.
@Test
void test_deposit_adds_to_balance() {
account.deposit(50);
assertEquals(150, account.getBalance());
}
Rather than give the method a long name describing its behavior, we can use an annotation to state the method's purpose.
@DisplayName¶
This annotation is placed above a class or method to describe it. * We can use any character in the description - even emojis.
@DisplayName("Account Balance Tests")
class AccountTests2 {
// ...
@Test
@DisplayName("Test deposit adds to balance.")
void testDeposit() {
account.deposit(50);
assertEquals(150, account.getBalance());
}
}
The description displays when the test case is run.

Practice Exercise¶
NOTE:
@DisplayNameis NOT a substitute for proper method names. Every method name (not just test methods) should always be a descriptive verb or phrase. Popular test method naming conventions include: *test_FeatureBeingTested*test_list_all_Presidents()test_MethodName_ExpectedResult_TestCondition*test_list_getPresident_returns_null_for_invalid_term_number()TestCondition_MethodName_ExpectedResultwhenTermNumber_invalid_getPresident_returns_null()
Drill¶
JUnit5/test/com.example.junit5.drills.AccountTests
- Change the
testmethod totestDeposit, above, with@DisplayName.Make sure you import the correct
assertEqualsmethod, located inorg.junit.jupiter.api.Assertions(Solution: AccountTests3.java)