|
| 1 | +package demo.assertJ; |
| 2 | + |
| 3 | +import org.assertj.core.data.Offset; |
| 4 | +import org.assertj.core.data.Percentage; |
| 5 | +import org.assertj.jodatime.api.Assertions; |
| 6 | +import org.joda.time.LocalDate; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.io.IOException; |
| 10 | +import java.util.*; |
| 11 | + |
| 12 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; |
| 13 | +import static org.assertj.core.api.BDDAssertions.assertThat; |
| 14 | +import static org.assertj.core.api.BDDAssertions.then; |
| 15 | + |
| 16 | + |
| 17 | +public class AssertJ { |
| 18 | + public static void main(String[] args) throws IOException { |
| 19 | + //Equal Assertions |
| 20 | + assertThat("John Doe").isEqualTo("John Doe"); |
| 21 | + assertThat("John").isNotEqualTo("Doe"); |
| 22 | + |
| 23 | + //Null Assertions |
| 24 | + String value="John"; |
| 25 | + String nullValue = null; |
| 26 | + assertThat(value).as("Non Null Check").isNotNull(); |
| 27 | + assertThat(nullValue).as("Null Check").isNull(); |
| 28 | + |
| 29 | + //Boolean Assertions |
| 30 | + Boolean boolValue = true; |
| 31 | + assertThat(boolValue).isTrue(); |
| 32 | + assertThat(boolValue).isNotEqualTo(false); |
| 33 | + |
| 34 | + //String Assertions - Chaining the Assertions |
| 35 | + String strValue="Happy New Year"; |
| 36 | + assertThat(strValue) |
| 37 | + .describedAs("String Assertions") |
| 38 | + .isNotNull() |
| 39 | + .startsWith("Happy") |
| 40 | + .contains("New") |
| 41 | + .endsWith("Year") |
| 42 | + .isNotBlank() |
| 43 | + .isNotEmpty() |
| 44 | + .overridingErrorMessage(()->"Custom Message..") |
| 45 | + .containsWhitespaces() |
| 46 | + .doesNotContainOnlyWhitespaces() |
| 47 | + .isMixedCase() |
| 48 | + .containsIgnoringWhitespaces("HappyNewYear") |
| 49 | + .hasSizeGreaterThan(5) |
| 50 | + .as("Input contains only the characters and space") |
| 51 | + .matches("^[a-zA-Z ]*") |
| 52 | + .describedAs("Input doesn't contain the numbers") |
| 53 | + .doesNotMatch("\\d.*") |
| 54 | + .isInstanceOf(String.class); |
| 55 | + assertThat("Game'of'Thrones").isEqualToNormalizingPunctuationAndWhitespace("GameofThrones"); |
| 56 | + |
| 57 | + //Numeric Assertions |
| 58 | + int number=58; |
| 59 | + assertThat(number) |
| 60 | + .describedAs("Numeric Assertions") |
| 61 | + .isPositive() |
| 62 | + .isNotNegative() |
| 63 | + .isNotZero() |
| 64 | + .isBetween(35,70) |
| 65 | + .isInstanceOf(Integer.class) |
| 66 | + .isEven() |
| 67 | + .isCloseTo(60, Offset.offset(2)) |
| 68 | + .isCloseTo(52, Percentage.withPercentage(15)) |
| 69 | + .isNotCloseTo(70,Percentage.withPercentage(5)); |
| 70 | + |
| 71 | + //List based Iterable Assertions |
| 72 | + List<String> languages= Arrays.asList("Java","Python","Ruby","Go"); |
| 73 | + List<String> spokenLanguage=Arrays.asList("English","Deutsch"); |
| 74 | + assertThat(languages) |
| 75 | + .hasSize(4) |
| 76 | + .hasSizeLessThan(6) |
| 77 | + .isNotEmpty() |
| 78 | + .isNotNull() |
| 79 | + .doesNotContainNull() |
| 80 | + .doesNotHaveDuplicates() |
| 81 | + .isInstanceOf(List.class) |
| 82 | + .doesNotContainAnyElementsOf(spokenLanguage) |
| 83 | + .withFailMessage(()->"Input doen't matches criteria") |
| 84 | + .allMatch(s->s.matches("^[a-zA-Z]*")) |
| 85 | + .containsExactly("Java","Python","Ruby","Go"); |
| 86 | + |
| 87 | + //Map based Assertions |
| 88 | + Map<String, String> sports=new HashMap<>(); |
| 89 | + sports.put("Cricket","England"); |
| 90 | + sports.put("Football","Germany"); |
| 91 | + sports.put("Hockey","India"); |
| 92 | + assertThat(sports) |
| 93 | + .hasSize(3) |
| 94 | + .containsKey("Cricket") |
| 95 | + .containsValue("India") |
| 96 | + .containsEntry("Football","Germany") |
| 97 | + .isInstanceOf(Map.class) |
| 98 | + .isNotNull() |
| 99 | + .satisfies(sport -> assertThat(sport.size()>2)); |
| 100 | + |
| 101 | + //Object based Assertions |
| 102 | + Student s1=new Student("James",24,"Hyd"); |
| 103 | + Student s2=new Student("John",28,"BLR"); |
| 104 | + Student s3=new Student("Rosy",26,"Mum"); |
| 105 | + assertThat(s1) |
| 106 | + .isInstanceOf(Student.class) |
| 107 | + .as("%s age should be less than 35",s1.getName()) |
| 108 | + .hasFieldOrProperty("age") |
| 109 | + .hasFieldOrPropertyWithValue("city","Hyd") |
| 110 | + .hasNoNullFieldsOrProperties() |
| 111 | + .extracting(s -> s.getAge()) |
| 112 | + .isEqualTo(24); |
| 113 | + |
| 114 | + //File based Assertions |
| 115 | + File fileObj= new File(System.getProperty("user.dir").concat(System.getProperty("file.separator")+"Sample.txt")); |
| 116 | + assertThat(fileObj) |
| 117 | + .describedAs("File Assertions") |
| 118 | + .canRead() |
| 119 | + .canWrite() |
| 120 | + .isFile() |
| 121 | + .hasExtension("txt"); |
| 122 | + |
| 123 | + //Exception Assertions |
| 124 | + assertThatThrownBy(()->getException()).as("Check Exception").isInstanceOf(RuntimeException.class); |
| 125 | + assertThatThrownBy(()->{throw new Exception("foo");}).hasMessage("foo"); |
| 126 | + |
| 127 | + //Date and Time Based Assertions |
| 128 | + LocalDate date=new LocalDate(2023,1,1); |
| 129 | + Assertions.assertThat(date) |
| 130 | + .hasYear(2023) |
| 131 | + .hasMonthOfYear(1) |
| 132 | + .hasDayOfMonth(1) |
| 133 | + .isBefore(new LocalDate(2024,1,1)); |
| 134 | + |
| 135 | + //BDD Assertions |
| 136 | + then("AssertJ") |
| 137 | + .satisfies(val->{ |
| 138 | + assertThat(val).isNotNull(); |
| 139 | + assertThat(val.length()).isGreaterThan(4); |
| 140 | + }); |
| 141 | + |
| 142 | + } |
| 143 | + public static void getException(){ |
| 144 | + throw new RuntimeException(); |
| 145 | + } |
| 146 | +} |
0 commit comments