Skip to content

Commit

Permalink
Add Java 8 examples + minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Costigliola committed Nov 13, 2015
1 parent 4892d04 commit 3b34e86
Showing 1 changed file with 64 additions and 32 deletions.
96 changes: 64 additions & 32 deletions pres/index.html
Expand Up @@ -58,7 +58,7 @@ <h2>AssertJ is user friendly</h2>
<li>Possibility to chain assertions </li>
<li><a href="http://joel-costigliola.github.io/assertj/assertj-core-quick-start.html">Quick start guide</a></li>
<li>Available in ivy and maven</li>
<li>Use version 2.1.0 for Java 7 and 3.1.0 for Java 8</li>
<li>Use version 2.2.0 for Java 7 and 3.2.0 for Java 8</li>
</ul>
</section>

Expand All @@ -79,7 +79,7 @@ <h2>Assertions for JDK types</h3>

<section>
<h2>Let's test this code :</h2>
<pre><code class="java" contenteditable>
<pre><code class="java">
public class TolkienCharacter {
// boilerplate getter omitted
private int age;
Expand All @@ -91,15 +91,15 @@ <h2>Let's test this code :</h2>
TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);

// ... and his friends from the Fellowship of the Ring
List&lt;TolkienCharacter&gt; fellowshipOfTheRing = newArrayList(
List&lt;TolkienCharacter&gt; fellowshipOfTheRing = Arrays.asList(
frodo, sam, merry, pippin, gandalf, legolas, gimli, aragorn, boromir);
</code></pre>
</section>


<section>
<h2>Basic assertions example</h2>
<pre><code class="java" contenteditable>
<pre><code class="java">
// unique entry point to get all assertThat methods
import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -114,21 +114,21 @@ <h2>Basic assertions example</h2>
</section>

<section>
<h2>Collection assertions</h2>
<pre><code class="java" contenteditable>
// iterable/array assertions
<h2>Collection / array assertions</h2>
<pre><code class="java">
assertThat(fellowshipOfTheRing).isNotEmpty()
.hasSize(9)
.contains(frodo, sam)
.doesNotContain(sauron);
.doesNotContain(sauron)
.doesNotHaveDuplicates();
</code></pre>
<p>Full assertions showcase in <a href="https://github.com/joel-costigliola/assertj-examples/tree/master/assertions-examples/src/test/java/org/assertj/examples">assertj-examples</a> project.</p>
<p>Full assertions showcase in <a href="https://github.com/joel-costigliola/assertj-examples/tree/java-8/assertions-examples/src/test/java/org/assertj/examples">assertj-examples</a> project.</p>
</section>

<section>
<h2>Collection assertions : extracting feature</h2>
<h2>Extracting feature</h2>
<p>Useful to check properties/fields of collection elements</p>
<pre><code class="java" contenteditable>
<pre><code class="java">
// extract the names of fellowshipOfTheRing characters and check them
assertThat(fellowshipOfTheRing).extracting("name")
.contains("Gandalf", "Frodo")
Expand All @@ -141,12 +141,11 @@ <h2>Collection assertions : extracting feature</h2>
tuple("Legolas", 1000, "Elf"));

</code></pre>
<p>Full assertions showcase in <a href="https://github.com/joel-costigliola/assertj-examples/tree/master/assertions-examples/src/test/java/org/assertj/examples">assertj-examples</a> project.</p>
</section>

<section>
<h2>Filter collection under test</h2>
<pre><code class="java" contenteditable>
<h2>Filter collection before assertions</h2>
<pre><code class="java">
// let's check who are the Hobbit in fellowshipOfTheRing
assertThat(fellowshipOfTheRing).filteredOn("race", HOBBIT)
.containsOnly(sam, frodo, pippin, merry);
Expand All @@ -155,19 +154,57 @@ <h2>Filter collection under test</h2>
assertThat(fellowshipOfTheRing).filteredOn("race", in(MAIA, MAN))
.containsOnly(gandalf, boromir, aragorn);

// Java 8 love : specify your filter condition with a Predicate
assertThat(fellowshipOfTheRing).filteredOn(hero -> hero.getName().contains("o"))
.containsOnly(aragorn, frodo, legolas, boromir);

</code></pre>
<p>Full assertions showcase in <a href="https://github.com/joel-costigliola/assertj-examples/tree/master/assertions-examples/src/test/java/org/assertj/examples">assertj-examples</a> project.</p>
</section>

<section>
<h2>Java 8 love</h2>

<pre><code class="java">
// matching using a Predicate
assertThat(frodo).matches(tc -> tc.getRace() == HOBBIT);

// extracting using a lambda
assertThat(fellowshipOfTheRing).extracting(tc -> tc.getRace().getName())
.contains("Hobbit", "Elf");

// filter using a Predicate
assertThat(fellowshipOfTheRing).filteredOn(tc -> tc.getName().contains("o"))
.contains(aragorn, frodo, legolas, boromir);

// Optional assertion
assertThat(Optional.of("Test")).isPresent()
.contains("Test");

</code></pre>
</section>
<section>
<h2>Java 8 love : testing exception</h2>

<pre><code class="java">
// call the code to test in a lambda
assertThatThrownBy(() -> { throw new Exception("boom!") })
.hasMessageContaining("boom");

// --- BDD Style ---
// GIVEN
String[] s = new String[0];

// WHEN
Throwable exception = catchThrowable(() -> System.out.println(s[7]));

// THEN
assertThat(exception).hasMessage("7");

</code></pre>
</section>

<section>
<h2>When equals is not good enough ... </h2>
<p>... use comparison by values</p>

<pre><code class="java" contenteditable>
<pre><code class="java">
TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
TolkienCharacter frodoClone = new TolkienCharacter("Frodo", 33, HOBBIT);

Expand All @@ -190,7 +227,7 @@ <h2>When equals is not good enough ... </h2>
<h2>When equals is not good enough ... </h2>
<p>... use a custom comparator</p>

<pre><code class="java" contenteditable>
<pre><code class="java">
// standard comparison : frodo is not equal to sam
assertThat(frodo).isNotEqualTo(sam);

Expand All @@ -213,7 +250,7 @@ <h2>When equals is not good enough ... </h2>
<section>
<h2>Custom assertions</h2>
<p>Express <a href="http://joel-costigliola.github.io/assertj/assertj-core-custom-assertions.html#custom-assertion-class">assertions</a> in the domain language</p>
<pre><code class="java" contenteditable>
<pre><code class="java">
// instead of:
assertThat(frodo.getName()).isEqualto("Frodo");
assertThat(frodo.getAge()).isEqualto(33);
Expand All @@ -229,8 +266,8 @@ <h2>Custom assertions</h2>
</section>

<section>
<h2>HAPI FHIR Custom assertions examples</h2>
<pre><code class="java" contenteditable>
<h2>Custom assertions examples</h2>
<pre><code class="java">
Observation obs1 = ...;
Observation obs2 = ... ;

Expand All @@ -252,14 +289,9 @@ <h2>HAPI FHIR Custom assertions examples</h2>
<section>
<h2>Using Condition</h2>
<p>Conditions are similar to Hamcrest matchers</p>
<pre><code class="java" contenteditable>
Condition&lt;String&gt; jedi = new Condition&lt;String&gt;("jedi") {
Set&lt;String&gt; jedis = newLinkedHashSet("Luke", "Yoda", "Obiwan");
@Override
public boolean matches(String actual) {
return jedis.contains(actual);
}
};
<pre><code class="java">
Set&lt;String&gt; JEDIS = newLinkedHashSet("Luke", "Yoda", "Obiwan");
Condition&lt;String&gt; jedi = new Condition&lt;&gt;(p -> JEDIS.contains(p), "jedi");

// use it
assertThat("Yoda").is(jedi);
Expand All @@ -281,9 +313,9 @@ <h2>More stuff </h2>
<ul>
<li>BDD assertions : use <i>then</i> as an alias of <i>assertThat</i></li>
<li>Soft assertions : record all assertion errors instead of stopping at the first one</li>
<li>assertj-core 3.x adds <a href="http://joel-costigliola.github.io/assertj/assertj-core-news.html#assertj-core-3.0.0">Java 8</a> support</li>
<li>assertj-joda-time : assertions for <i>DateTime</i> and <i>LocalDateTime</i></li>
<li>assertj-guava : assertions for <i>Multimap</i>, <i>Range</i>, <i>Optional</i> ...</li>
<li>assertj-db : assertions for relational databases</li>
<li>Lot of minor features ...</li>
</ul>
</section>
Expand Down

0 comments on commit 3b34e86

Please sign in to comment.