New User Special Price Expires in

Let's log you in.

Sign in with Facebook


Don't have a StudySoup account? Create one here!


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here


by: Mrs. Maximo Lueilwitz


Marketplace > Oregon State University > ComputerScienence > CS 162 > INTRODUCTION TO COMPUTER SCIENCE II
Mrs. Maximo Lueilwitz
GPA 3.95

W. Wong

Almost Ready


These notes were just uploaded, and will be ready to view shortly.

Purchase these notes here, or revisit this page.

Either way, we'll remind you when they're ready :)

Preview These Notes for FREE

Get a free preview of these Notes, just enter your email below.

Unlock Preview
Unlock Preview

Preview these materials now for free

Why put in your email? Get access to more of this material and other relevant free materials for your school

View Preview

About this Document

W. Wong
Class Notes
25 ?




Popular in Course

Popular in ComputerScienence

This 5 page Class Notes was uploaded by Mrs. Maximo Lueilwitz on Monday October 19, 2015. The Class Notes belongs to CS 162 at Oregon State University taught by W. Wong in Fall. Since its upload, it has received 10 views. For similar materials see /class/224489/cs-162-oregon-state-university in ComputerScienence at Oregon State University.

Similar to CS 162 at OSU

Popular in ComputerScienence




Report this Material


What is Karma?


Karma is the currency of StudySoup.

You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 10/19/15
JUnit Version C8162 IntrOdUCtion to NOTE We are using JUnit 41 If you are Computer Science using JUnit 38 the material on these slides Will not work JUnit Outline 1 Setting up JUnit 1 Setting up JUnit What is JUnit 2 The basics Test asserts test A tool to help design and organize test fixtures cases 3 Special tags Exceptions lgnore Design a companion test class for each 4 Running Tests Class YOU deVelOP If your class is called MyClass you call the companion test class MyClassTest 1 Setting up JUnit 1 Setting up JUnit Key features JUnit is typically included in your Eclipse lncrementally add tests as software grows download Update tests once you nd a bug It is also available at httpjunitorg Runs automatically and tests can check their 39 A 9000 intrOdUCtorY tUtOFial is at own results hftn39llilmi r Visual feedback on test results OKhtm Test entire code base or any subset of your code 1 Setting up JUnit lfyou are having problems using JUnit 39om Eclipse you probably need to include it in your project s build path Howto do this 7 Right click oh your proiect in the Package Explorer 7 Select properties 7 Select Java Build Path 7 Click ohtheliorariestao 7 Click oh Add External Jars 7 Go to your Eclipse directory 7 Go to the plugihs Subdlrectory 7 Go to Ol glul llMJl 1 01 orsorhethihg airhilar e Selectlunlt741lar 1 Setting up JUnit 1 Create a JUnit test case Click on the package you want to add the test case to Go to File a New a JUnit Test Case Give it a name hit finish 2 Notice that it looks just like a java class 3 Add the line import orgjunit 4 Add the line import static orgjunitAssert 2 The Basics Test To add test functions all you have to do is put a Test in ronto t e met 0 Test functions are special as they are run automatically by JUnit import orgjun1tTest public class TreasurechestTest l Test public void adolcololo l l 2 The Basics Asserts The recipe Write the code which runs the function you are testing Check the results using the asserts provided by JUnit N 2 The Basics Asserts To check results you will need to check serts eg assertEqualsexpected actual The value your code computes goes here The correct value goes here 2 The Basics Asserts assertEqualsmessage expected actual assertEqualsexpected actual deta assertEqualsmessage expected actual delta Used on doubles or oats where delta is the acceptable difference between expected and actual such that they are considered equal assertFalsecondition assertFalsemessagecondition Used to assert that condition is false assertTruecondition assertTruemessage con iion Used to assert that condition is true fail failmessage Causes yourtest to fail 2 The Basics Asserts A simple test case example import orgun1tTest public class TreasurechestTest l Test public void addGold i new Treasurechest tcaddGold1El assertEquals 2mm checkGold 2 The Basics Test Fixtures Test public void addGold i Treasurechest to new Treasurechest tcaddGold 1D assertEqualS 2Dccheckcold 0 Test public void removeGoldO i Treasurechest tC new Treasurechest tcremoveGold 1D assertEqualSDtc checkGold Notice that We need to set up a TreasureChest object in each test You could repeat the code as above but there is another alternative M 2 The Basics Test Fixtures Sometimes you may have 2 or more test functions that use the same set of objects This set of objects is called a test fixture To use a test fixture 1 Create a membervariable for each object in the test xture 2 Write a method to set up the test xture member variables and use the Before tag 3 Write a method to clean up the test xture member variables if necessary and use the After tag 2 The Basics Test Fixtures Note Methods tagged with Before are called before every test method Methods tagged with After are called after every test method Why Ensure no side effects between test function runs 2 The Basics Test Fixtures public class i private Treasurechest to Before public void scwpo w Treasurcchcstuu l Test public void addcoldo l tcaddcold1u assertEquals 1Dtc checkGold Test public void removecoldo i tcremoveGold1D assertEqualS Dtccheckcold 3 Special Cases Exceptions We would like to test ifthe following function throws the exception appropriately public void addendum gold thrwws Runeimeaxcepeiun i if lt n l w RuntlmeExcept10nquotNegatlve amount for e allowedquot l thisnumeold gold 3 Special Cases Exceptions 3 Special Cases Ignore Sometimes you need to ignore a test Notice that We declare What exception We expect to be thrown To do 507156 me Ignore ag 39 You Will need to import org junit Ignore TeSt expeCtedRuntimeExceptlon 1355 39 Ifyou ignore a test make sure you include amessage to say public void addGoldExceptlon l Treasurechest tc new Treasurechest Why you are ignoring it 7 Ignore Not ru nlng because lt1nsen reesongtquot tc39addGold 10 Test expectedZRuntlmeExceptlonclass assertEqualslO tccheckGold public vold dd ldExceptlonO i Treasurechest tc new Treesurechesto cceddeoioi n assertEqualS 1ncccheckcoid 77 4 Running Tests 4 Running Tests Test Suites Ween mum quot 5quotmquot mmquotds V 39 Right MP Y9 You can combine TestCases into i39ya i7 TestCase In Eclipse s T ts t m 33 EErmrs u EFailures u package Exporer es u39 es Select Run As gt You can also combine TestSuites into 5 Junquot Test other TestSuites to get a hierarchy gunmen A er running it you maddearmvm will see a result like Lets you run all your tests or you can that shown on the Ie run a subset ofthem All green means all yourtests passed Note There Is an option In Eclipse 8 to create a Test SUIte file Do not use It It T 71 is from the old version of JUnit 4 Running Tests Test Suites 4 Running Tests Test Suites To create a test suite create an empty class With RunWith and Suite tags I ll call the empty class AllTests in this example Tihe Runw th tag tells JUmL to mquot we Org1mm runners Suns c ass import org junlt runnerRunW1th import org junlt runners Sulte The Suite SuiteClasses tag tells JUnit Which test classes are included in this Suite Runwlth5u1tec1a55 import org junlt runner Runw1th Sultesu1teclasses t import org junlt runners Suite TreasurechestTestc1ass P1rateTestc1ass Runw1th5u1teclass Sulte5u1teClasses t public class AllTests asurechestTestclasSy PlrateTestclass l l public class AllTests t 22 l m m 4 Running Tests Test Suites my yuu wuma run aregular mm test TreasureChestTest and Pn39ateTest have bean


Buy Material

Are you sure you want to buy this material for

25 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

Why people love StudySoup

Jim McGreen Ohio University

"Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

Allison Fischer University of Alabama

"I signed up to be an Elite Notetaker with 2 of my sorority sisters this semester. We just posted our notes weekly and were each making over $600 per month. I LOVE StudySoup!"

Bentley McCaw University of Florida

"I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"


"Their 'Elite Notetakers' are making over $1,200/month in sales by creating high quality content that helps their classmates in a time of need."

Become an Elite Notetaker and start selling your notes online!

Refund Policy


All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email


StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here:

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.