From 83df968617730c42f6871324916e21304cd74433 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Mon, 20 Jun 2016 07:49:39 +0200 Subject: [PATCH 1/3] Initial commit --- assertj-introduction/pom.xml | 38 +++++++++++++++++++ .../com/baeldung/assertj/AssertJDemoTest.java | 10 +++++ pom.xml | 2 + 3 files changed, 50 insertions(+) create mode 100644 assertj-introduction/pom.xml create mode 100644 assertj-introduction/src/test/java/com/baeldung/assertj/AssertJDemoTest.java diff --git a/assertj-introduction/pom.xml b/assertj-introduction/pom.xml new file mode 100644 index 0000000000..21cd0c0121 --- /dev/null +++ b/assertj-introduction/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + + com.baeldung + assertj-introduction + 1.0.0-SNAPSHOT + + + + junit + junit + 4.12 + + + org.assertj + assertj-core + 3.4.1 + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + + + + + \ No newline at end of file diff --git a/assertj-introduction/src/test/java/com/baeldung/assertj/AssertJDemoTest.java b/assertj-introduction/src/test/java/com/baeldung/assertj/AssertJDemoTest.java new file mode 100644 index 0000000000..3f4cb390bb --- /dev/null +++ b/assertj-introduction/src/test/java/com/baeldung/assertj/AssertJDemoTest.java @@ -0,0 +1,10 @@ +package com.baeldung.assertj; + +import org.junit.Test; + +public class AssertJDemoTest { + + @Test + public void shouldTest() throws Exception { + } +} diff --git a/pom.xml b/pom.xml index 19c602091d..505b72e1ab 100644 --- a/pom.xml +++ b/pom.xml @@ -9,6 +9,8 @@ apache-fop + assertj-introduction + core-java core-java-8 gson From a871f2558fa32e7c40c08b9eeb90f2b3acdb6ecc Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Tue, 21 Jun 2016 19:53:15 +0200 Subject: [PATCH 2/3] Add first set of assertions --- .../java/com/baeldung/assertj/domain/Dog.java | 19 +++++++ .../com/baeldung/assertj/AssertJDemoTest.java | 57 ++++++++++++++++++- 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 assertj-introduction/src/main/java/com/baeldung/assertj/domain/Dog.java diff --git a/assertj-introduction/src/main/java/com/baeldung/assertj/domain/Dog.java b/assertj-introduction/src/main/java/com/baeldung/assertj/domain/Dog.java new file mode 100644 index 0000000000..adfcf05225 --- /dev/null +++ b/assertj-introduction/src/main/java/com/baeldung/assertj/domain/Dog.java @@ -0,0 +1,19 @@ +package com.baeldung.assertj.domain; + +public class Dog { + private String name; + private Float weight; + + public Dog(String name, Float weight) { + this.name = name; + this.weight = weight; + } + + public String getName() { + return name; + } + + public Float getWeight() { + return weight; + } +} diff --git a/assertj-introduction/src/test/java/com/baeldung/assertj/AssertJDemoTest.java b/assertj-introduction/src/test/java/com/baeldung/assertj/AssertJDemoTest.java index 3f4cb390bb..249cb865d9 100644 --- a/assertj-introduction/src/test/java/com/baeldung/assertj/AssertJDemoTest.java +++ b/assertj-introduction/src/test/java/com/baeldung/assertj/AssertJDemoTest.java @@ -1,10 +1,65 @@ package com.baeldung.assertj; +import com.baeldung.assertj.domain.Dog; import org.junit.Test; +import java.util.Arrays; +import java.util.List; +import java.util.NoSuchElementException; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.withPrecision; + public class AssertJDemoTest { @Test - public void shouldTest() throws Exception { + public void whenComparingReferences_thenNotEqual() throws Exception { + Dog fido = new Dog("Fido", 5.15f); + Dog fidosClone = new Dog("Fido", 5.15f); + + assertThat(fido).isNotEqualTo(fidosClone); + } + + @Test + public void whenComparingFields_thenEqual() throws Exception { + Dog fido = new Dog("Fido", 5.15f); + Dog fidosClone = new Dog("Fido", 5.15f); + + assertThat(fido).isEqualToComparingFieldByFieldRecursively(fidosClone); + } + + @Test + public void whenCheckingForElement_thenContains() throws Exception { + List list = Arrays.asList("1", "2", "3"); + + assertThat(list) + .contains("1"); + } + + @Test + public void whenCheckingForElement_thenMultipleAssertions() throws Exception { + List list = Arrays.asList("1", "2", "3"); + + assertThat(list) + .isNotEmpty() + .contains("1") + .startsWith("1") + .doesNotContainNull() + .containsSequence("2", "3"); + } + + @Test + public void whenCheckingRunnable_thenIsInterface() throws Exception { + assertThat(Runnable.class).isInterface(); + } + + @Test + public void whenAssigningNSEExToException_thenIsAssignable() throws Exception { + assertThat(Exception.class).isAssignableFrom(NoSuchElementException.class); + } + + @Test + public void whenComparingWithOffset_thenEquals() throws Exception { + assertThat(5.1).isEqualTo(5, withPrecision(1d)); } } From 4aa5f7f30c98c49fecf7608a7f667a0622400ea9 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Tue, 21 Jun 2016 20:14:22 +0200 Subject: [PATCH 3/3] Add second set of assertions --- .../com/baeldung/assertj/domain/Person.java | 19 +++++++++++++++++++ .../com/baeldung/assertj/AssertJDemoTest.java | 12 ++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 assertj-introduction/src/main/java/com/baeldung/assertj/domain/Person.java diff --git a/assertj-introduction/src/main/java/com/baeldung/assertj/domain/Person.java b/assertj-introduction/src/main/java/com/baeldung/assertj/domain/Person.java new file mode 100644 index 0000000000..4f6bb7f55b --- /dev/null +++ b/assertj-introduction/src/main/java/com/baeldung/assertj/domain/Person.java @@ -0,0 +1,19 @@ +package com.baeldung.assertj.domain; + +public class Person { + private String name; + private Integer age; + + public Person(String name, Integer age) { + this.name = name; + this.age = age; + } + + public String getName() { + return name; + } + + public Integer getAge() { + return age; + } +} diff --git a/assertj-introduction/src/test/java/com/baeldung/assertj/AssertJDemoTest.java b/assertj-introduction/src/test/java/com/baeldung/assertj/AssertJDemoTest.java index 249cb865d9..71ce2c53b5 100644 --- a/assertj-introduction/src/test/java/com/baeldung/assertj/AssertJDemoTest.java +++ b/assertj-introduction/src/test/java/com/baeldung/assertj/AssertJDemoTest.java @@ -1,6 +1,8 @@ package com.baeldung.assertj; import com.baeldung.assertj.domain.Dog; +import com.baeldung.assertj.domain.Person; +import org.junit.Ignore; import org.junit.Test; import java.util.Arrays; @@ -62,4 +64,14 @@ public class AssertJDemoTest { public void whenComparingWithOffset_thenEquals() throws Exception { assertThat(5.1).isEqualTo(5, withPrecision(1d)); } + + @Ignore // IN ORDER TO TEST, REMOVE THIS LINE + @Test + public void whenRunningAssertion_thenDescribed() throws Exception { + Person person = new Person("Alex", 34); + + assertThat(person.getAge()) + .as("%s's age should be equal to 100") + .isEqualTo(100); + } }