From b0be6dbf36e521ed39d4400cda5a8d3fdeaca45e Mon Sep 17 00:00:00 2001 From: michaelin007 Date: Sat, 9 Dec 2023 18:10:51 +0000 Subject: [PATCH 1/2] Failure Threshold Support for @RepeatedTest in Junit --- .../FailureThresholdUnitTest.java | 21 +++++++++++++++++++ .../junit5-annotations/test_file.txt | 1 + 2 files changed, 22 insertions(+) create mode 100644 testing-modules/junit5-annotations/src/test/java/com/baeldung/junit5/failurethreshold/FailureThresholdUnitTest.java create mode 100644 testing-modules/junit5-annotations/test_file.txt diff --git a/testing-modules/junit5-annotations/src/test/java/com/baeldung/junit5/failurethreshold/FailureThresholdUnitTest.java b/testing-modules/junit5-annotations/src/test/java/com/baeldung/junit5/failurethreshold/FailureThresholdUnitTest.java new file mode 100644 index 0000000000..8f0043cf51 --- /dev/null +++ b/testing-modules/junit5-annotations/src/test/java/com/baeldung/junit5/failurethreshold/FailureThresholdUnitTest.java @@ -0,0 +1,21 @@ +package com.baeldung.junit5.failurethreshold; + +import org.junit.jupiter.api.RepeatedTest; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +class FailureThresholdUnitTest { + + @RepeatedTest(value = 5, failureThreshold = 1) + void givenTextFile_whenItIsReadAndContainsSpecifiedWordRepeatedly_thenMatchingLinesFound() throws IOException { + String filePath = "test_file.txt"; + try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { + String line = reader.readLine(); + assertTrue(line.contains("The")); + } + } +} diff --git a/testing-modules/junit5-annotations/test_file.txt b/testing-modules/junit5-annotations/test_file.txt new file mode 100644 index 0000000000..f29ab3685b --- /dev/null +++ b/testing-modules/junit5-annotations/test_file.txt @@ -0,0 +1 @@ +The Efficacy of meta-cognitive therapy on the symptoms of obsessive-compulsive disorder in patients with multiple sclerosis \ No newline at end of file From 62195e14d45b2d78424ac691b2eb482f1fde334b Mon Sep 17 00:00:00 2001 From: michaelin007 Date: Mon, 11 Dec 2023 06:18:58 +0000 Subject: [PATCH 2/2] Failure Threshold Support for @RepeatedTest in Junit --- .../FailureThresholdUnitTest.java | 17 ++++++----------- .../junit5-annotations/test_file.txt | 1 - 2 files changed, 6 insertions(+), 12 deletions(-) delete mode 100644 testing-modules/junit5-annotations/test_file.txt diff --git a/testing-modules/junit5-annotations/src/test/java/com/baeldung/junit5/failurethreshold/FailureThresholdUnitTest.java b/testing-modules/junit5-annotations/src/test/java/com/baeldung/junit5/failurethreshold/FailureThresholdUnitTest.java index 8f0043cf51..de7651ece6 100644 --- a/testing-modules/junit5-annotations/src/test/java/com/baeldung/junit5/failurethreshold/FailureThresholdUnitTest.java +++ b/testing-modules/junit5-annotations/src/test/java/com/baeldung/junit5/failurethreshold/FailureThresholdUnitTest.java @@ -1,21 +1,16 @@ package com.baeldung.junit5.failurethreshold; import org.junit.jupiter.api.RepeatedTest; - -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; +import java.util.Random; import static org.junit.jupiter.api.Assertions.assertTrue; class FailureThresholdUnitTest { + Random random = new Random(); - @RepeatedTest(value = 5, failureThreshold = 1) - void givenTextFile_whenItIsReadAndContainsSpecifiedWordRepeatedly_thenMatchingLinesFound() throws IOException { - String filePath = "test_file.txt"; - try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { - String line = reader.readLine(); - assertTrue(line.contains("The")); - } + @RepeatedTest(value = 10, failureThreshold = 2) + void givenRandomNumberGenerator_whenGeneratingRandomNumber_thenNumberShouldBeWithinRange() { + int number = random.nextInt(10); + assertTrue(number <= 9); } } diff --git a/testing-modules/junit5-annotations/test_file.txt b/testing-modules/junit5-annotations/test_file.txt deleted file mode 100644 index f29ab3685b..0000000000 --- a/testing-modules/junit5-annotations/test_file.txt +++ /dev/null @@ -1 +0,0 @@ -The Efficacy of meta-cognitive therapy on the symptoms of obsessive-compulsive disorder in patients with multiple sclerosis \ No newline at end of file