From df1d1f53600731e3604cbd0af182f0776dc62d0b Mon Sep 17 00:00:00 2001 From: viniberaldo Date: Sat, 28 Sep 2024 23:13:05 -0300 Subject: [PATCH] Created JUnit tests for Day007.class --- days/day007/pom.xml | 61 +++++++++++++++++++ .../ahundreddaysofjava/day007/Day007.java | 2 +- .../ahundreddaysofjava/day007/Day007Test.java | 39 ++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 days/day007/src/test/java/com/thegreatapi/ahundreddaysofjava/day007/Day007Test.java diff --git a/days/day007/pom.xml b/days/day007/pom.xml index 1d94514..f212836 100644 --- a/days/day007/pom.xml +++ b/days/day007/pom.xml @@ -10,6 +10,67 @@ 4.0.0 day007 + day007 + + UTF-8 + 21 + + + + + org.junit.jupiter + junit-jupiter-api + test + + + org.assertj + assertj-core + test + + + + + + + + maven-clean-plugin + 3.4.0 + + + maven-resources-plugin + 3.3.1 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 3.5.0 + + + maven-jar-plugin + 3.4.2 + + + maven-install-plugin + 3.1.3 + + + maven-deploy-plugin + 3.1.3 + + + maven-site-plugin + 3.20.0 + + + maven-project-info-reports-plugin + 3.7.0 + + + + \ No newline at end of file diff --git a/days/day007/src/main/java/com/thegreatapi/ahundreddaysofjava/day007/Day007.java b/days/day007/src/main/java/com/thegreatapi/ahundreddaysofjava/day007/Day007.java index 3043d36..d2254b3 100644 --- a/days/day007/src/main/java/com/thegreatapi/ahundreddaysofjava/day007/Day007.java +++ b/days/day007/src/main/java/com/thegreatapi/ahundreddaysofjava/day007/Day007.java @@ -8,7 +8,7 @@ public static void main(String[] args) { showMessage("Java", "is", "great"); } - private static void showMessage(String param1, String param2, String param3) { + public static void showMessage(String param1, String param2, String param3) { String message = MessageFormat.format("This message contains 3 parameters. The #1 is ''{0}'', the #2 is ''{1}'', and the #3 is ''{2}''.", param1, param2, param3); System.out.println(message); diff --git a/days/day007/src/test/java/com/thegreatapi/ahundreddaysofjava/day007/Day007Test.java b/days/day007/src/test/java/com/thegreatapi/ahundreddaysofjava/day007/Day007Test.java new file mode 100644 index 0000000..0194cc9 --- /dev/null +++ b/days/day007/src/test/java/com/thegreatapi/ahundreddaysofjava/day007/Day007Test.java @@ -0,0 +1,39 @@ +package com.thegreatapi.ahundreddaysofjava.day007; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.jupiter.api.AfterEach; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class Day007Test { + + private final PrintStream standardOut = System.out; + private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); + + @BeforeEach + public void setUp() { + System.setOut(new PrintStream(outputStreamCaptor)); + } + + @Test + void shouldFormatMessageCorrectly() { + // Arrange + String param1 = "Hello"; + String param2 = "World"; + String param3 = "Java"; + + // Act + Day007.showMessage(param1, param2, param3); + + // Assert + String expectedMessage = "This message contains 3 parameters. The #1 is 'Hello', the #2 is 'World', and the #3 is 'Java'."; + assertEquals(expectedMessage, outputStreamCaptor.toString().trim()); + } + + @AfterEach + public void tearDown() { + System.setOut(standardOut); + } +}