Test Overview
This test uses a fake object to simulate a database service. It verifies that the service returns the expected user name for a given user ID.
This test uses a fake object to simulate a database service. It verifies that the service returns the expected user name for a given user ID.
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; class UserService { private final Database database; UserService(Database database) { this.database = database; } String getUserName(int userId) { return database.getUserNameById(userId); } } interface Database { String getUserNameById(int userId); } class FakeDatabase implements Database { @Override public String getUserNameById(int userId) { if (userId == 1) { return "Alice"; } else { return "Unknown"; } } } public class UserServiceTest { @Test void testGetUserNameReturnsCorrectName() { Database fakeDb = new FakeDatabase(); UserService userService = new UserService(fakeDb); String result = userService.getUserName(1); assertEquals("Alice", result); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | JUnit test runner is ready to execute the test method | — | PASS |
| 2 | Creates a FakeDatabase object | FakeDatabase instance is created with hardcoded user data | — | PASS |
| 3 | Creates UserService with the fake database | UserService instance holds reference to FakeDatabase | — | PASS |
| 4 | Calls getUserName(1) on UserService | UserService calls FakeDatabase.getUserNameById(1) | — | PASS |
| 5 | FakeDatabase returns "Alice" for userId 1 | UserService receives "Alice" | — | PASS |
| 6 | Assert that returned name equals "Alice" | JUnit compares expected "Alice" with actual "Alice" | assertEquals("Alice", result) | PASS |
| 7 | Test ends successfully | Test method completes without errors | — | PASS |
fake object in JUnit testing?DataService?implements keyword and must provide method implementations.implements and defines the method. public class FakeDataService extends DataService { public int getData() { return 42; } } incorrectly uses extends with an interface. public interface FakeDataService implements DataService { int getData(); } wrongly declares an interface implementing another interface with method body. public class FakeDataService { int getData() { return 42; } } lacks interface implementation.interface Calculator { int add(int a, int b); }
class FakeCalculator implements Calculator {
public int add(int a, int b) { return 10; }
}
@Test
void testAdd() {
Calculator calc = new FakeCalculator();
assertEquals(10, calc.add(3, 7));
}add method in FakeCalculator always returns 10 regardless of inputs.calc.add(3, 7) equals 10, which matches the fake's return value.interface UserRepository {
String findUserName(int id);
}
class FakeUserRepository implements UserRepository {
public String findUserName() {
return "TestUser";
}
}findUserName requires an int id parameter, but the fake method has no parameters.Pass the test and this pattern moves unknown