Professional Documents
Culture Documents
SD Lecture p2.2
SD Lecture p2.2
2
IN THIS PRESENTATION:
3
PROGRAM DESIGN
DISCUSSED IN THE LAST LECTURE
Hotel Guest
room1 Room guest -name : String
-name : String
-number : int
-address : String 0..1 +getName() : String
room2 +getNumber() : int
+getName() : String room +getRoom() : Room
+getGuest() : Guest +checkin(Room room) : void
+getFreeRoom() : Room 0..1
+checkout() : boolean
4
GUEST CLASS
FOR THIS EXAMPLE
5
OBJECT INTERACTIONS Convention: all instance
variables are private!
r
checkin(r)
6
CLASS SPECIFICATIONS
PRECONDITIONS, POSTCONDITIONS AND CLASS INVARIANTS
For each class and each method, the program designer must
specify the conditions for objects of this class to work properly!
• Preconditions: Which conditions should hold beforehand for a
method to work correctly once is called?
• Postconditions: What conditions are satisfied once the method
has worked correctly?
• (Class) Invariants: What conditions must always hold in an
object of a class?
7
GUEST CLASS SPECIFICATION
(room.getGuest() == this)
• Class Invariant: If room attribute is not null
then the guest related to the room is this guest : Room
number = 101
(room != null ==> room.getGuest() == this) guest =
8
COUNTER CLASS EXAMPLE
9
PRECONDITION
10
PRECONDITION
RESPONSIBILITY OF THE CALLER
• Class invariant
0 <= code && code <= 999
• enterDigit precondition
@requires 0 <= digit && digit <= 9
• If we define like as precondition,
Caller must ensure this!
• Lock implementation does not
need to check it!
12
POSTCONDITION
RESPONSIBILITY OF THE CALLED OBJECT (IMPLEMENTER)
Skater laps Counter
-value
+passFinish() +getValue()
+distance() +setValue()
+increment()
14
CLASS INVARIANTS
15
BOOLEAN CONDITIONS
17
ANSWER 1: TRUST CLIENT
Consequences Dilbert
• No special precautions necessary!
18
ANSWER 2: GENERATE ERROR MESSAGE
20
ANSWER 4: CHECK OR VERIFY
Runtime Checking
• Automatically insert precondition and postcondition checks
during execution
Static Checking
• Construct formal proof that Advanced, requires
• Preconditions hold at every method call appropriate tooling
• Postconditions hold at every method exit
Not considered
• Invariants are always maintained
further in this module
21
PROGRAMMING BY CONTRACT
ESSENCE OF OBJECT-ORIENTATION
Specification
implementation
• Methods have preconditions that (how)
caller (client) must respect
• Then methods should guarantee that :Obj
postconditions are satisfied
Implementation
• Class invariants and method specification
implementation guarantee that (what)
postconditions are satisfied
22
PROGRAMMING BY CONTRACT AND APIS
s != null &&
proper format
23
TAKE HOME MESSAGES
24
IN THIS PRESENTATION:
25
TESTING PURPOSES
26
KINDS OF TESTS: THE V-MODEL
DESIGN AND TESTING HAND-IN-HAND
Realm of this
lecture
27
KINDS OF TESTS
28
TEST PLAN
Goal
• Systematically test whether the system or unit
behaves as expected if used in specified way
Contents
Checklist
• List of execution scenarios → test cases
• How the tests should be carried out
• What is the expected result
29
TEST PLAN: SYSTEM TESTING
30
TEST PLAN: UNIT TESTING
Lock
-int code
-boolean open
+boolean isOpen()
+void close()
+void enterDigits(int code)
32
TEST CASES
FOUR CASES
1. Attempt to open (calling enterDigits) in open state
• Postcondition: isOpen() returns true
2. Attempt to open (calling enterDigits) in closed state
• Postcondition: isOpen() returns true if and only if code is correct
3. Attempt to close (calling close) in open state
• Postcondition: isOpen() returns false Lock
-int code
4. Attempt to close in closed state -boolean open
• Postcondition: isOpen() returns false +boolean isOpen()
+void close()
+void enterDigits(int code)
33
TEST CASE 2: OPEN IN CLOSED STATE
34
TEST CASE 2: OPEN IN CLOSED STATE
36
TESTING FOR UNEXPECTED SITUATIONS
37
TESTING FOR ROBUSTNESS
38
FROM TESTING THEORY TO PRACTICE
JUNIT
39
USING JUNIT 5
40
@TEST METHOD
41
MODULOCOUNTER CLASS EXAMPLE
42
MODULOCOUNTERTEST JUNIT CLASS
counter;
Setup method
();
(@BeforeEach annotation)
Test case:
increase zero times
(@Test annotation)
Test case:
increase counter nine times,
reinitialised in setup()
43
MODULOCOUNTER WITH A SERIOUS BUG
We need to improve
our tests!!!
44
ADDITIONAL TEST CASE
Test cases:
1. increase it nine times
2. increase it eleven times,
which should trigger the
“resetting to zero” action
45
RELATION WITH PROGRAMMING BY
CONTRACT
• Postconditions of method increase()
@ensures old.getValue() < 9 ==> getValue() == old.getValue() + 1
@ensures old.getValue() == 9 ==> getValue() == 0
• Can be made to correspond to the test cases
• Methods testNine() and testEleven() or testTen(),
respectively
46
JUNIT EXAMPLE
MODULOCOUNTER WITH CORRECT METHODS
47
JUNIT 5 ASSERTIONS
48
TESTING AND DEVELOPMENT PROCESS
Approach recommended
in this module!
• Iterative process
• Develop tests before you start to implement (test-driven
development) based on the specifications
• Perform unit tests immediately after functionality is complete
• Extending the implementation requires extending test plan
• Test results lead to further coding
• Test coverage results lead to extended test plan
49
TESTING AND DEVELOPMENT PROCESS
• Regression
• Bugs may be introduced in previously correct code
• Rerun your tests on a regular basis (regression testing)
• You will only ever do this if it is automated
→ JUnit is your friend!
• Philosophies
• “Code a little, test a little”
• “Test a little, code a little” (test-driven development, test-first)
• Watch out: Testing can take up to 50% of development time
50
TEST QUALITY
QUALITY OF YOUR TESTS
51
TEST QUALITY
TEST COVERAGE
http://www.eclemma.org 53
CODE COVERAGE
DIFFICULTIES
54
CODE COVERAGE
SOME HINTS
55
TAKE HOME MESSAGES