3/10/2021
T1 – Meet & Greet
While all assessments in this unit are completed individually, the major assignments will be ‘operations on existing code or platforms’, which have been designed and written by someone else – this is most often the case in the software development in industry. Because of this, the tutorial tasks will often include activities between different codebases, to allow you to practice this sort of collective coding work.
Take this time to meet your tutor, and to meet the classmates you will be working with.
https://canvas.sydney.edu.au/courses/31635/pages/t1-meet-and-greet 1/2
3/10/2021 T1 – Introduction to Unit Testing: SOFT3202 COMP9202 Software Construction and Design 2
T1 – Introduction to Unit Testing
The lecture introduced several types and levels of testing – throughout the early portion of the semester we will be applying a few of these to live code. The first type is ‘unit testing’. This is the most widely known test style, and due to its requirements is almost always done programmatically.
There are several frameworks and IDE supports for unit testing in various languages – however the vast majority of them follow the xUnit style that grew out of Smalltalk’s SUnit –Java has JUnit, C++ has CPPUnit, Python has PyUnit, .NET has XUnit, and many more. By learning the methods and conventions of one, you can apply this knowledge in many different areas regardless of the base language.
For this reason, our first programming task will be done in pseudocode, to learn the basic techniques for unit testing.
Specification (https://canvas.sydney.edu.au/courses/31635/pages/t1-shoppingbasket)
The page linked above contains a specification for a ShoppingBasket class and its methods. We are going to write unit tests that would identify if an implementation of that class contained bugs. First, some notes on unit testing in general:
Unit testing is based on testing a single ‘unit’ of code – exactly how big a ‘unit’ is something not everyone agrees on. The lowest form of unit testing is also known as ‘microtesting’ and applies to a single class and its methods, with each unit test aimed at specific methods. However, collections of classes into ‘modules’ or ‘packages’ is also considered unit testing by some.
We will be doing microtesting to verify this class meets the given specification.
The types we are using include String, Boolean, Number (may be an integer or a fraction), and Nothing (like Java’s void or null, may be returned from any method or be held in any variable regardless of type)
The pseudocode testing language we will be using is made up of the following keywords:
TEST: This is the first line of the test, and can be considered the ‘method signature’. Every TEST is run exactly once when running your entire ‘test suite’.
ASSERT: This statement ‘asserts’ or ‘claims’ that what follows is true. If it does not, then the test fails (tests can also fail if uncaught exceptions are thrown). Examples:
ASSERT myVal is equal to 7 (eg test would fail if myVal == 2 at that line) ASSERT someOtherVal < myVal
ASSERT someFunction() does not return null
https://canvas.sydney.edu.au/courses/31635/pages/t1-introduction-to-unit-testing 1/2
3/10/2021
T1 - Introduction to Unit Testing: SOFT3202 COMP9202 Software Construction and Design 2
FAIL: This statement causes immediate test failure (same as ASSERT 1 equals 0)
Beyond these test keywords you should feel free to write clear English commands to describe what you are doing instead of any particular language.
Try to write a suite of TESTs that completely captures every possible bug in the ShoppingBasket class. Remember you don’t need to catch syntax errors like returning a String instead of a Number, because code not compiling causes automatic test failures. You also don’t need to check for unwanted or unexpected exceptions, because if they get thrown and you don’t catch them the test fails.
Copyright © The University of Sydney. Unless otherwise indicated, 3rd party material has been reproduced and communicated to you by or on behalf of the University of Sydney in accordance with section 113P of the Copyright Act 1968 (Act). The material in this communication may be subject to copyright under the Act. Any further reproduction or communication of this material by you may be the subject of copyright protection under the Act. Do not remove this notice.
Live streamed classes in this unit may be recorded to enable students to review the content. If you have concerns about this, please visit our student guide (https://canvas.sydney.edu.au/courses/4901/pages/zoom) and contact the unit coordinator.
https://canvas.sydney.edu.au/courses/31635/pages/t1-introduction-to-unit-testing 2/2
3/10/2021 T1 - ShoppingBasket: SOFT3202 COMP9202 Software Construction and Design 2
Specification of previous page (https://canvas.sydney.edu.au/courses/31635/pages/t1-shoppingbasket)
T1 - ShoppingBasket
Specification for class: ShoppingBasket Constructor:
ShoppingBasket()
Default constructor, throws no exceptions. Methods:
Nothing addItem(String item, Number count)
addItem takes a String parameter that must be one of ‘apple’, ‘orange’, ‘pear’, or ‘banana’. If a different item is given, or an empty/null item, then the method throws an ArgumentException. addItem takes a Number parameter that must be an integer which is 1 or more. If the Number is not an integer or is less than 1 then the method throws an NumberException. addItem adds the ‘count’ of that ‘item’ to the basket.
Boolean removeItem(String item, Number count)
removeItem takes a String parameter, which must not be Nothing but may be any String. If it is Nothing then the method throws an ArgumentException. removeItem takes a Number parameter that must be an integer which is 1 or more. If the Number is not an integer or is less than 1 then the method throws an NumberException. If the item does not exist, or the count is more than the current count of the matching item, nothing is changed in the basket and removeItem returns false. Otherwise, removeItem removes the ‘count’ of that ‘item’ from the basket, and returns true. If the count would be reduced to 0 the item record is removed entirely.
List Of String and Number Pairs getItems()
getItems returns a list of all items currently held in this basket along with their counts. getItems never returns Nothing, but may return an empty list.
Number getValue()
getValue returns the current sale value of the combined cart contents. Apples are worth $2.50, Oranges $1.25, Pears $3.00, and Bananas $4.95. If the cart is empty getValue returns Nothing, not zero.
https://canvas.sydney.edu.au/courses/31635/pages/t1-shoppingbasket 1/2
3/10/2021 T1 - ShoppingBasket: SOFT3202 COMP9202 Software Construction and Design 2
Nothing clear()
clear empties this basket, removing all items.
Copyright © The University of Sydney. Unless otherwise indicated, 3rd party material has been reproduced and communicated to you by or on behalf of the University of Sydney in accordance with section 113P of the Copyright Act 1968 (Act). The material in this communication may be subject to copyright under the Act. Any further reproduction or communication of this material by you may be the subject of copyright protection under the Act. Do not remove this notice.
Live streamed classes in this unit may be recorded to enable students to review the content. If you have concerns about this, please visit our student guide (https://canvas.sydney.edu.au/courses/4901/pages/zoom) and contact the unit coordinator.
https://canvas.sydney.edu.au/courses/31635/pages/t1-shoppingbasket 2/2
3/10/2021 T1 - JUnit Testing: SOFT3202 COMP9202 Software Construction and Design 2
T1 - JUnit Testing
The particular test framework we will be using is JUnit. This is an xUnit framework for Java that covers (among many other things) the TEST, ASSERT, and FAIL style commands we have been pseudocoding with.
For your own sanity later it would be best to follow the standard Maven/Gradle source folder requirements: ‘src/main/java/
The first thing we want to do is write and run our first real unit test. Consider the following class (src/main/MyClass.java):
Close to the simplest method we can do some basic testing on. Consider the specification for this method as “always returns 4, never throws an exception”. We only need 1 test case for this class, and it looks like this (src/test/MyClassTest.java):
public class MyClass {
public static int getValue() {
return 4; }
}
import org.junit.Test;
import static org.junit.Assert.*;
public class MyClassTest {
@Test
public void getValueTest() {
assertEquals(“getValue returned the wrong value”, 4, MyClass.getValue());
}
}
That’s it. No main method, no need to ‘reset’ anything or catch the output of the assertion – JUnit handles this for us. Using the command line, we have to manually compile our classes, then assuming the machine you are working on has Java installed, with the junit4 and hamcrest jars downloaded:
https://canvas.sydney.edu.au/courses/31635/pages/t1-junit-testing 1/3
3/10/2021
T1 – JUnit Testing: SOFT3202 COMP9202 Software Construction and Design 2
java -cp “.;./src/main/java;./src/test/java;/path/to/junit4/junit.jar;/path/to/hamcrest/hamcrest.jar” or
g.junit.runner.JUnitCore MyClassTest
(use : in place of ; for Mac and Linux and swap / to \ if necessary)
The number of dependencies we will be working with is going to grow over the semester, and the number of times you are going to run tests will be large – you should become familiar with the Gradle build tool: https://gradle.org/ (https://gradle.org/) , which will help you manage these dependencies as well as automatically run your unit tests
Here is a build.gradle file which will work (https://canvas.sydney.edu.au/courses/31635/files/15297405/download?wrap=1) .
Once you have:
installed Gradle if it is not already present
set your environment variables correctly
with that build.gradle in a folder
which also contains src/main/java and src/test/java structure with your MyClass and MyClassTest files
You should be able to simply type ‘gradle test’ into the console to compile your .java files, and run the tests. Use ‘gradle test -i’ for more verbose output.
Gradle makes some things very easy. For example, look at the build/reports/tests/test/index.html file it has created for us.
Change MyClass.getValue to return 100, run the test again, then check the report.
JUnit can be run from the command line, or it can be run in any number of IDEs: Eclipse, IntelliJ, NetBeans, etc. As your assignments will all be run via script (effectively the command line) there is no need to control what IDE you wish to use – if you wish to use any at all. Please see the following documentation on the specific processes for each IDE running JUnit:
https://help.eclipse.org/2018-12/index.jsp? topic=%2Forg.eclipse.jdt.doc.user%2FgettingStarted%2Fqs-junit.htm
https://canvas.sydney.edu.au/courses/31635/pages/t1-junit-testing 2/3
3/10/2021 T1 – JUnit Testing: SOFT3202 COMP9202 Software Construction and Design 2
(https://help.eclipse.org/2018-12/index.jsp? topic=%2Forg.eclipse.jdt.doc.user%2FgettingStarted%2Fqs-junit.htm)
https://www.jetbrains.com/help/idea/configuring-testing-libraries.html (https://www.jetbrains.com/help/idea/configuring-testing-libraries.html)
https://netbeans.org/kb/docs/java/junit-intro.html (https://netbeans.org/kb/docs/java/junit- intro.html)
In all cases, however, you are required to use Gradle as the test runner and dependency manager, as this structure is how the starting code will be provided to you and how it will be assessed. Your use of an IDE is entirely up to you – you must support this yourself using the available documentation.
The required Java version for the unit is 11, and JUnit version is 4 (vanilla, do not use Arquillian or any other package/library as it will not be present in the assessment marking environment).
You should try to implement their ShoppingBasket pseudocode suite as a JUnit suite – you can make reasonable assumptions regarding Number and its conversion to int/double etc. Use this as a reference if needed: http://junit.sourceforge.net/javadoc/org/junit/Assert.html (http://junit.sourceforge.net/javadoc/org/junit/Assert.html)
Copyright © The University of Sydney. Unless otherwise indicated, 3rd party material has been reproduced and communicated to you by or on behalf of the University of Sydney in accordance with section 113P of the Copyright Act 1968 (Act). The material in this communication may be subject to copyright under the Act. Any further reproduction or communication of this material by you may be the subject of copyright protection under the Act. Do not remove this notice.
Live streamed classes in this unit may be recorded to enable students to review the content. If you have concerns about this, please visit our student guide (https://canvas.sydney.edu.au/courses/4901/pages/zoom) and contact the unit coordinator.
https://canvas.sydney.edu.au/courses/31635/pages/t1-junit-testing
3/3
3/10/2021 T1 – Fortnightly Tasks – Introduction: SOFT3202 COMP9202 Software Construction and Design 2
T1 – Fortnightly Tasks – Introduction
In SOFT3202 there is ongoing assessment in the form of 5 more or less fortnightly tasks. These tasks are designed as formative assessment – that is, they are intended to be something that you do while you are learning something instead of summative assessment which is something you do to prove you have already achieved some learning outcome.
Formative assessment is not designed to evaluate you – it is designed to help you learn. For this reason, you should NOT make the mistake of assuming that because a single fortnightly task is worth 2% that it is worth 2% of your time – this will lead to unnecessarily poor outcomes in the summative assessments that are based on what you are expected to learn while completing these tasks.
The mapping is as follows:
Task 1 involves the skills you will need for A1(assignment 1) Task 2 involves the skills you will need for A2(assignment 2) Tasks 3-5 involve the skills you will need for the exam
These tasks will be released 2 tutorials prior to their due date – Task 1 will release before the first tutorials, and will be due on Friday 19MAR. Task 1 is shifted a little bit compared to the others which will usually be Sunday to Sunday, so we can get feedback to you in time for you to use it in A1 (due in Week 4, 28MAR). Because the more important measure than ‘days’ is ‘number of tutorials’ COMP students will have this due on Sunday 21MAR (since their tutorials are on Fridays).
While late submissions of the fortnightly tasks are accepted with the usual 5% per day up to 10 days policy, you are not guaranteed to receive any feedback before the relevant summative assessment if you submit late, so this is not recommended.
The expectation is that you will be attempting these tasks well before the due date, and using the 2 tutorials you have to discuss them with your tutor. While all code must as always be your own work for academic honesty reasons, your tutor will be free to discuss how to complete them with you and your classmates. This is not the case for the major assessments, so make sure you use the tasks properly to learn the skills you will need.
Copyright © The University of Sydney. Unless otherwise indicated, 3rd party material has been reproduced and communicated to you by or on behalf of the University of Sydney in accordance with section 113P of the Copyright Act 1968 (Act). The material in this communication may be subject to copyright under the Act. Any further reproduction or communication of this material by you may be the subject of copyright protection under the Act. Do not remove this notice.
Live streamed classes in this unit may be recorded to enable students to review the content. If you have concerns about this, please visit our student guide (https://canvas.sydney.edu.au/courses/4901/pages/zoom) and contact the unit coordinator.
https://canvas.sydney.edu.au/courses/31635/pages/t1-fortnightly-tasks-introduction 1/2
3/10/2021 T1 – Final Notes: SOFT3202 COMP9202 Software Construction and Design 2
T1 – Final Notes
Before the end of the tutorial, make sure you take a look at the first assessable Task:
Task 1 (https://canvas.sydney.edu.au/courses/31635/assignments/284429)
This tutorial has been primarily about getting you to a point where you can run your own unit tests. You should practice a lot with the different sorts of assertions (http://junit.sourceforge.net/javadoc/org/junit/Assert.html) available from junit.sourceforge.net/javadoc/org/junit/Assert.html (http://junit.sourceforge.net/javadoc/org/junit/Assert.html) , and ensure you are easily able to set up and run Gradle projects through to running tests either on the command line or in your IDE of choice. If you did not have time to implement and run your ShoppingBasket suite you should complete this. Beginning next week our focus will shift to what to write in our tests and why, and it will be assumed you are able to run them. If you are stuck at any point, ask for help on Ed rather than wait for next week’s tutorial, so you do not fall behind.
Copyright © The University of Sydney. Unless otherwise indicated, 3rd party material has been reproduced and communicated to you by or on behalf of the University of Sydney in accordance with section 113P of the Copyright Act 1968 (Act). The material in this communication may be subject to copyright under the Act. Any further reproduction or communication of this material by you may be the subject of copyright protection under the Act. Do not remove this notice.
Live streamed classes in this unit may be recorded to enable students to review the content. If you have concerns about this, please visit our student guide (https://canvas.sydney.edu.au/courses/4901/pages/zoom) and contact the unit coordinator.
https://canvas.sydney.edu.au/courses/31635/pages/t1-final-notes 1/2