-
-
Notifications
You must be signed in to change notification settings - Fork 73
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: Helber Belmiro <[email protected]>
Co-authored-by: Helber Belmiro <[email protected]>
Co-authored-by: Helber Belmiro <[email protected]>
Co-authored-by: Helber Belmiro <[email protected]>
Hi @hbelmiro, I'm quoting you to see my pull request 😄 |
There was a problem hiding this 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
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.release>21</maven.compiler.release> | ||
</properties> |
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not needed.
<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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not needed.
@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()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please change it to:
@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) { |
There was a problem hiding this comment.
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.
This pull request introduces a new JUnit test class (Day007Test.java) to verify the functionality of the Day007 class.
Key changes:
The test verifies that the showMessage() method correctly formats the message using MessageFormat.
Benefits:
Please review the test implementation and provide feedback if needed.