I have to write a program that takes an Integer and converts it into its English word version.
For example:
Input: 21
Output: twenty one
Input: 110
Output: one hundred and ten
I need the program to demonstrate TDD so I want to use mocking.
I have written a class that has a function that does the conversion (based on 2 arrays of English words). What I need now to design the program in such a way that I can demonstrate with Easymock.
Therefore, I need create an interface to be the subject of my mock. Can anyone give me any pointers on how I would design my program?
Would this be suitable?
- Write a
Converterclass that has a reference to an interface calledConverterInterface. I could then mock the interface and set it into myConverterclass.
Any help is welcome.
You can demonstrate TDD without mocking. In fact, mocking can confuse people that are new to TDD. I would simply start by test driving the functionality you are trying to develop, and then worry about mocking later. Let’s assume you’ve test driven the numeric to English converter (which it doesn’t appear you have done yet based on your description) and you have a class that looks something like this:
You are likely to also have some sort of main class:
You have now demonstrated TDD without mocking. In trying to practice TDD the mocking question will invariably come up. In order to demonstrate mocking, you can add some arbitrary business rule around the
toEnglishmethod such as “All requests for numbers larger than 1000 must be logged to the large number department”. Knowing that the large number department is hosted on another server that we don’t want to have a dependency on we can test drive the interface and mock it out.