Today protected access modifier Using the debugger in Eclipse JUnit testing TDD Winter 2016CMPE212 - Prof. McLeod1.

Slides:



Advertisements
Similar presentations
Annoucements  Next labs 9 and 10 are paired for everyone. So don’t miss the lab.  There is a review session for the quiz on Monday, November 4, at 8:00.
Advertisements

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.
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.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
MT311 Tutorial Li Tak Sing( 李德成 ). Uploading your work You need to upload your work for tutorials and assignments at the following site:
Cell phones off Name signs out – congrats Sean! CSE 116 Introduction to Computer Science for Majors II1.
Testing and Debugging CS221 – 2/13/09. Airline Program.
Testing and Debugging pt.2 Intro to Complexity CS221 – 2/18/09.
Fall 2007CS 2251 Programming Tools Eclipse JUnit Testing make and ant.
Applied Software Project Management Andrew Stellman & Jennifer Greene Applied Software Project Management Applied Software.
1 CSC/ECE 517 Fall 2010 Lec. 2 Overview of Eclipse Lectures 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Debugging 5.Testing.
DEBUGGERS For CS302 Data Structures Course Slides prepared by TALHA OZ (most of the text is from
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
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.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Testing and Debugging Session 9 LBSC 790 / INFM 718B Building the Human-Computer Interface.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
What is Testing? Testing is the process of finding errors in the system implementation. –The intent of testing is to find problems with the system.
Week 14 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
JUnit Don Braffitt Updated: 10-Jun-2011.
JUnit A framework which provides hooks for easy testing of your Java code, as it's built Note: The examples from these slides can be found in ~kschmidt/public_html/CS265/Labs/Java/Junit.
Error messages 25-Apr-17.
Scalatest. 2 Test-Driven Development (TDD) TDD is a technique in which you write the tests before you write the code you want to test This seems backward,
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Unit Testing with FlexUnit
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.
Lec 15 Testing GUIs 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 15 GUI Testing 10/21/
JUnit Testing Why we do this and how we can get better.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
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… Functions, Cont.: –Designing functions. –Functional Decomposition –Importing our own module –A demo: Functional solution to assignment 2. Winter.
Powerpoint Templates Page 1 Powerpoint Templates Unit Testing Ari Seppi
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
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
Test Driven Development
CMPE212 – Stuff… Assn 2 due next Friday. Winter 2018
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
CISC124 Assignment 3 due this Wednesday at 7pm.
CMPE212 – Stuff… Assn 2 due this Friday. Winter 2018
Test-driven development (TDD)
Testing and Test-Driven Development CSC 4700 Software Engineering
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.
CISC124 Assignment 3 due tomorrow at 7pm.
Introduction to JUnit IT323 – Software Engineering II
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
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
CMPE212 – Reminders Assignment 2 due this Friday.
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 2 due today, 7pm.
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
Junit Tests.
Workshop for Programming And Systems Management Teachers
Framework Anil
Presentation transcript:

Today protected access modifier Using the debugger in Eclipse JUnit testing TDD Winter 2016CMPE212 - Prof. McLeod1

Aside – Protected Access So far we have used public and private access modifiers. protected is what you get if you don’t specify an access modifier. This means “ public inside the package, private outside the package”. Used in the API. Use sparingly! Winter 2016CMPE212 - Prof. McLeod2

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 2016CMPE212 - Prof. McLeod3

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. 1.Place one or more breakpoints in your edit window. 2.Run in debug mode. 3.Use the “step” choices. 4.View the method call stack, variables, and use expressions if needed. Winter 2016CMPE212 - Prof. McLeod4

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 2016CMPE212 - Prof. McLeod5

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 2016CMPE212 - Prof. McLeod6

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 2016CMPE212 - Prof. McLeod7

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 2016CMPE212 - Prof. McLeod8

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 TestQuiz1Methods.java Winter 2016CMPE212 - Prof. McLeod9

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

assertThat() Takes a Matcher 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 2016CMPE212 - Prof. McLeod11

assertThat(), Cont. For example (from JUnit.org): 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 2016CMPE212 - Prof. McLeod12

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 2016CMPE212 - Prof. McLeod13

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

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 2016CMPE212 - Prof. McLeod15

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 2016CMPE212 - Prof. McLeod16

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 2016CMPE212 - Prof. McLeod17

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 2016CMPE212 - Prof. McLeod18

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 2016CMPE212 - Prof. McLeod19

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 2016CMPE212 - Prof. McLeod20

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 2016CMPE212 - Prof. McLeod21

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 2016CMPE212 - Prof. McLeod22

Winter 2016CMPE212 - Prof. McLeod23 Test Driven Development or “TDD” How much code can you “implement” without testing it? I get nervous writing 20 or 30 lines without testing. And whenever I complete a non-trivial method I test it. You should not separate the process of implementation and testing. Not surprising – this is a basic tenant of Agile Development.

TDD, Cont. Is all about writing tests while you are writing, or decomposing your code. In fact the testing drives and leads the coding. We need a systematic technique to create tests that ensures coverage: Winter 2016CMPE212 - Prof. McLeod24

Winter 2016CMPE212 - Prof. McLeod25 TDD, Cont. The three Laws of TDD: 1.You may not write production code until you have written a failing unit test. 2.You may not write more of a unit test than what is sufficient to cause a failure, and not compiling is a failure. 3.You may not write more production code than what is sufficient to pass the current failing test.

Winter 2016CMPE212 - Prof. McLeod26 TDD, Cont. So, tests should be written at the same time as the production code. You are always one test ahead of what the production code can pass. As the amount of production code grows so do the number of tests. Testing code should be in separate files from production code. It really helps to have some kind of framework to help organize and run these tests!