Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
  1. Create a new test project in the FindHighestNumber solution

  2. We’ll draft out a test defining what we think we want the production code to do

  3. Code Block
    languagejava
    public class TopicScoreWriterTest
    {
       @Test[Test]
       public   void  verify_topic_score_details_written_out_once()
       {
          // arrange
          String physics = "Physics";
    
          ArrayList<TopicTopScore>TopicTopScore[] topTopScores = new ArrayList<>();
          topTopScores.add( { new TopicTopScore(physics, 89)) }; 
          
          TopicScoreWriter cut = new TopicScoreWriter();
          
          // act
          cut.writeScores( topTopScores );
          
          // assert
       }
    }
  4. Line 10 represents the implementation class. We will call a method called writeScores() passing an array TopicTopScores. Since this method will not return a result that is testable in this context, how are we going to test the result of the writeScrores(), it's going to make a call to a Java API. We are not going to test the Java API call, many millions of developers have already done this. We can use the mocking libraries verify capability. But what are we going to verify?

  5. We’ll begin by decoupling the Java API call to write to the file stream from the TopicScorWriter. To do this could pass into the constructor a java.io.FileWriter object or something similar. But this would tie the production too tightly to java.io.FileWriter. Instead, let’s pass an object that encapsulates the file writer functionality. This object needs to support two capabilities 1) open a text file to write to, and 2) write the formatted string to the opened text file. With this design in mind, we can update our test and then dig a little deeper into the implementation code.

  6. Our updated test class now looks like this

  7. Code Block
    languagejava
    namespace TopicWriterTests
    {
        interface IFileWriter
        {
            void WriteLine(String lineToWrite);
        }
        public class TopicWriterTests
        {
            [Test]
            public void Verify_topic_score_details_written_out_once()
            {
                // arrange
                String physics = "Physics";
                String art = "Art";
                String compSci = "Comp Sci";
                String expectedResult = "Physics, 89";
                TopicTopScore[] topTopScores = { new TopicTopScore(physics, 89) };
                IFileWriter fileWriter = Substitute.For<IFileWriter>();
          
                TopicScoreWriter cut = new TopicScoreWriter(fileWriter);
                
                // act
                cut.WriteScores(topTopScores);
    
                // assert
                fileWriter.Received(1).WriteLine(expectedResult);
            }
        }
    }
  8. Notice the use of the interface at line 3. We then use this interface to create Mock object at line 18. At line 26 we are going to verify that the method WriteLine() is called once with a string formatted as shown at line 16.

  9. Move the IFileWriter interfaces into the same file as TopicScoreWriter

  10. Code Block
    namespace TopicManagerService
    {
        public interface IFileWriter
        {
            void WriteLine(string lineToWrite);
        }
  11. Let’s write the implementation code for TopicScoreWriter. We’ve used a canned result (line 20) to test our theory

  12. Code Block
    languagejava
       public class TopicScoreWriter
        {
            private IFileWriter fileWriter;
    
            public TopicScoreWriter(IFileWriter fileWriter)
            {
                this.fileWriter = fileWriter;
            }
    
            public void WriteScores(TopicTopScore[] topTopScores)
            {
                fileWriter.WriteLine("Physics, 89");
            }
        }
    }
  13. Run the test and it should pass

  14. We’ll now update WriteScores() so that it writes the data being passed

  15. Code Block
    languagejava
            public void WriteScores(TopicTopScore[] topTopScores)
            {
                if (topTopScores.Length > 0)
                {
                    TopicTopScore tts = topTopScores[0];
                    string dataToWrite = tts.TopicName + ", " + tts.TopScore;
                    fileWriter.WriteLine(dataToWrite);
                }
            }

  16. The if statement introduces a new logic path in the code, we should have test for this

  17. Code Block
    languagejava
            [Test]
            public void Verify_empty_array_topic_score_not_written()
            {
                // arrange
                String physics = "Physics";
                String art = "Art";
                String compSci = "Comp Sci";
                String expectedResult = "Physics, 89";
                TopicTopScore[] topTopScores = Array.Empty<TopicTopScore>();
                IFileWriter fileWriter = Substitute.For<IFileWriter>();
    
                TopicScoreWriter cut = new TopicScoreWriter(fileWriter);
                // act
                cut.WriteScores(topTopScores);
    
                // assert
                fileWriter.DidNotReceive().WriteLine(expectedResult);
            }
  18. Finally, add another test that writes more top scores for different topics

  19. Code Block
            [Test]
            public void Verify_topic_score_details_written_out_multiple_times()
            {
                // arrange
                string physics = "Physics";
                string art = "Art";
                string compSci = "Comp Sci";
                string physicsResult = "Physics, 89";
                string artsResult = "Art, 87";
                string comSciResult = "Comp Sci, 97";
    
                TopicTopScore[] topTopScores = { new TopicTopScore(physics, 89),
                                                 new TopicTopScore(art, 87),
                                                 new TopicTopScore(compSci, 97)};
    
                String fileToWrite = "testfile.txt";
                IFileWriter fileWriter = Substitute.For<IFileWriter>();
          
                TopicScoreWriter cut = new TopicScoreWriter(fileWriter);
    
                // act
                cut.WriteScores(topTopScores );
    
                // assert
                fileWriter.Received(1).WriteLine(physicsResult);
                fileWriter.Received(1).WriteLine(artsResult);
                fileWriter.Received(1).WriteLine(comSciResult);
            }
  20. And the implementation code has been updated to

  21. Code Block
    namespace TopicManagerService
    {
        public interface IFileWriter
        {
            void WriteLine(string lineToWrite);
        }
    
        public class TopicScoreWriter
        {
            private IFileWriter fileWriter;
    
            public TopicScoreWriter(IFileWriter fileWriter)
            {
                this.fileWriter = fileWriter;
            }
    
            public void WriteScores(TopicTopScore[] topTopScores)
            {
                foreach( TopicTopScore tts in topTopScores )
                {
                    string dataToWrite = tts.TopicName + ", " + tts.TopScore;
                    fileWriter.WriteLine(dataToWrite);
                }
            }
        }
    }