CMPE212 – Stuff… Assn 2 due this Friday. Winter 2018

Slides:



Advertisements
Similar presentations
T ESTING WITH J UNIT IN E CLIPSE Farzana Rahman. I NTRODUCTION The class that you will want to test is created first so that Eclipse will be able to find.
Advertisements

Today Assignment 2 is posted. Due the first Friday after Reading Week. (“Virtual” lecture looked at the use of try/catch and try with resources blocks.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Intoduction to Unit Testing Using JUnit to structure Unit Testing SE-2030 Dr. Rob Hasker 1 Based on material by Dr. Mark L. Hornick.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Justin Bare and Deric Pang with material from Erin Peach, Nick Carney, Vinod Rathnam, Alex Mariakakis, Krysta Yousoufian, Mike Ernst, Kellen Donohue Section.
Winter 2006CISC121 - Prof. McLeod1 Stuff We had better discuss a midterm date… –27 Feb. to 3 March or –6 to 10 March.
JUnit Testing Why we do this and how we can get better.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Today protected access modifier Using the debugger in Eclipse JUnit testing TDD Winter 2016CMPE212 - Prof. McLeod1.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Today… StringTokenizer class. Method Overloading. Catching Exceptions (and what they are!). Start Pointers and Aliasing. Winter 2016CMPE212 - Prof. McLeod1.
CSE 143 Lecture 14: testing.
Notices Assn 2 is due Friday, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including Thursday’s lecture: Big Topics.
Winter 2006CISC121 - Prof. McLeod1 Stuff Midterm exam in JEF234 on March 9th from 7- 9pm.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Appendix A Barb Ericson Georgia Institute of Technology May 2006
Dept of Computer Science University of Maryland College Park
CSE 374 Programming Concepts & Tools
Testing and Debugging.
CMPE212 – Stuff… Assn 2 due next Friday. Winter 2018
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
CISC/CMPE320 - Prof. McLeod
Chapter 14: Exception Handling
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Winter 2018 CMPE212 11/12/2018 CMPE212 – Stuff…
Some Tips for Using Eclipse
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Assignment 3 due this Wednesday at 7pm.
CISC101 Reminders Assn 3 due Friday, this week. Quiz 3 next week.
Credit to Eclipse Documentation
Fall 2018 CISC124 12/1/2018 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until.
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
CISC124 Assignment 3 due tomorrow at 7pm.
Introduction to JUnit IT323 – Software Engineering II
CISC124 Assignment 4 on Inheritance due next Friday.
Fall 2018 CISC124 2/1/2019 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC124 Labs start this week in JEFF 155. Fall 2018
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CMPE212 – Reminders Assignment 2 due this Friday.
CMPE212 – Reminders Assignment 2 sample solution is posted.
CMPE212 – Reminders Assignment 3 due next Friday.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Winter 2019 CMPE212 4/17/2019 CMPE212 – Reminders
CSE 143 Lecture 5 More ArrayIntList:
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
CMPE212 – Reminders Assignment 2 due this Friday.
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Winter 2019 CMPE212 5/3/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 2 due in a week.
CMPE212 – Reminders Assignment 2 due today, 7pm.
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
CISC101 Reminders Assignment 3 due today.
CMPE212 – Reminders Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Workshop for Programming And Systems Management Teachers
Presentation transcript:

CMPE212 – Stuff… Assn 2 due this Friday. Winter 2018 Winter 2018 CMPE212 11/29/2018 CMPE212 – Stuff… Assn 2 due this Friday. Winter 2018 CMPE212 - Prof. McLeod Prof. Alan McLeod

Today WaiterData – another Encapsulation Example. Debugging. JUnit Testing. Winter 2018 CMPE212 - Prof. McLeod

Another Encapsulated Example The problem on the next slide – “WaiterData” - was a quiz from several years ago (written on paper!). Let’s read over the problem and then look at the solution. This solution has some extra stuff!: Javadoc comments The exception class toString, clone, equals Winter 2018 CMPE212 - Prof. McLeod

Write the code for a mutable encapsulated class called WaiterData Write the code for a mutable encapsulated class called WaiterData. This class will use the IllegalWaiter exception, but you do not have to write this exception class – you can just assume that it already exists. (Let’s write it anyways!) WaiterData is designed to hold the daily sales for a month for a particular waiter. The class will hold two attributes, a String for the name of the waiter and an array of type double for the actual daily sales values. The String must have a length of 3 or more to be legal. The array must have a size between 5 and 31 inclusive. Each individual value in the array must lie between 0 and 500 inclusive. You may assume that neither argument will ever be null. Write a two parameter constructor for WaiterData. WaiterData must have a mutator for each attribute and must throw the IllegalWaiter exception if an attempt is made to create an illegal WaiterData object or if an attempt is made to set an attribute to an illegal value through either mutator. Write accessors for both attributes. Finally, write a compareTo() method for your WaiterData class. Comparison is based on the total amount of the sales array. If the supplied WaiterData object has had a higher sales total then return -1. Return 1 if the supplied object has lower total sales and zero of both totals are equal to within a dollar. Do not write any other public methods – you do not need a toString(), clone() or an equals() method. (Let’s write them anyways!) Winter 2018 CMPE212 - Prof. McLeod

Debugging Sometimes a mental “walk-through” of your code is not enough to figure out an error. With the debugger you can run to a breakpoint, stop your program and then execute one line at a time while watching the call stack, variables and custom expressions. Let’s fix UseDebugger.java Winter 2018 CMPE212 - Prof. McLeod

Debugging, Summary See “Java development users guide” > “Concepts” > “Debugger” and “Java development user guide” > “Getting Started” > “Basic tutorial” > “Debugging your programs” in the Eclipse help system. Place one or more breakpoints in your edit window. Run in debug mode. Use the “step” choices. View the method call stack, variables, and use expressions if needed. Winter 2018 CMPE212 - Prof. McLeod

Testing Until now “we” have used another class with a main method to run tests on the class we have “harnessed” for testing. See TestHalloween4.java and TestHalloween5.java for example. JUnit is a framework designed for this kind of work and it is very easy to use in Eclipse. Winter 2018 CMPE212 - Prof. McLeod

JUnit Testing Best reference is junit.org See the links in the centre column and the API docs. For use in Eclipse see: “Java development user guide” > “Getting Started” > “Basic tutorial” > “Writing and running JUnit tests”. Winter 2018 CMPE212 - Prof. McLeod

JUnit Testing in Eclipse In the project containing the class(es) you wish to test, add a Testing class by choosing “New JUnit Test Case”: Name the class and choose the classes to be tested. Choose extra stubs, if needed. Allow the addition of the JUnit library to the Build Path, if prompted. Winter 2018 CMPE212 - Prof. McLeod

JUnit Testing in Eclipse, Cont. Next, let the wizard create test method stubs in your testing class for all methods you wish to test: Winter 2018 CMPE212 - Prof. McLeod

JUnit Testing in Eclipse, Cont. Fill in the method stubs with tests. You can have multiple tests in a method or multiple methods or both, as required. You could end up with hundreds of tests, just for one object! See TestQuiz1MethodsWizard.java and TestQuiz1Methods.java. Using assertArrayEquals. Winter 2018 CMPE212 - Prof. McLeod

Assertions Use (expected, actual) or (String, expected, actual): assertArrayEquals() assertEquals() assertFalse() assertNotEquals() assertNotNull() assertNotSame() // for objects assertSame() // for objects assertNull() assertTrue() Winter 2018 CMPE212 - Prof. McLeod

Another Example See TestWaiterData.java for more assertions in use as well as testing to see if an exception is thrown. Winter 2018 CMPE212 - Prof. McLeod

assertThat() Takes a Matcher<T> object for its second (or third) parameter. Possible object types: AllOf, AnyOf, BaseMatcher, CombinableMatcher, CustomMatcher, CustomTypeSafeMatcher, DescribedAs, DiagnosingMatcher, Every, FeatureMatcher, Is, IsAnything, IsCollectionContaining, IsEqual, IsInstanceOf, IsNot, IsNull, IsSame, StringContains, StringEndsWith, StringStartsWith, SubstringMatcher, TypeSafeDiagnosingMatcher, TypeSafeMatcher Winter 2018 CMPE212 - Prof. McLeod

assertThat(), Cont. For example (from JUnit.org): And, there is a: assertThat("albumen", both(containstring("a")).and(containsString("b")) And, there is a: import static org.hamcrest.CoreMatchers.*; In the JUnit API docs look up this class for a list of these useful methods. Winter 2018 CMPE212 - Prof. McLeod

assertThat, Cont. Note that the order is different - the actual comes first followed by the expected. You can still have a String message as the first parameter. You can build more sophisticated assertions with this method. Winter 2018 CMPE212 - Prof. McLeod

Another Example See TestHalloween6.java Has more assertion methods including assertThat. And it tests to see if exceptions are thrown by using: @Test (expected=IllegalHalloweenException.class) Winter 2018 CMPE212 - Prof. McLeod

No try/catch Don’t bother with try/catch blocks. If you are testing code that has a throws decoration, then just have your unit test method throw Exception to satisfy the compiler. If an exception is thrown unexpectedly (a run-time error!) you will get a different kind of failure notice in the report. Note that the testing does not stop! Winter 2018 CMPE212 - Prof. McLeod

Test Suites You can combine separate JUnit testing classes into a single suite and run them all at once. In Eclipse, go “New”, “Other”, “JUnit”, “JUnit Test Suite”. Specify the src folder location for the existing testing *.java files and choose the ones you want to include in the suite. See AllTests.java. Winter 2018 CMPE212 - Prof. McLeod

Advantages Tests are all separate from the code being harnessed. Very easy to add tests. Running tests is very simple and fast. Results are nicely summarized and you can zoom in on failed tests easily and get some decent diagnostics. Tests can be run on remote classes on a server, for example. Winter 2018 CMPE212 - Prof. McLeod

Designing and Running Unit Tests Start by testing the most concrete independent methods first. Then test the methods that depend on these ones. Then test classes. Then test systems. Winter 2018 CMPE212 - Prof. McLeod

Designing and Running Unit Tests, Cont. You will need to write stub and driver code as required in the testing classes. Stub code substitutes something “fake” to a method that depends upon it. Driver code simulates code that uses the method being tested. Keep the stub and driver code simple so that errors can only come from the harnessed code, not the testing code. Winter 2018 CMPE212 - Prof. McLeod

Designing and Running Unit Tests, Cont. Start with tests that easily fall into the normal, expected range of inputs. Few or many? Use test generator tools? Create more tests for inputs that you suspect are related to each other. Choose tests for inputs that are just barely legal. Choose tests for inputs that are just barely illegal. Choose tests for inputs that are way out there! Make sure you exercise all the code being harnessed. Hundreds or thousands of tests! Winter 2018 CMPE212 - Prof. McLeod

Designing and Running Unit Tests, Cont. Keep all tests unless you have deleted some of the methods/classes that you were testing. You can even leave these in with a “fail” call. Fixes applied to some methods can invoke a ripple effect, causing previously passed tests to fail. In a team effort always run (and pass) all of your unit tests before committing your code to the repository. Winter 2018 CMPE212 - Prof. McLeod

Unit Testing You can never test all possible input conditions. Unit testing doesn’t prove that you code is without errors, but you can end up feeling pretty good about your code if it passes all tests. Winter 2018 CMPE212 - Prof. McLeod