Recitation 5 1

advertisement
Recitation 5
1
Outline
Goals of this recitation:
1. Practice using JUnit
2. Practice creating test cases
2
Start up
• Download Recitation 3 files:
– Animal.java
– Slug.java
– MutantSlug.java
• Start up Eclipse
• Create JUnit test cases for Slug.java and
MutantSlug.java
3
Create Test Methods for Slug.java
• The moveLeft() and moveRight() methods
for Slug have changed. Read the comments
in the code.
• Write test methods for both of them. What
should you test?
4
Test Methods for Slug
1. moveLeft()
•
Slug with > 0 hit points:
•
•
•
For 0 < x  100: Test that x coordinate is changed by -1
For x == 0: Test that x coordinate is unchanged
Slug with 0 hit points:
•
Test that x coordinate is unchanged
2. moveRight()
•
Slug with > 0 hit points:
•
•
•
For 0  x < 100: Test that x coordinate is changed by +1
For x == 100: Test that x coordinate is unchanged
Slug with 0 hit points:
•
Test that x coordinate is unchanged
5
Create Test Methods for
MutantSlug.java
• Write a test method for getHunger()
• The moveLeft() and moveRight() methods
for MutantSlug have also changed. Read
the method descriptions in the code.
• Write test methods for moveLeft() and
moveRight(). What should you test?
6
Test Methods for MutantSlug
1. getHunger()
•
Just test that the hunger value is returned
properly
7
Test Methods for MutantSlug
2.
moveLeft()
•
Slug with > 0 hit points:
•
•
•
Slug with 0 hit points:
•
•
3.
For 9 < x  100: Test that x coordinate is changed by -10
For 0  x  9: test that x coordinate doesn’t go beyond 0
Test that x coordinate is unchanged
For all slugs: make sure hunger is increased by +10
moveRight()
•
Slug with > 0 hit points:
•
•
•
Slug with 0 hit points:
•
•
For 0  x  90: Test that x coordinate is changed by +10
For 90 < x  100: test that x coordinate doesn’t go beyond 100
Test that x coordinate is unchanged
For all slugs: make sure hunger is increased by +10
8
Create TestMethods for
MutantSlug.java
• Write a test method for eat(Animal a). Read
the method description in the code.
• Think hard about how to test pre-conditions,
post-conditions, and boundary conditions.
9
Test methods for eat()
• If MutantSlug’s hunger is == 0
– Test that the eaten animal’s hit points are not set
to 0
• If MutantSlug’s hunger is > 0
– Test that the eaten animal’s hit points are set to
0
– Test that the Mutant Slug’s hunger is reduced
by the amount of the animal’s hit points
– Test that the Mutant Slug’s hunger is not
reduced past 0
10
Once you’re done
• Get a start on Assignment #3. Feel free to
talk to the TA for help.
11
Download