An organisation delivers several topics (subjects). Students are graded against each topic. You are required to store the top score for each topic.
...
If the input were {4, 5, -8, 3, 11, -21, 6} the result should be 11
An empty array should throw an exception
A single-item array should return the single item
If several numbers are equal and highest, only one should be returned
If the input were {7, 13} then the result should be 13
If the input were {13, 4} then the result should be 13
Steps
Begin by creating a solution called FindHighesNumber with a testing project called FindHighestNumberTests (use the NUnit testing framework)Rename the test new maven project called
HighesNumberServices
Create a package in the
test
folder calledcom.demos.findhighestnumber
Create a test class called to
HighestNumberFinderTests
The most challenging part is determining which test to write first. Always start simple and with a test that will not need to handle exceptions.
So the simplest test we could do here is
A single-item array should return the single item
Write this first test, let’s call the test method array_of_one_item_returns_this_item()
Code Block language c# java package com.s2s.demos.findhighestnumber.v1; import org.junit.Test; import static org.junit.Assert.*; /** * * @author selvy */ public class HighestNumberFinderTest { // TODO add test methods here. // The methods must be annotated with annotation @Test. For example: [Test]// @Test public void Arrayfind_highest_in_array_of_one_itemexpect_returns_thissingle_item() { { // Arrange // Arrange int array[] = int[] values = { 33 }; 10 int expectedResult = 33; }; HighestNumberFinder cut = new HighestNumberFinder(); int expectedResult = 10; // Act // Act int result = cut.findHighestNumber(values array ); // Assert assertEquals( Assert.That(result, Is.EqualTo(expectedResult)expectedResult, result ); } }
Create a new project called FindHighestNumberService in the same solution
Rename the production class to HighestNumberFinder
Depending on the IDE you are using, most will allow you to create a class or method that doesn’t currently exist. In the Test file HighestNumberFinderTests, try right-click clicking on the HighestNumberFinder declaration and select Add reference to HighestNumberFinder, the error should disappear
In the Test file HighestNumberFinderTests, right-click on the method findHighestNumber() and select Generate method
and and see if there is an option to create the missing class. Make sure to create the class in the src folder and not the test folder
Try the same technique to create the method
Following a TDD approach, we get the IDE generate the production methods that match the tests
Now begin to work on the production code
Write enough production code to pass the test.
Do not be tempted to try and answer other parts of the requirements. Focus only on this requirement “A single item array should return the single item”
Code Block language c# using System; namespace FindHighestNumberService { public class HighestNumberFinder { public int findHighestNumber(int[] valuesarray) { return valuesarray[0]; } } }
Make sure the test passes
A Golden rule of TDD - if this was the only requirement then you have completed your task. Only write enough code to pass the test.
Commit your passing code to your git repo (never commit broken code)
Select the next requirement
I would suggest this one - If the input were {13, 4} then the result should be 13
Write the second test, let’s call the test method array_of_two_descending_items_return_first_item()
Code Block language c# @Test [Test] public void public void Arrayfind_highest_in_array_of_two_descending_itemsexpect_return_first_itemelement() { // Arrange int intarray[] values = { 13, 4 }; int expectedResult = 13; HighestNumberFinder cut = new HighestNumberFinder(); // Act // Act int result = cut.findHighestNumber(valuesarray); //Assert // Assert Assert.That(result, Is.EqualTo(expectedResult)); assertEquals(expectedResult, result); }
Write enough production code to pass the test. In this edge case, the production code does not change
Make sure the test passes
Commit your passing code to your git repo (never commit broken code)
Select the next requirement
I would suggest - If the input were {7, 13} then the result should be 13
Write the third test, let’s call the test method array_of_two_ascending_items_return_last_item()
Code Block language c# [Test] public void Array_of_two_ascending_items_return_last_item() { // Arrange int[] values = { 7, 13 }; int expectedResult = 13; HighestNumberFinder cut = new HighestNumberFinder(); // Act int result = cut.findHighestNumber(values); //Assert Assert.That(result, Is.EqualTo(expectedResult)); }
Write enough production code to pass the test. Do not be tempted to try and answer the parts of the requirements. Focus only on this requirement “If the input were {7, 13} then the result should be 13”
Code Block language c# public int findHighestNumber(int[] valuesarray) { int resulthighestSoFar = valuesarray[0]; if( (valuesarray.Lengthlength > 1 && valuesarray[1] > resulthighestSoFar ) highestSoFar result = valuesarray[1]; return result; return highestSoFar; }
Make sure the test passes
Commit your passing code to your git repo (never commit broken code)
The Production code has changed. Does it need to be refactored?
if yes, refactor the code
Make sure the test still passes
Commit your passing code to your git repo (never commit broken code)
Select each requirement and implement the test first then the production code
The steps above are known as RED, GREEN, REFACTOR
Info |
---|