July 2023
Intermediate to advanced
276 pages
6h 55m
English
Reading data from a file is a common operation in programming and I’m sure you’ve done that countless number of times. But the code to perform that operation was rather verbose and messy in the past. Let’s take a look at a function that reads the contents of a file and counts the number of occurrences of a word in it. Then we’ll refactor to use the functional style.
Let’s start with a small set of tests to verify that a countInFile function works as expected:
| | public class WordCountTest { |
| | WordCount wordCount; |
| | |
| | @BeforeEach |
| | public void init() { |
| | wordCount = new WordCount(); |
| | } |
| | |
| | @Test |
| | public void count() { |
| | assertAll( |
| | () -> assertEquals(2, |
| | wordCount.countInFile( ... |
Read now
Unlock full access