Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JUnit test for Day007.class #181

Open
wants to merge 20 commits into
base: main
Choose a base branch
from

Conversation

Viniberaldo
Copy link
Contributor

This pull request introduces a new JUnit test class (Day007Test.java) to verify the functionality of the Day007 class.

Key changes:

  • Created Day007Test.java in src/test/java/com/thegreatapi/ahundreddaysofjava/day007
  • Changed the visibility of the showMessage method in the Day007 class, from private to public
  • Added a single test method "shouldFormatMessageCorrectly()" to test the showMessage() method
  • Implemented the test using JUnit 5 and AssertJ for more readable assertions

The test verifies that the showMessage() method correctly formats the message using MessageFormat.

Benefits:

  • Ensures proper formatting of the message
  • Provides confidence in the correctness of the showMessage() method

Please review the test implementation and provide feedback if needed.

@Viniberaldo
Copy link
Contributor Author

Hi @hbelmiro, I'm quoting you to see my pull request 😄

Copy link
Owner

@hbelmiro hbelmiro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this contribution @Viniberaldo.
The idea is to replace the existing code with tests. So, you need to move the example to the test and remove the original class.
Also, since we'll use tests, we can change the example to not print the formatted message. In the original code, I've printed it to the console so people can see the result. Now that we'll have a test, the test itself is the verification. So all the boilerplate you had to add to verify the output can be removed.
See what I did here, for example: https://github.com/hbelmiro/100DaysOfJava/pull/62/files

Comment on lines +15 to +18
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>21</maven.compiler.release>
</properties>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed. It's already set in the root pom.xml.

@@ -10,6 +10,67 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>day007</artifactId>
<name>day007</name>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed.

Comment on lines +33 to +74
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.4.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.3</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.3</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.20.0</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.7.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed.

Comment on lines +20 to +33
@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());
}
Copy link
Owner

@hbelmiro hbelmiro Sep 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change it to:

Suggested change
@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());
}
@Test
void shouldFormatMessageCorrectly() {
// Arrange
String param1 = "Hello";
String param2 = "World";
String param3 = "Java";
// Act
String actualMessage = MessageFormat.format("This message contains 3 parameters. The #1 is ''{0}'', the #2 is ''{1}'', and the #3 is ''{2}''.",
param1, param2, param3);
// Assert
assertThat(actualMessage).isEqualTo("This message contains 3 parameters. The #1 is 'Hello', the #2 is 'World', and the #3 is '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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should be removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants