DOC PREVIEW
Penn CIT 594 - Testing and debugging

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Testing and debuggingPrint statementsDebugging output methods, IDebugging output methods, IIProgram to capture outputtoString()Review: Overriding methodsThe EndJan 13, 2019Testing and debuggingA short interludePrint statementsAn old-fashioned, but still very useful, debugging technique is putting in print statementsHowever, it’s a nuisance to remove them again (then add them again, then remove them again...)Here’s a helpful technique:boolean debugging = true;debug("Some debugging message...");private void debug(Object obj) { if (debugging) System.out.println(obj);}Debugging output methods, IJUnit testing should not produce any output that you have to look at--it should be fully automaticFirst, consider whether you can separate the output method into one method that computes the output, and another method that actually prints itThen you can test the former methodSince a String can contain newlines, you are not restricted to single-line outputSeparation of concerns is good practice in any caseDebugging output methods, IIYou can also change (maybe temporarily) where the output goesPrintStream usualSystemOut = System.out; // save old streamSystem.setOut(someOtherPrintStream); // use new streamSystem.setOut(usualPrintStream); // change it backYou can write to a file, then test whether the file has the right contentsHere's a more sophisticated approach:Use a ByteArrayOutputStream to capture the output in a byte arrayUse the ByteArray's toString() method to examine the outputComplete sample code is on the next slideProgram to capture output public static void main(String[] args) { PrintStream originalOut = System.out; OutputStream os = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(os); System.setOut(ps); System.out.print("Hello, output!"); System.setOut(originalOut); System.out.println("I got: [" + os.toString() + "]"); }I got: [Hello, output!]toString()In general, you want to avoid unnecessary methods in your classes, but......It’s usually a good idea to override toString()Having a toString() method often simplifies debugging, because you can see what your objects really areDon’t use your toString() methods to compare objects in your JUnit tests, unless you are very sure you will never change the string representation of your objectsFor JUnit testing (especially with assertEquals), you should have a good version of public boolean equals(Object o)The default equals method tests identity, not equality, and this is probably not what you wantReview: Overriding methodsTo override a method is to write a method that has the exact same signature as an inherited methodThe names of parameters are not part of the signature, so they can be different if you really wantYou cannot override a method with a less public method (but it can be more public if you want)You cannot throw any checked exceptions that the inherited method doesn’t throwIn Java 5, you should precede your method with @OverrideExamples:public String toString() { ... }public boolean equals(Object o) { ... } // Notice type of parameter!The EndEclipse Hints ● You can put a “task” in any comment: ● TODO -- code to be written ● FIXME -- error to be fixed ● XXX -- need to think more about this To show tasks, choose ● Window  Show view  Other... 


View Full Document

Penn CIT 594 - Testing and debugging

Documents in this Course
Trees

Trees

17 pages

Searching

Searching

24 pages

Pruning

Pruning

11 pages

Arrays

Arrays

17 pages

Stacks

Stacks

30 pages

Recursion

Recursion

25 pages

Hashing

Hashing

24 pages

Recursion

Recursion

24 pages

Graphs

Graphs

25 pages

Storage

Storage

37 pages

Trees

Trees

21 pages

Arrays

Arrays

24 pages

Hashing

Hashing

24 pages

Recursion

Recursion

25 pages

Graphs

Graphs

23 pages

Graphs

Graphs

25 pages

Stacks

Stacks

25 pages

Recursion

Recursion

25 pages

Quicksort

Quicksort

21 pages

Quicksort

Quicksort

21 pages

Graphs

Graphs

25 pages

Recursion

Recursion

25 pages

Searching

Searching

24 pages

Counting

Counting

20 pages

HTML

HTML

18 pages

Recursion

Recursion

24 pages

Pruning

Pruning

11 pages

Graphs

Graphs

25 pages

Load more
Download Testing and debugging
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Testing and debugging and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Testing and debugging 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?