Reference no: EM133673717
Task - Unit test standards - AAA and [F]IRST
Overview
Unit tests provide many significant benefits when crafted with care. There are best practices of unit testing covered this week which talks about how tests can be organised such as AAA (Arrange, Act, Assert, After) or properties of good tests such as FIRST (Fast, Isolated, Repeatable, Selfvalidating, Timely). In this task a WeatherControllerTest class has been provided to test 3 functions of WeatherController. WeatherController stores today's hourly temperature values and calculates minimum, maximum and average of hourly temperature values and stores in cache for faster access. There is a function in WeatherController to query temperature for a given hour which is a slow operation, also, the WeatherController initialisation and shutdown operations are also slow process. You will need to follow AAA standard to organise 3 tests in WeatherControllerTest so that the Fast property of FIRST principle can be achieved. Identify use cases where following some of these unit testing standards are difficult to follow and recommendation with references from research articles or corporate blog posts.
Submission details
Use the instructions on the following page to carry out this task's steps.
For this task you will need to study the existing project task5_1P.zip which contains WeatherController.java and its test cases in WeatherControllerTest.java. You update the WeatherControllerTest.java file as required.
Submit a pdf combining below items
A screenshot of your Eclipse IDE's (i) JUnit tab which shows test statistics including Runs, Errors and Failures and (ii) Eclipse console which shows outputs.
Your program's source code for tests (WeatherControllerTest.java)
With proper references, identify use cases where unit testing standards are difficult to follow while creating test cases and possible work arounds.
A screenshot of your GitHub page where your latest project folder is pushed.
You want to focus on the following key ideas, and make sure you can explain them in relation to your program.
Keep tests consistent with AAA
FIRST properties of good tests
JUnit's support for AAA.
Instructions
For this task you will need to
Download task5_1P.zip Java project and unzip it in a common folder (say, java_projects) which you will be using to store all the weekly projects.
Import the project (as a maven project) in Eclipse IDE.
Observe WeatherController class structure in WeatherController.java file which contains today's hourly temperature values, currently 3 hours per day is set (see the HOURS_PER_DAY=3 field to increase number of hours up to 24, if needed). Member functions are summarised in the table below.
Functions Details
getTemperatureMinFromCache Cached statistics
getTemperatureMaxFromCache Cached statistics
getTemperatureAverageFromCache Cached statistics
getTemperatureForHour Retrieves temperature value for the queried
hour, slower response
persistTemperature Stores reported temperature value for the specified hour, it returns persistence
timestamp.
getTotalHours Number of hours temperature values are persisted (currently 3, controlled by the
constant HOURS_PER_DAY).
getInstance() and close() Slow responding functions to create and
destroy WeatherController class object.
One of the cache functions and the slow process of interest getTemperatureForHour() are shown below. As you can see, getTemperatureForHour
To keep things simple, the temperature values are randomly initialised in the constructor function during creation of WeatherController object instead of reading directly from sensors. This means, when the getInstance() is called for the first time or after calling the close() method, the WeatherController constructor executes and random temperature values are initialised.
A test function in WeatherControllerTest.java to test the cached minimum temperature value is shown below. The test function's body contains code to initialise the WeatherController, retrieves all the temperature values from controller by using the slow responding function getTemperatureForHour(int hour) for all the available hours in the controller. Compares the calculated minimum value with the controller's cached minimum value. Finally, it closes the controller.
The other 2 test functions testTemperatureMax() and testTemperatureAverage() share similar code structure. Note that the function testTemperaturePersist() is commented which is above Pass task and does not require for 5.1P task to change the function, you can leave it as it is.
If you run one of these tests, you will notice each of them takes more than 10 seconds. You restructure these 3 tests so that the tests can run as fast as possible. You can adopt techniques such as separating common initialisation and shutdown functions from the main test function; instead of querying hourly temperature values for each test, store them locally so they can be reused across these tests. If you consider reusing WeatherController function's getInstance() across test functions, keep in mind not to call close() function which will create a new instance and a new random set of temperature values will be initialised and eventually WeatherController's cached functions (min, max or average temperatures) will show different values. It is advised to study the behavior of WeatherController in case of any confusion.
There are 2 test failures that you need to correct by providing your name and id in the first 2 test cases.
Run the test, take screenshot of test statistic and the console output.
Upload your folder to your GitHub account and take a screenshot.
Your Task
Your task is to:
Study WeatherController.java to understand the member functions of the controller as outlined in a table above.
Study WeatherControllerTest.java to understand the 3 test cases (all but the testTemperaturePersist for this task). They share some common codes in addition to the main test logic.
Run these tests and observe the time taken to complete these tests.
You need to restructure the tests using AAA standards so these tests can run faster.