CPS3222 - Assignment 1: Unit Testing

advertisement
CPS3222 - Assignment 1: Unit Testing
Assessment Weighting: Marked out of 100 but contributes to 25% of the study unit mark
th
Deadline: 5 March 2012
“Activity Recommender” is a simple system that proposes activities to the user based on the
current weather conditions. Figure 1 shows a screenshot of the recommender system on a
day when there is a thunderstorm in Malta.
Figure 1 - A screenshot of the Activity Recommender system
The system provides a GUI with the following functionality:
1. An information bar at the top of the window gives information about the current
weather conditions.
2. A background image that changes based on the current weather conditions. E.g. if it
is currently raining, the background image shows a rain scene.
3. A list of activities which can be done in the current weather conditions.
You have been assigned the task of building a unit test suite for the system and have been
told you can make any changes required (to the system under test) in order to achieve your
goal. You are also asked to fix and document and bugs you may find. The assignment has
been split into a number of tasks in order to facilitate your approach.
Please maintain an assignment journal organized by task such that you explain any decisions
and answer any questions related to each task in the journal.
Deliverables:
1. The modified Activity Recommender system, complete with unit tests
2. The assignment journal explaining your decisions and answering any questions
posed in the task.
Figure 2 - A high-level schematic view of the system
Task 1
Read about and understand the following test patterns:
1. Dependency Injection
2. Dependency Lookup
3. Test Utility Method
The recommended resource for this task is Gerard Meszaros’ book entitled “xUnit Test
Patterns”. Please note that no entry for this task is required in the journal.
Task 2
Download the system source code from the course VLE, familiarise yourself with the
individual classes and source organization. Make sure you are able to run the software by
running the class com.activityrecommender.Main. Please note that you will need an
internet connection in order for the system to work properly.
Please note that no entry for this task is required in the journal.
Task 3 (20%)
Write a set of unit tests to verify the correct behaviour of the updateBackgroundImage()
method in the class MainFrame. Explain your choice of test cases in the assignment journal.
Task 4 (20%)
Familiarise yourself with the class WeatherFeedBBC.
The class implements the
WeatherFeed interface and is responsible for:
1. obtaining weather information from the URL
http://newsrss.bbc.co.uk/weather/forecast/3032/ObservationsRSS.xml
2. parsing the xml string returning the URL in order to obtain:
a. Current weather conditions (sunny, rainy, etc)
b. Temperature and wind information by parsing the textual description
provided by the feed
Figure 3 shows an example of a weather feed.
Produce
a
suite
of
unit
tests
which
test
the
parseDescription()
and
parseCondition() methods. Both methods use indirect inputs.
1. What are the particular indirect inputs referred to in this task?
2. Modify the SUT using the dependency injection pattern variation “Setter Injection” so
as to enable you to control the indirect inputs in question.
3. Do you agree with the selection of the Setter Injection pattern for this particular
situation? Why?
Figure 3 - A sample result from the BBC weather feed
Task 5 (20%)
The getApplicableActivities() method in the class MainFrame searches through
database activities so as to select activities that are feasible in current weather conditions.
1. Convert the SUT using the Dependency Lookup pattern such that them activities
database is created by a factory class at runtime.
2. Write a set of unit tests for the getApplicableActivities() method. Use the
Test Utility Method pattern in order to facilitate the creation of test databases. The
tests should ensure that the getApplicableActivities() method behaves as
follows:
a.
b.
c.
d.
Returns an empty Vector when no activities match
Returns a 1-item Vector when only one activity matches
Returns a sorted list whenever more than one activity is found
Returns a maximum of 10 activities, even if the database has more
3. Document changes you made to the SUT, why you made them and any issues you
may come across.
Task 6 (40%)
Install a code-coverage plug-in such as Emma or Cobertura. What is your code coverage so
far? Use the tool to identify untested code and add unit tests in an attempt to get as close as
possible to 100% line coverage. In particular, explain your selection of test cases for the
method canDoInWeather() in the class Activity (e.g. what equivalence classes did you
define? what test design technique did you use?).
explain why.
If you do not reach 100% coverage,
Download