Skip to content

Commit

Permalink
Created JUnit tests for Day007.class
Browse files Browse the repository at this point in the history
  • Loading branch information
Viniberaldo committed Sep 29, 2024
1 parent 7ffef63 commit 0b189bc
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
12 changes: 12 additions & 0 deletions days/day007/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,17 @@

<artifactId>day007</artifactId>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.thegreatapi.ahundreddaysofjava.day007;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.text.MessageFormat;
import static org.assertj.core.api.Assertions.assertThat;
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
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
String expectedMessage = "This message contains 3 parameters. The #1 is"
+ " 'Hello', the #2 is 'World', and the #3 is 'Java'.";
assertThat(actualMessage).isEqualTo(expectedMessage);
//assertEquals(expectedMessage, outputStreamCaptor.toString().trim());
}

@AfterEach
public void tearDown() {
System.setOut(standardOut);
}
}

0 comments on commit 0b189bc

Please sign in to comment.