class CalculatorTest {
private Calculator calculator;
@BeforeEach
void setUp() {
calculator = new Calculator();
}
@Test
@DisplayName("λ μλ₯Ό λν μ μμ΄μΌ νλ€.")
void shouldAddTwoNumbers() {
// given
int a = 5;
int b = 3;
// when
int result = calculator.add(a, b);
// then
assertThat(result).isEqualTo(8);
}
@Test
@DisplayName("λ μλ₯Ό λΊ μ μμ΄μΌ νλ€.")
void shouldSubtractTwoNumbers() {
// given
int a = 5;
int b = 3;
// when
int result = calculator.subtract(a, b);
// then
assertThat(result).isEqualTo(2);
}
@Test
@DisplayName("λ μλ₯Ό κ³±ν μ μμ΄μΌ νλ€.")
void shouldMultiplyTwoNumbers() {
// given
int a = 5;
int b = 3;
// when
int result = calculator.multiply(a, b);
// then
assertThat(result).isEqualTo(15);
}
@Test
@DisplayName("λ μλ₯Ό λλ μ μμ΄μΌ νλ€.")
void shouldDivideTwoNumbers() {
// given
int a = 10;
int b = 2;
// when
int result = calculator.divide(a, b);
// then
assertThat(result).isEqualTo(5);
}
@Test
@DisplayName("0μΌλ‘ λλ κ²½μ° μμΈλ₯Ό λμ ΈμΌ νλ€.")
void shouldThrowExceptionWhenDividingByZero() {
// given
int a = 10;
int b = 0;
// when, then
assertThatThrownBy(() -> calculator.divide(a, b))
.isInstanceOf(ArithmeticException.class)
.hasMessage("Divisor cannot be zero!");
}
}
class ExampleTest {
@BeforeAll
static void setUpAll() {
// ν
μ€νΈ ν΄λμ€μ λͺ¨λ ν
μ€νΈ λ©μλ μ€ν μ μ ν λ²λ§ μ€νλ¨
System.out.println("Before all tests");
}
@BeforeEach
void setUp() {
// κ° ν
μ€νΈ λ©μλ μ€ν μ μ μ€νλ¨
System.out.println("Before each test");
}
@Test
void test1() {
System.out.println("Test 1");
}
@Test
void test2() {
System.out.println("Test 2");
}
@AfterEach
void tearDown() {
// κ° ν
μ€νΈ λ©μλ μ€ν νμ μ€νλ¨
System.out.println("After each test");
}
@AfterAll
static void tearDownAll() {
// ν
μ€νΈ ν΄λμ€μ λͺ¨λ ν
μ€νΈ λ©μλ μ€ν νμ ν λ²λ§ μ€νλ¨
System.out.println("After all tests");
}
//μ€ν κ²°κ³Ό
Before all tests
Before each test
Test 1
After each test
Before each test
Test 2
After each test
After all tests
}
class UserServiceTest {
private UserService userService;
@Mock
private UserRepository userRepository;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
userService = new UserService(userRepository);
}
@Test
@DisplayName("μλ‘μ΄ μ¬μ©μλ₯Ό μμ±ν μ μμ΄μΌ νλ€.")
void shouldCreateNewUser() {
// given
User user = new User("john.doe@example.com", "password123");
when(userRepository.save(user)).thenReturn(true);
// when
boolean result = userService.createUser(user);
// then
assertThat(result).isTrue(); // μμ± κ²°κ³Όκ° trueμΈμ§ νμΈ
verify(userRepository).save(user); // userRepository.save() λ©μλκ° νΈμΆλμλμ§ κ²μ¦
}
@Test
@DisplayName("κΈ°μ‘΄ μ¬μ©μλ₯Ό μ‘°νν μ μμ΄μΌ νλ€.")
void shouldRetrieveExistingUser() {
// given
String email = "john.doe@example.com";
User existingUser = new User(email, "password123");
when(userRepository.findByEmail(email)).thenReturn(existingUser);
// when
User result = userService.getUserByEmail(email);
// then
assertThat(result).isNotNull(); // μ‘°νλ μ¬μ©μκ° nullμ΄ μλμ§ νμΈ
assertThat(result).isEqualTo(existingUser); // μ‘°νλ μ¬μ©μκ° μμν μ¬μ©μμ λμΌνμ§ νμΈ
verify(userRepository).findByEmail(email); // userRepository.findByEmail() λ©μλκ° νΈμΆλμλμ§ κ²μ¦
}
@Test
@DisplayName("μ¬μ©μλ₯Ό μμ ν μ μμ΄μΌ νλ€.")
void shouldDeleteUser() {
// given
String email = "john.doe@example.com";
when(userRepository.deleteByEmail(email)).thenReturn(true);
// when
boolean result = userService.deleteUserByEmail(email);
// then
assertThat(result).isTrue(); // μμ κ²°κ³Όκ° trueμΈμ§ νμΈ
verify(userRepository).deleteByEmail(email); // userRepository.deleteByEmail() λ©μλκ° νΈμΆλμλμ§ κ²μ¦
}
}
@SpringBootTest // Spring Boot μ ν리μΌμ΄μ
컨ν
μ€νΈλ₯Ό λ‘λνμ¬ ν
μ€νΈ
@SpringJUnitConfig(classes = {UserService.class, UserRepository.class}) // ν
μ€νΈμ νμν λΉμ μ€μ
@ActiveProfiles("test") // "test" νλ‘νμΌμ ν΄λΉνλ λΉμ΄ λ‘λλ¨
class UserServiceTest {
@Autowired
private UserService userService;
@MockBean
private UserRepository userRepository; // UserRepositoryμ MockBeanμ μ£Όμ
@Test
@DisplayName("μλ‘μ΄ μ¬μ©μλ₯Ό μμ±ν μ μμ΄μΌ νλ€.")
void shouldCreateNewUser() {
// given
User user = new User("john.doe@example.com", "password123");
when(userRepository.save(user)).thenReturn(user);
// when
User result = userService.createUser(user);
// then
assertThat(result).isNotNull(); // μμ±λ μ¬μ©μκ° nullμ΄ μλμ§ νμΈ
assertThat(result.getEmail()).isEqualTo(user.getEmail()); // μμ±λ μ¬μ©μμ μ΄λ©μΌμ΄ μμν μ΄λ©μΌκ³Ό λμΌνμ§ νμΈ
verify(userRepository).save(user); // userRepository.save() λ©μλκ° νΈμΆλμλμ§ κ²μ¦
}
@Test
@DisplayName("κΈ°μ‘΄ μ¬μ©μλ₯Ό μ‘°νν μ μμ΄μΌ νλ€.")
void shouldRetrieveExistingUser() {
// given
String email = "john.doe@example.com";
User existingUser = new User(email, "password123");
when(userRepository.findByEmail(email)).thenReturn(existingUser);
// when
User result = userService.getUserByEmail(email);
// then
assertThat(result).isNotNull(); // μ‘°νλ μ¬μ©μκ° nullμ΄ μλμ§ νμΈ
assertThat(result.getEmail()).isEqualTo(email); // μ‘°νλ μ¬μ©μμ μ΄λ©μΌμ΄ μμν μ΄λ©μΌκ³Ό λμΌνμ§ νμΈ
verify(userRepository).findByEmail(email); // userRepository.findByEmail() λ©μλκ° νΈμΆλμλμ§ κ²μ¦
}
@Test
@DisplayName("μ¬μ©μλ₯Ό μμ ν μ μμ΄μΌ νλ€.")
void shouldDeleteUser() {
// given
String email = "john.doe@example.com";
User existingUser = new User(email, "password123");
when(userRepository.findByEmail(email)).thenReturn(existingUser);
// when
boolean result = userService.deleteUserByEmail(email);
// then
assertThat(result).isTrue(); // μμ κ²°κ³Όκ° trueμΈμ§ νμΈ
verify(userRepository).delete(existingUser); // userRepository.delete() λ©μλκ° νΈμΆλμλμ§ κ²μ¦
}
}
ν΅ν©ν μ€νΈ(MockMvc)λ λ€μ κ°μμμ μ 리
κ²°λ‘
- λ¨μ ν μ€νΈλ μ΅λν μ€νλ§μ΄ μλ, μμ μλ° μ½λλ‘λ§ μμ±
- λ¨μ ν μ€νΈλ κ°λ³ λͺ¨λ λλ ν΄λμ€μ λμμ λ 립μ μΌλ‘ ν μ€νΈνλ κ²μ΄ λͺ©ν
- μ€ν μλκ° λΉ λ₯΄κ³ νΉμ νλ μμν¬μ μ’ μλμ§ μκ³ , 격리λ νκ²½μμ ν μ€νΈ μν κ°λ₯
- μμ μλ° μ½λλ‘ μμ±λ ν μ€νΈλ μλν λꡬλ CI/CD νμ΄νλΌμΈμ μ½κ² ν΅ν© κ°λ₯
- ν΅ν© ν μ€νΈλ μ€νλ§μ΄ μ 곡νλ κΈ°λ₯λ€μ νμ©
- μ€μ νκ²½(Spring Bean)κ³Ό μ μ¬ν 쑰건μμ ν μ€νΈλ₯Ό μν
- λ€μν κ³μΈ΅ κ°μ μνΈμμ©(컨νΈλ‘€λ¬)κ³Ό ν΅ν© κ²μ¦
- λ°μ΄ν°λ² μ΄μ€, μΈλΆ μλΉμ€ λ±κ³Όμ μνΈμμ© κ²μ¦