-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Added solution #820
base: main
Are you sure you want to change the base?
Added solution #820
Conversation
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.
unify all test method names
they should end notOk
user lowercase after underscore. By underscore we separate not words, but logical components of the name - _
} | ||
|
||
@Test | ||
@Order(1) |
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.
it's generally better to write independent and isolated test methods whenever possible, as test method order dependencies can make your test suite more brittle and harder to maintain
URL resources = classLoader.getResource(INPUT_1_FILE_NAME); | ||
List<String> readFromFile = fileReader.readFromFile(new File(resources.getFile())); | ||
List<FruitTransaction> fruitTransactions = parseService.parse(readFromFile); | ||
for (FruitTransaction fruitTransaction : fruitTransactions) { | ||
operationStrategy.get(fruitTransaction.getOperation()) | ||
.handle(fruitTransaction.getFruit(), | ||
fruitTransaction.getQuantity()); | ||
} | ||
String actual = reportService.generateReport(); | ||
String expected = HEADER + System.lineSeparator() | ||
+ "apple" + SEPARATOR + 90 + System.lineSeparator() | ||
+ "banana" + SEPARATOR + 152 + System.lineSeparator() | ||
+ "pear" + SEPARATOR + 140; | ||
assertEquals(expected, actual); |
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.
a lot of duplicated logic, extract to a private method like this
private void performTest(String fileName, String expectedReport) {
URL resources = classLoader.getResource(fileName);
List<String> readFromFile = fileReader.readFromFile(new File(resources.getFile()));
List<FruitTransaction> fruitTransactions = parseService.parse(readFromFile);
for (FruitTransaction fruitTransaction : fruitTransactions) {
operationStrategy.get(fruitTransaction.getOperation())
.handle(fruitTransaction.getFruit(),
fruitTransaction.getQuantity());
}
String actual = reportService.generateReport();
assertEquals(expectedReport, actual);
}
List<FruitTransaction> expected = new ArrayList<>(List.of( | ||
new FruitTransaction("pear", 200, FruitTransaction.Operation.BALANCE), | ||
new FruitTransaction("apple", 150, FruitTransaction.Operation.BALANCE), | ||
new FruitTransaction("banana", 100, FruitTransaction.Operation.BALANCE), | ||
new FruitTransaction("pear", 80, FruitTransaction.Operation.PURCHASE), | ||
new FruitTransaction("banana", 25, FruitTransaction.Operation.SUPPLY), | ||
new FruitTransaction("apple", 10, FruitTransaction.Operation.RETURN), | ||
new FruitTransaction("pear", 20, FruitTransaction.Operation.RETURN), | ||
new FruitTransaction("banana", 5, FruitTransaction.Operation.PURCHASE), | ||
new FruitTransaction("banana", 15, FruitTransaction.Operation.SUPPLY), | ||
new FruitTransaction("banana", 20, FruitTransaction.Operation.PURCHASE))); |
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.
move this out of the method, it's hard to read
URL resources = classLoader.getResource(INPUT_1_FILE_NAME); | ||
List<String> readFromFile = fileReader.readFromFile(new File(resources.getFile())); | ||
List<FruitTransaction> fruitTransactions = parseService.parse(readFromFile); | ||
for (FruitTransaction fruitTransaction : fruitTransactions) { | ||
operationStrategy.get(fruitTransaction.getOperation()) | ||
.handle(fruitTransaction.getFruit(), | ||
fruitTransaction.getQuantity()); | ||
} | ||
assertEquals(FruitDaoImpl.getFruits().get("banana"), 152); | ||
assertEquals(FruitDaoImpl.getFruits().get("apple"), 90); |
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.
same, your method look very similar, avoid code duplication
No description provided.