Best Assertj code snippet using org.assertj.core.data.TolkienCharacterAssert.hasName
Source:CustomAssertExamples.java
...31public class CustomAssertExamples extends AbstractAssertionsExamples {32 @Test33 public void successful_custom_assertion_example() {34 // custom assertion : assertThat is resolved from TolkienCharacterAssert static import35 TolkienCharacterAssert.assertThat(frodo).hasName("Frodo");36 // To have a unique entry point for your custom assertions and the core ones, create a class inheriting from37 // Assertions and add an assertThat(TolkienCharacter) that build a TolkienCharacterAssert, then import statically38 // MyProjectAssertions.assertThat to have a unique entry point to all assertions : yours and the core ones.39 // For example, the assertions below are accessed from MyProjectAssertions :40 // - hasName comes from .MyProjectAssertions.assertThat(TolkienCharacter actual)41 assertThat(frodo).hasName("Frodo")42 .hasAge(33)43 .hasRace(Race.HOBBIT)44 .isNotEqualTo(merry);45 // - isEqualTo is accessible since MyProjectAssertions inherits from Assertions which provides Integer assertions.46 assertThat(frodo.age).isEqualTo(33);47 }48 @Test49 public void failed_custom_assertion_example() {50 sam.setName("Sammy");51 try {52 // custom assertion : assertThat is resolved from MyProjectAssertions.assertThat static import53 assertThat(sam).hasName("Sam");54 } catch (AssertionError e) {55 // As we are defining custom assertion, we can set meaningful assertion error message, like this one :56 assertThat(e).hasMessage("Expected character's name to be <Sam> but was <Sammy>");57 }58 // show that the user description is honored.59 try {60 assertThat(sam).as("check name").hasName("Sam");61 } catch (AssertionError e) {62 // As we are defining custom assertion, we can set meaningful assertion error message, like this one :63 assertThat(e).hasMessage("[check name] Expected character's name to be <Sam> but was <Sammy>");64 }65 }66 @Test67 public void inherited_assertion_example() {68 Employee employee = new Employee();69 employee.jobTitle = "CEO";70 employee.name = "John Smith";71 assertThat(employee).hasJobTitle("CEO").hasName("John Smith").isEqualTo(employee);72 assertThat(employee).hasName("John Smith").hasJobTitle("CEO");73 Human joe = new Human();74 joe.name = "joe";75 assertThat(joe).hasName("joe");76 EmployeeOfTheMonth employeeOfTheMonth = new EmployeeOfTheMonth();77 employeeOfTheMonth.setName("John Doe");78 employeeOfTheMonth.setMonth(1);79 employeeOfTheMonth.jobTitle = "Guru";80 EmployeeOfTheMonthAssert.assertThat(employeeOfTheMonth).forMonth(1).isEqualTo(employeeOfTheMonth);81 EmployeeOfTheMonthAssert.assertThat(employeeOfTheMonth).isEqualTo(employeeOfTheMonth).as("").hasName("John Doe")82 .forMonth(1).hasJobTitle("Guru");83 }84 @Test85 public void generics_limitation() {86 Employee martin = new Employee("Martin Fowler");87 Employee kent = new Employee("Kent Beck");88 List<Employee> employees = newArrayList(martin, kent);89 // now let's declare Martin as a Human (Employee inherits from Human90 @SuppressWarnings("unused")91 Human martinAsHuman = martin;92 // this line compile93 assertThat(employees).contains(kent, martin);94 // this one does not compile because contains expect Employee not Human !95 // assertThat(employees).contains(martinAsHuman);96 }97 @Test98 public void soft_custom_assertions_example() {99 // basic object to test100 String name = "Michael Jordan - Bulls";101 // custom object to test102 Employee kent = new Employee("Kent Beck");103 kent.jobTitle = "TDD evangelist";104 // use our own soft assertions inheriting from SoftAssertions105 MyProjectSoftAssertions softly = new MyProjectSoftAssertions();106 softly.assertThat(name).startsWith("Mike").contains("Lakers").endsWith("Chicago");107 softly.assertThat(kent).hasName("Wes Anderson").hasJobTitle("Director");108 try {109 softly.assertAll();110 } catch (SoftAssertionError e) {111 logAssertionErrorMessage("SoftAssertion errors example", e);112 }113 }114 @Test115 public void extending_core_assertions() {116 assertThat(BigDecimal.ONE).isOne().isNotZero().isOne();117 }118}...
Source:TolkienCharacterAssert.java
...38 // thus this class will be your oonly entry poiny to all AssertJ assertions and YOURS.39 // see MyProjectAssertions for an example.40 /**41 * An entry point for TolkienCharacterAssert to follow AssertJ standard <code>assertThat()</code> statements.<br>42 * With a static import, one's can write directly : <code>assertThat(frodo).hasName("Frodo");</code>43 * 44 * @param actual the TolkienCharacter we want to make assertions on.45 * @return a new </code>{@link TolkienCharacterAssert}</code>46 */47 public static TolkienCharacterAssert assertThat(TolkienCharacter actual) {48 return new TolkienCharacterAssert(actual);49 }50 // 4 - a specific assertion51 /**52 * Verifies that the actual TolkienCharacter's name is equal to the given one.53 * 54 * @param name the given name to compare the actual TolkienCharacter's name to.55 * @return this assertion object.56 * @throws AssertionError - if the actual TolkienCharacter's name is not equal to the given one.57 */58 public TolkienCharacterAssert hasName(String name) {59 // check that actual TolkienCharacter we want to make assertions on is not null.60 isNotNull();61 // check condition62 if (!actual.getName().equals(name)) {63 failWithMessage("Expected character's name to be <%s> but was <%s>", name, actual.getName());64 }65 // return the current assertion for method chaining66 return this;67 }68 /**69 * Verifies that the actual TolkienCharacter's race is equal to the given one.70 * @param race the given race to compare the actual TolkienCharacter's race to.71 * @return this assertion object.72 * @throws AssertionError - if the actual TolkienCharacter's race is not equal to the given one....
hasName
Using AI Code Generation
1import org.assertj.core.api.Assertions;2import org.assertj.core.data.TolkienCharacter;3import org.junit.Test;4public class AssertJTest {5 public void testAssertJ() {6 Assertions.assertThat(new TolkienCharacter("Frodo", 33)).hasName("Frodo");7 }8}9[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ assertj ---10[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ assertj ---11[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ assertj ---12[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ assertj ---13[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ assertj ---14[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ assertj ---
hasName
Using AI Code Generation
1import org.assertj.core.api.Assertions;2import org.assertj.core.data.TolkienCharacter;3import org.assertj.core.data.TolkienCharacterAssert;4public class 1 {5 public static void main(String[] args) {6 TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, "ring bearer");7 TolkienCharacterAssert frodoAssert = new TolkienCharacterAssert(frodo);8 Assertions.assertThat(frodoAssert.hasName("Frodo")).isTrue();9 }10}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!