...
Here is the Stub you will use (create it in the test folder - it’s not production code)
Code Block | ||
---|---|---|
| ||
package com.s2s.demos.topicmanager; public class HighestNumberFinder { public int findHighestNumber(int[] array) { return 89; } } |
...
Create a new interface that is part of the production code, and create it in the
com.s2s.demos.findhighestnumber
packageCode Block language c#java package com.s2s.demos.findhighestnumber; public interface IHighestNumberFinder { int findHighestNumber(int[] values); }
Modify the production version and test version of HighestNumberFinder, so that they both implement the interface
IHighestNumberFinder
Here is the stub version
Code Block language java package com.s2s.demos.topicmanager; import com.s2s.demos.findhighestnumber.IHighestNumberFinder; // The STUB version public class HighestNumberFinder implements IHighestNumberFinder { @Override public int findHighestNumber(int[] array) { return 89; } }
Here is the production version
Code Block language java package com.s2s.demos.findhighestnumber.fin; import com.s2s.demos.findhighestnumber.IHighestNumberFinder; public class HighestNumberFinder implements IHighestNumberFinder { @Override public int findHighestNumber(int[] array) { int highestSoFar = Integer.MIN_VALUE; for( int val : array ) { if( val > highestSoFar ) highestSoFar = val; } return highestSoFar; } }
Refactor
TopicManager
so it now works with the interfaceIHighestNumberFinder
and not directly with the implementation classCode Block language java package com.s2s.demos.topicmanager; import com.s2s.demos.findhighestnumber.IHighestNumberFinder; import java.util.ArrayList; class TopicManager { private final IHighestNumberFinder highestNumberFinder; public TopicManager() { this.highestNumberFinder = null; } public TopicManager( IHighestNumberFinder hnf ) { highestNumberFinder = hnf; } ...
You should find that the test is no longer in an error state
Rerun all your tests they should still be passing.
...