Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Here is the Stub you will use (create it in the test folder - it’s not production code)

Code Block
languagec#java
package com.s2s.demos.topicmanager;

public class HighestNumberFinder
{
    public int findHighestNumber(int[] array)
    {
        return 89;
    }
}

...

  1. Create a new interface that is part of the production code, and create it in the com.s2s.demos.findhighestnumber package

  2. Code Block
    languagec#java
    package com.s2s.demos.findhighestnumber;
    
    public interface IHighestNumberFinder
    {
       int findHighestNumber(int[] values);
    }
  3. Modify the production version and test version of HighestNumberFinder, so that they both implement the interface IHighestNumberFinder

  4. Here is the stub version

  5. Code Block
    languagejava
    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;
        }
    }
    
  6. Here is the production version

  7. Code Block
    languagejava
    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;
        }    
    }
  8. Refactor TopicManager so it now works with the interface IHighestNumberFinder and not directly with the implementation class

  9. Code Block
    languagejava
    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;
       }
       ...
  10. You should find that the test is no longer in an error state

  11. Rerun all your tests they should still be passing.

...