Skip to content

Commit

Permalink
Changed writeCsv tests by @ParametrizedTest
Browse files Browse the repository at this point in the history
  • Loading branch information
HovorukhaBohdan committed Oct 26, 2023
1 parent d8e974e commit 7aca24d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 56 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<plugins>
Expand Down
1 change: 1 addition & 0 deletions report.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
fruit,quantity
banana,10
apple,100
pineapple,111
grape,150
melon,10
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class WriteToCsvFileServiceImplTest {
private static final String FIRST_FILENAME = "fruits1.csv";
Expand Down Expand Up @@ -54,10 +56,15 @@ void setUp() {
builder = new StringBuilder();
}

@Test
void csvWriter_twoFruits_Ok() {
@ParameterizedTest // idk, can I use it?
@ValueSource(strings = {
FIRST_FILENAME,
SECOND_FILENAME,
THIRD_FILENAME
})
void csvWriter_Fruits_Ok(String filename) {
dataProcessor.processFruits(
dataConverter.convert(csvReader.readFile(FIRST_FILENAME)));
dataConverter.convert(csvReader.readFile(filename)));

String expected = reportCreator.createReport();
csvWriter.write(expected);
Expand All @@ -77,57 +84,4 @@ void csvWriter_twoFruits_Ok() {

assertEquals(expected, actual);
}

@Test
void csvWriter_threeFruits_Ok() {
dataProcessor.processFruits(
dataConverter.convert(csvReader.readFile(SECOND_FILENAME)));

String expected = reportCreator.createReport();
csvWriter.write(expected);

try (BufferedReader reader = new BufferedReader(new FileReader(REPORT_FILENAME))) {
String line = reader.readLine();

while (line != null) {
builder.append(line)
.append(System.lineSeparator());
line = reader.readLine();
}
} catch (IOException e) {
throw new RuntimeException("Cannot find file: " + REPORT_FILENAME);
}
String actual = builder.toString().trim();

assertEquals(expected, actual);
}

@Test
void csvWriter_fourFruits_Ok() {
dataProcessor.processFruits(
dataConverter.convert(csvReader.readFile(THIRD_FILENAME)));

String expected = reportCreator.createReport();
csvWriter.write(expected);

try (BufferedReader reader = new BufferedReader(new FileReader(REPORT_FILENAME))) {
String line = reader.readLine();

while (line != null) {
builder.append(line)
.append(System.lineSeparator());
line = reader.readLine();
}
} catch (IOException e) {
throw new RuntimeException("Cannot find file: " + REPORT_FILENAME);
}
String actual = builder.toString().trim();

assertEquals(expected, actual);
}

@AfterEach
void tearDown() {
Storage.fruitsCount.clear();
}
}

0 comments on commit 7aca24d

Please sign in to comment.