|
| 1 | +package org.codefx.lab.junitlambda; |
| 2 | + |
| 3 | +import org.junit.gen5.api.Test; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.util.function.BooleanSupplier; |
| 7 | + |
| 8 | +import static org.junit.gen5.api.Assertions.assertAll; |
| 9 | +import static org.junit.gen5.api.Assertions.assertEquals; |
| 10 | +import static org.junit.gen5.api.Assertions.assertFalse; |
| 11 | +import static org.junit.gen5.api.Assertions.assertNotEquals; |
| 12 | +import static org.junit.gen5.api.Assertions.assertNotNull; |
| 13 | +import static org.junit.gen5.api.Assertions.assertNotSame; |
| 14 | +import static org.junit.gen5.api.Assertions.assertNull; |
| 15 | +import static org.junit.gen5.api.Assertions.assertSame; |
| 16 | +import static org.junit.gen5.api.Assertions.assertTrue; |
| 17 | +import static org.junit.gen5.api.Assertions.expectThrows; |
| 18 | + |
| 19 | +public class _1_Assertions { |
| 20 | + |
| 21 | + @Test |
| 22 | + public void boringAssertions() { |
| 23 | + String mango = "Mango"; |
| 24 | + |
| 25 | + // as usual: expected, actual |
| 26 | + assertEquals("Mango", mango); |
| 27 | + assertNotEquals("Banana", mango); |
| 28 | + assertSame(mango, mango); |
| 29 | + assertNotSame(new String(mango), mango); |
| 30 | + |
| 31 | + assertNull(null); |
| 32 | + assertNotNull(mango); |
| 33 | + assertFalse(false); |
| 34 | + assertTrue(true); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void interestingAssertions() { |
| 39 | + String mango = "Mango"; |
| 40 | + |
| 41 | + // message comes last |
| 42 | + assertEquals("Mango", mango, "Y U no equal?!"); |
| 43 | + |
| 44 | + // message can be created lazily |
| 45 | + assertEquals("Mango", mango, () -> "Expensive string, creation deferred until needed."); |
| 46 | + |
| 47 | + // for 'assert[True|False]' it is possible to directly test a supplier that exists somewhere in the code |
| 48 | + BooleanSupplier existingBooleanSupplier = () -> true; |
| 49 | + assertTrue(existingBooleanSupplier); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void exceptionAssertions() { |
| 54 | + IOException exception = expectThrows( |
| 55 | + IOException.class, |
| 56 | + () -> { |
| 57 | + throw new IOException("Something bad happened"); |
| 58 | + }); |
| 59 | + assertTrue(exception.getMessage().contains("Something bad")); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void groupedAssertions() { |
| 64 | + assertAll("Multiplication", |
| 65 | + () -> assertEquals(15, 3 * 5, "3 x 5 = 15"), |
| 66 | + // this fails on purpose to see what the message looks like |
| 67 | + () -> assertEquals(15, 5 + 3, "5 x 3 = 15") |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | +} |
0 commit comments