...
Begin by creating a new test
Code Block | ||
---|---|---|
| ||
@Test public void find_heighest_score_with_array_of_one_return_array_of_one_using_stub() { // Arrange int[] scores = { 56, 67, 45, 89 }; String topicName = "Physics"; ArrayList<TopicScores> topicScores = new ArrayList<>(); topicScores.add(new TopicScores(topicName, scores)); // Use a stub version of HighestNumberFinder com.s2s.demos.topicmanager.HighestNumberFinder hnf = new com.s2s.demos.topicmanager.HighestNumberFinder(); TopicManager cut = new TopicManager(hnf); ArrayList<TopicTopScore> expectedResult = new ArrayList<>(); expectedResult.add(new TopicTopScore(topicName, 89)); // Act ArrayList<TopicTopScore> result = cut.findTopicHighScores(topicScores); assertEquals(expectedResult.get(0).getTopicName(), result.get(0).getTopicName()); assertEquals(expectedResult.get(0).getTopScore(), result.get(0).getTopScore() ); } |
There will be a number of errors in the code, we will eliminate them one by one
Here is the Stub you will use (create it in the test folder - it’s not production code)
...
The stub is substituted in line 1311.
But when you try and use this class in the tests, you should see type errors. This is because, in the implementation unit of TopicManager
, is typed against com.s2s.demos.findhighestnumber.fin.HighestNumberFinder
. So namespaces packages do not offer us the solution we are looking for.
We need to use interfaces. And the interface needs to be in a place that is available to both production code and testsAlternatively, you could modify TopicManager
so it has a default constructor, but you will need to think about different issues and more tests for if TopicManager
default constructor is used.
Create a new interface that is part of the production code, and create it in the
com.s2s.demos.findhighestnumber
packageCode Block language c# 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 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 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 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.
...