From 8dce57b64f061888271e15686e85a530fec2ed94 Mon Sep 17 00:00:00 2001 From: andresluzu Date: Mon, 11 Oct 2021 19:00:54 -0500 Subject: [PATCH] BAEL-5210 JUnit fail assertions use cases (#11314) Co-authored-by: Andres Luzuriaga --- .../baeldung/junit/FailAssertionUnitTest.java | 72 +++++++++++++++++++ .../com/baeldung/FailAssertionUnitTest.java | 50 +++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 testing-modules/junit-4/src/test/java/com/baeldung/junit/FailAssertionUnitTest.java create mode 100644 testing-modules/junit-5/src/test/java/com/baeldung/FailAssertionUnitTest.java diff --git a/testing-modules/junit-4/src/test/java/com/baeldung/junit/FailAssertionUnitTest.java b/testing-modules/junit-4/src/test/java/com/baeldung/junit/FailAssertionUnitTest.java new file mode 100644 index 0000000000..f85dc1914a --- /dev/null +++ b/testing-modules/junit-4/src/test/java/com/baeldung/junit/FailAssertionUnitTest.java @@ -0,0 +1,72 @@ +package com.baeldung.junit; + +import org.junit.Ignore; +import org.junit.Test; + +import java.util.Random; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +public class FailAssertionUnitTest { + + public static final Random RANDOM = new Random(); + + @Test + @Ignore + public void incompleteTest() { + fail("Not yet implemented"); + } + + @Test + public void expectedException() { + try { + methodThrowsException(); + fail("Expected exception was not thrown"); + } catch (Exception e) { + assertNotNull(e); + } + } + + @Test + public void unexpectedException() { + try { + safeMethod(); + // more testing code + } catch (Exception e) { + fail("Unexpected exception was thrown"); + } + } + + @Test + public void testingCondition() { + int result = randomInteger(); + if (result > Integer.MAX_VALUE) { + fail("Result cannot exceed integer max value"); + } + // more testing code + } + + @Test + public void returnBefore() { + int value = randomInteger(); + for (int i = 0; i < 5; i++) { + // returns when (value + i) is an even number + if ((i + value) % 2 == 0) { + return; + } + } + fail("Should have returned before"); + } + + private void safeMethod() { + } + + private void methodThrowsException() throws Exception { + throw new Exception(); + } + + private int randomInteger() { + return RANDOM.nextInt(Integer.MAX_VALUE); + } +} diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/FailAssertionUnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/FailAssertionUnitTest.java new file mode 100644 index 0000000000..8dfa99ab3c --- /dev/null +++ b/testing-modules/junit-5/src/test/java/com/baeldung/FailAssertionUnitTest.java @@ -0,0 +1,50 @@ +package com.baeldung; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.function.Supplier; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.fail; + +public class FailAssertionUnitTest { + + @Test + void failThrowable() { + try { + safeMethod(); + // more testing code + } catch (Exception e) { + fail(e); + } + } + + @Test + void failMessageThrowable() { + try { + safeMethod(); + // more testing code + } catch (Exception e) { + fail("Unexpected exception was thrown", e); + } + } + + @Test + @Disabled + void failMessageSupplier() { + Supplier timedMessage = () -> DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDate.now()); + fail(timedMessage); + } + + @Test + void genericType() { + Stream.of().map(entry -> fail("should not be called")); + } + + private void safeMethod() { + return; + } +}