JStonham / budjen

MIT License
0 stars 0 forks source link

Use dependency injection to test Application #10

Closed kinbiko closed 6 years ago

kinbiko commented 6 years ago

Introduce two new interfaces, Logger and TransactionData, and take advantage of these to be able to test the hitherto untested Application class. The previous Logger is now DefaultLogger and the previous TransactionData is now DefaultTransactionData.

Dependency injection:

Fancy way of saying "The fields I need should be given to me in a constructor". I use this to set a different Logger and TransactionData in the ApplicationTest, where I define my own implementations of the Logger and TransactionData.

This meant that I had to change the main method in Budjen to give the Application an instance of a DefaultLogger and a DefaultTransactionData. If I really wanted to maintain the tiny main method I could strictly speaking create yet another class to wrap around that logic. I decided against that approach as the goal of a small main method is to limit the use of static. Seeing as constructors are effectively static as well (you can't call the constructor on an instance of a class) this seemed reasonable. This 'constructors-are-static' is a subtle technicality, so you might just have to take my word for it. Happy to discuss in more detail in our session if you'd like, but you definitely don't need to have this level of insight anytime soon.

Reviewing this PR:

Please read carefully.

I've left 3 intentional, and X unintentional mistakes in this PR. Any mistakes in this PR description are unintentional, and may be ignored unless utterly confusing.

The intentional mistakes are of the following format:

Please leave constructive comments wherever you find something you wish to improve. I.e. don't just say that something is incorrect, but instead suggest an improvement. This is much harder, and worth practicing.

Please also leave any questions you might have as review comments.

Leave all comments as line comments by clicking the blue + that appears when hovering over line numbers. Note that this only works on changed (red or green) lines. Once you're done with the review remember to click the green 'Review changes' in the top right of the "Files changes". I will not see any of your comments until you do this last step.

Choose "Request changes" the first time around. I will then push one or more commits addressing your comments. If you are then happy with the changes select "Approve" or "Request changes" if you still have further review comments.