특정 객체가 null이 아님을 확인합니다.
import org.junit.Test;
import static org.junit.Assert.assertNotNull;
public class MyTests {
@Test
public void testObjectIsNotNull() {
String str = "JUnit Test";
assertNotNull("The object should not be null", str);
}
}
객체가 null인지 확인합니다.
import org.junit.Test;
import static org.junit.Assert.assertNull;
public class MyTests {
@Test
public void testObjectIsNull() {
String str = null;
assertNull("The object should be null", str);
}
}
두 값이 동일한지 확인합니다.
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class MyTests {
@Test
public void testSum() {
int expectedSum = 5;
int actualSum = sum(2, 3);
assertEquals("Sum should be 5", expectedSum, actualSum);
}
public int sum(int a, int b) {
return a + b;
}
}
주어진 조건이 true또는 false인지 확인합니다.
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
public class MyTests {
@Test
public void testIsEven() {
assertTrue("Number should be even", isEven(4));
assertFalse("Number should be odd", isEven(3));
}
public boolean isEven(int number) {
return number % 2 == 0;
}
}
두 배열이 동일한지 비교합니다.
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
public class MyTests {
@Test
public void testArrayEquals() {
int[] expectedArray = {1, 2, 3};
int[] actualArray = {1, 2, 3};
assertArrayEquals("Arrays should be equal", expectedArray, actualArray);
}
}
특정 예외가 발생하는지를 확인합니다.
import org.junit.Test;
import static org.junit.Assert.assertThrows;
public class MyTests {
@Test
public void testThrowsException() {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> {
throw new IllegalArgumentException("Exception message");
});
assertEquals(ex.getMessage(), "Exception message");
}
}
람다 표현식이나 메서드 참조를 사용하여 각 단언을 전달합니다.
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class MyTests {
@Test
public void testMultipleAssertions() {
String str1 = "JUnit 5";
String str2 = "JUnit 5";
String str3 = "Unit testing";
assertAll("Test all assertions",
() -> assertEquals("JUnit 5", str1),
() -> assertEquals("JUnit 5", str2),
() -> assertEquals("JUnit 5", str3) // This will fail
);
}
}
import static org.assertj.core.api.Assertions.assertThat;
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
public class MyTests {
@Test
public void testObjects() {
Person person = new Person("John", 30);
assertThat(person).isNotNull();
assertThat(person.name).isEqualTo("John");
assertThat(person.age).isBetween(20, 40);
}
}
import static org.assertj.core.api.Assertions.assertThat;
public class MyTests {
@Test
public void testBasicTypes() {
int number = 10;
assertThat(number).isGreaterThan(5).isLessThanOrEqualTo(10);
String text = "Hello";
assertThat(text).isEqualTo("Hello").startsWith("He").endsWith("lo").contains("ell");
}
}
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.List;
public class MyTests {
@Test
public void testCollections() {
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
assertThat(names).hasSize(3).contains("Alice", "Charlie").doesNotContain("Eve");
int[] numbers = {1, 2, 3, 4, 5};
assertThat(numbers).containsExactly(1, 2, 3, 4, 5).doesNotContain(0);
}
}
import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class MyTests {
@Test
public void testException() {
assertThatThrownBy(() -> { throw new IllegalArgumentException("Invalid argument"); })
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Invalid argument");
}
}
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.junit.jupiter.api.Assumptions.assumingThat;
import org.junit.jupiter.api.Test;
public class MyTests {
@Test
public void testWithAssumption() {
String environment = System.getenv("TEST_ENV");
// 조건이 참일 때만 코드 블록을 실행합니다.
assumingThat(
"PROD".equals(environment),
() -> {
// 환경이 "PROD"일 때만 실행
System.out.println("Running in production environment");
// production-specific tests
}
);
// 항상 실행되는 코드
System.out.println("Running in all environments");
// common tests
}
}