19-Aug-15 JUnit tests for output. Capturing output System.out.print and System.out.println usually “print” to the screen, but you can change that OutputStream.

Slides:



Advertisements
Similar presentations
JUnit Tutorial Hong Qing Yu Nov JUnit Tutorial The testing problems The framework of JUnit A case study JUnit tool Practices.
Advertisements

© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
Approach for Unit testing with the help of JUnit... Satish Mishra
1 From Yesterday private = accessible only to the class that declares it public = accessible to any class at all protected = accessible to the class and.
1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial.
JUnit, Revisited 17-Apr-17.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
22-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
24-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Writing a Unit test Using JUnit At the top of the file include: import junit.framework.TestCase; The main class of the file must be: public Must extend.
13-Jul-15 Effective Programming. “The new US stealth fighter, the F-22 Raptor, was deployed for the first time to Asia earlier this month. On Feb. 11,
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
Programmer Testing Testing all things Java using JUnit and extensions.
Io package as Java’s basic I/O system continue’d.
Laboratory Study October, The very first example, traditional "Hello World!" program: public class first { public static void main (String[ ]
Java Software Solutions Foundations of Program Design Sixth Edition
Preventing and Correcting Errors
Testing with Android Part I of II. Android Testing Framework Based on JUnit The Android JUnit extensions provide component-specific test case classes.
Automated GUI testing How to test an interactive application automatically?
ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.
CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
JUnit test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
1.  Writing snippets of code that try to use methods (functions) from your program.  Each snippet should test one (and only one) function......by calling.
Comments in Java. When you create a New Project in NetBeans, you'll notice that some text is greyed out, with lots of slashes and asterisks:
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
CS115 Math Class. Printing Out The command : System.out.println(“ Something that we want to print out on the screen as output”); Prints out in one line.
Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
JUnit Dwight Deugo Nesa Matic
JUnit Dwight Deugo Nesa Matic
© 2004 Pearson Addison-Wesley. All rights reserved April 24, 2006 Exceptions (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING 2006.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
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.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
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.
JUnit A Unit Testing Framework for Java. The Objective Introduce JUnit as a tool for Unit Testing Provide information on how to: Install it Build a test.
CPSC 871 John D. McGregor Module 8 Session 3 Assignment.
1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that.
I/O in java and Revision. 2 I/O in Java Many packages and libraries associated with Java provide sophisticated ways to input and output information, e.g.:
Test automation / JUnit Building automatically repeatable test suites.
29-Jun-16 Effective Programming. “The new US stealth fighter, the F-22 Raptor, was deployed for the first time to Asia earlier this month. On Feb. 11.
CSE 1201 Object Oriented Programming
CSC1351 Class 6 Classes & Inheritance.
Exercise Java programming
Unit testing Java programs Using JUnit
Maha AlSaif Maryam AlQattan
CS102 – Exceptions David Davenport Latest: May 2015
Code Magnets problem for wk5
I/O Basics.
Introduction to Objects
System.out.println for console output
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Testing and debugging A short interlude 2-Dec-18.
Effective Programming
Introduction to Computing Using Java
Effective Programming
Suppose I want to add all the even integers from 1 to 100 (inclusive)
Testing and debugging A short interlude 17-Apr-19.
import junit. framework
Drawing complex figures
Joel Adams and Jeremy Frens Calvin College
TCSS 360, Spring 2005 Lecture Notes
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
Introduction to Objects
Presentation transcript:

19-Aug-15 JUnit tests for output

Capturing output System.out.print and System.out.println usually “print” to the screen, but you can change that OutputStream os = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(os); System.setOut(ps); If you do this, though, you probably want to change these methods back to “normal” when you are done PrintStream originalOut = System.out; (code from above) System.setOut(originalOut);

A class to test public class CaptureOutput { void myPrint(String message) { System.out.print(message); } void myPrintln(String message) { System.out.println(message); } }

JUnit framework import java.io.ByteArrayOutputStream; import java.io.OutputStream; import java.io.PrintStream; import junit.framework.TestCase; public class CaptureOutputTest extends TestCase { CaptureOutput capture; protected void setUp() throws Exception { super.setUp(); capture = new CaptureOutput(); } // test methods go here }

Testing myPrint public final void testMyPrint() { // Prepare to capture output PrintStream originalOut = System.out; OutputStream os = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(os); System.setOut(ps); // Perform tests capture.myPrint("Hello, output!"); assertEquals("Hello, output!", os.toString()); // Restore normal operation System.setOut(originalOut); }

Testing myPrintln This does not work: capture.myPrintln("Hello, output!"); assertEquals("Hello, output!\n" + separator, os.toString()); Why not? Line separators are different on different systems! You need to add, not "\n", but the correct line separator! Here’s the correct code: String separator = System.getProperty("line.separator"); capture.myPrintln("Hello, output!"); assertEquals("Hello, output!" + separator, os.toString());

The End