1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| import org.junit.jupiter.api.Test;
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Optional;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.junit.jupiter.api.Assertions.*;
class AssertionTests {
@Test void testEquality() { int actual = 5; int expected = 10; assertEquals(expected, actual, "两端的值应该相等"); }
@Test void testInequality() { int value1 = 5; int value2 = 5; assertNotEquals(value1, value2, "两端的值不应该相等"); }
Object object1 = new Object(); Object object2 = object1; Object object3 = new Object();
@Test void testSameReference() { assertSame(object1, object3, "引用了不同的对象"); }
@Test void testDifferentReferences() { assertNotSame(object1, object2, "引用了相同的对象"); }
Object nullableObject = null; Object notNullableObject = new Object();
@Test void testIsNull() { assertNull(notNullableObject, "对象应该为空"); }
@Test void testIsNotNull() { assertNotNull(nullableObject, "对象不能为空"); }
@Test void testTrueCondition() { boolean condition = false; assertTrue(condition, "判断不是True"); }
@Test void testFalseCondition() { boolean condition = true; assertFalse(condition, "判断不是False"); }
@Test void testExceptionThrown() { Exception exception = assertThrows(IllegalArgumentException.class, () -> {
}, "应该抛出异常的代码"); }
@Test void testNoExceptionThrown() { assertDoesNotThrow(() -> { int i = 1 / 0; }, "不应该抛出异常的代码"); }
@Test void testMultipleAssertions() {
Person person = new Person(); person.setName("John1"); person.setAge(29); person.setEmployed(false);
assertAll("Person properties", () -> assertEquals("John", person.getName(), "name 不匹配"), () -> assertEquals(30, person.getAge(), "age 不匹配"), () -> assertTrue(person.isEmployed(), "employed 不匹配") ); }
static class Person { String name; int age; boolean employed;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public boolean isEmployed() { return employed; }
public void setEmployed(boolean employed) { this.employed = employed; } }
@Test void testAssertThat() { assertThat(testAdd(1, 2)).isEqualTo(3); assertThat(testAdd(1, 2)).isNotEqualTo(4); assertThat(testAdd(0, 0)).isZero(); assertThat(testAdd(1, 2)).isNotZero(); assertThat(returnNull()).isNull(); assertThat(testAdd(1, 2)).isNotNull(); assertThat(returnTrue()).isTrue(); assertThat(returnFalse()).isFalse(); assertThat(new ArrayList<>().isEmpty()); assertThat(getStringList().contains("A")); assertThat(Optional.of("A")).hasValue("A"); assertThat("I am good").containsPattern("I am"); } public Object returnNull() {return null;} public int testAdd(int a, int b) {return a + b;} public boolean returnTrue() {return true;} public boolean returnFalse() {return false;} public List<String> getStringList() {return Arrays.asList(new String[]{"A", "B", null});}
}
|