Part1:
Unit Testing – Course Registration approval (12 Points)
- Watch the steps in the video tutorial named Unit Testing in Java, Part 2 (canRegister method).
- Pay special attention to the very last frame, where a table is shown.
- I want to see the table either in an Excel Spreadsheet or in a nice table in a Word document.
- You should have the four of the listed canRegister tests, plus you must add at least four additional tests to the table.
Part 2: Java Skills Review (16 Points)
- In this lab, you will create an account at codingbat.com and practice some elementary Java skills. It’s a great practice site, created by Nick Parlante, an instructor at Stanford University.
- Please create an account and then solve the following problems (under the Java heading).
- To get to the Array-2 section, click the “All Java Sections” link at the top & bottom of the page:
Array-1: firstLast6 | Array-2: matchup |
Array-1: midThree | Array-2: tenRun |
Array-1: plusTwo | Array-2: isEverywhere |
Array-1: maxTriple | Array-2: sameEnds |
- When you have finished solving the Codingbat problems, click the “done” hyperlink in the upper right corner of the Web browser window, causing Codingbat to display a graphical report of your work.
- Next, copy the URL from your Web browser’s address box to the Windows clipboard (use Ctrl-C).
- Paste the URL into a Word document. I do not need the results in the document, just the URL.
Here’s an example of what a report looks like:
Submit a zip file called FirstnameLastnamelab2.zip. It should contain: (2 Points)
- The Word document or Excel Workbook from Part 1.The Word document with your Codingbat results from Part 2.Do not include any other files or folders
Rubric
Lab 2 Rubric
Criteria | Ratings | Pts | |
---|---|---|---|
This criterion is linked to a Learning OutcomePart 1: Unit Testing – Word Table or Excel SpreadsheetPay special attention to the very last frame,where a table is shown. You must use this tableto add more unit tests to your program. Submit either an Excel spreadsheet or a Word document with the table. |
1 |
2 pts |
|
This criterion is linked to a Learning OutcomePart 2: Java Skills ReviewComplete the listed problems on the codingbat.com website. Provided the url of your results.Array-1: firstLast6; Array-1: midThree;Array-1: plusTwo; Array-1: maxTriple;Array-2: matchup; Array-2: tenRun;Array-2: isEverywhere; Array-2: sameEndsSubmit the URL from your web browser’s address box in a Word document as a clickable link. |
16 pts |
||
This criterion is linked to a Learning OutcomeSubmissionSubmitted a zip file with the Word document with the link in it along with either a Word document or Excel Spreadsheet with the table in it. The submission file should be called FirstnameLastnamelab2.zip using your name. |
2 ptsExcellentZip file submitted properly with the two files and no additional files or folders.1 ptsGoodSubmitted a zip file but there were extra files or folders.0 ptsPoorDid not submit a zip file or it could not be processed. | ||
Total Points: 30 |
- Tutorial: Java Unit Testing with JUnit – How to Create And Use Unit TestsLinks to an external site.
- Slideshow: Unit Testing in Java (PDF) Actions
- Reasons why Unit Testing is important (PDF) Actions(from Irvine Advanced VB bookLinks to an external site.
- API Documentation on the Assert class Links to an external site.
- Code Access Security (PDF)Actions
- Microsoft article: Code Access SecurityLinks to an external site.
- Java Coding GuidelinesLinks to an external site.
- Do not log unsanitized user inputLinks to an external site.
- Normalize strings before validating themLinks to an external site.
Watch the following videos:
- Video: Unit Testing in Java using Eclipse, Part 1Links to an external site.
- Video: Unit Testing in Java using Eclipse, Part 2Links to an external site.
Unit Testing in Java
by Kip Irvine
COP 4814
Revision date: Nov 29, 2009
(c) 2008-2009 Kip Irvine, all rights reserved. You may modify and copy this slide show for your personal use as long as
this copyright statement is included.
Overview
•
•
•
•
•
•
•
•
•
•
What is a Unit Test?
Why do Unit Testing?
Goals
Test-Driven Approach
Common Excuses
Example 1: Largest
Running JUnit Tests
Static methods & asserts
Example 2: Stack
Reference Sources
2
Goals of Software Testing
1. Does the application code accomplish the basic
goals of the application?
2. Does the code work with a variety of inputs?
3. Will it stand up under extreme types of input?
4. Does the code document my intent?
3
What is a Unit Test?
• A method that tests results produced by another
method
• proves (or disproves) that individual methods work as
expected
• Called a structural test, or white box test because it
requires knowledge of the source code details
4
Two Classes
• Class being tested
• called the class under test, or target class
• contains methods that do the actual work
• Test class
• also called a Test Fixture
• contains methods that test the Application class
methods
5
Here’s What Unit Testing Can Do
• Reduces the time you spend debugging,
particularly at the end of a project
• Improves reliability of application code
• Minimizes bug fixes that affect existing code
(run regression tests after fixing a bug)
6
source: www.verisoftindia.com
7
Test-Driven Approach
1. Write several unit tests before writing the target
application code
2. The test should document the application code’s
intent
3. Run the tests and watch them fail
4. Write application code that permits the tests to pass
8
I’m already busy writing code! Why should
I write even more?
9
Common Excuse for not testing:
Writing tests takes up too much time
Rebuttal:
• Unit Testing saves time—why?
• It’s easier to write tests while you still remember the
code
• Waiting until the end means dealing with greater
complexity
• reworking defective code under pressure
• tracking down bugs to their source in a large program
• you might create new bugs when fixing old ones
10
Task completion (x) vs Time (y)
30
25
20
Code, then test
TDD
15
10
5
0
1
2
3
4
5
6
7
8
9
11
Java Unit Tests
12
Example 1: getLargest
• ArrayUtils class contains a method named getLargest
that returns the largest element in an array of integers
• Create the TestArrayUtils class to test methods in
the ArrayUtils class
• testGetLargest_1 calls ArrayUtils.getLargest
• testGetLargest_2 calls ArrayUtils.getLargest
• etc.
See: ArrayUtils.java, TestArrayUtils.java
13
JUnit Framework
• Download from http://JUnit.org
• Created by Kent Beck and Erich Gamma
• Current version: 4.x
View the Course topics page
14
Running Tests
• In Eclipse, select the test class and execute it as a
JUnit application.
• Alternatives:
• Graphical TestRunner application:
java junit.swingui.TestRunner
• Text-based TestRunner application:
java junit.textui.TestRunner classname
15
JUnit Static Methods
• Compare primitive and object types:
assertEquals( [String message], expected, actual )
• Compare primitive and object arrays:
assertArrayEquals( [String message], expected[],
actual[] )
• For floating-point values, use:
assertEquals( [String message], expected, actual,
tolerance )
• Set tolerance to a small value, such as 0.001
16
More JUnit Asserts
assertNull( [String message], object )
assertNotNull( [String message], object )
assertSame( [String message], expected, actual )
// Returns true if expected and actual refer to the same
// object.
assertNotSame( … )
assertTrue( [String message], boolean condition)
assertFalse( … )
fail( [String message] );
// Forces the current test to fail when this statement
// executes.
17
Controlling the Order of Execution
@Before
method executes before each test
@After
method executes after each test
@Ignore( String message )
do not test this method
18
Example 2: Stack Class
• Holds strings
• Methods:
• push, pop, empty full, clear, stackSize, count
19
Some Tests We Can Run
1. Does count() return the correct values before and after calling
push()?
2. Does count() return the correct values before and after calling
pop()?
3. Does full() return true when count() equals stackSize()?
4. Is an exception thrown when calling push() on an empty stack?
5. Is an exception thrown when calling pop() on an empty stack?
6. Is an exception thrown when calling push() on a full stack?
7. Does the full() method return false after one or more values
have been pushed on the stack?
20
More Tests
8. After a value is pushed on the stack, is the same value
returned by pop()?
9. After multiple values are pushed, is the first value returned by
pop() equal to the last value pushed?
10. Does empty() return true after all values have been popped
from the stack?
21
Code Coverage
• Determine if the unit tests have covered a large
percentage of the code under test.
• Criteria:
• has every method been called?
• has every statement been executed?
• has each branch been executed?
• has each boolean expression been evaluated to both
true and false?
• has each state in a finite-state machine been reached
and explored?
• have all common values for parameters been
considered?
22
Adding EclEmma to Eclipse 4
• From the Help menu, select Install New Software
• Click the Add button:
23
Adding EclEmma
• Enter the following into the dialog and click OK.
24
Click the Checkbox and click the Next button
25
More Steps…
26
Finishing the Installation
• You will be prompted to restart Eclipse.
• When Eclipse restarts, go to next slide
27
Showing the Coverage View
• Select Show View from the
Window menu
• Select Other…
• Select Java
• Select Coverage, click OK.
• Run your unit tests by clicking the
new green and red launch button
just left of the debug button in the
toolbar.
28
Source Code Annotations
29
Coverage View Details
30
Reference Sources
• JUnit organization: http://junit.org/
• JUnit Cookbook (Kent Beck, Erich Gamma, )
• Get Acquainted with JUnit 4
• JUnit FAQ (SourceForge article)
• Using JUnit with Eclipse
• Next-Generation Java Testing (Cedric Beust)
• EclEmma Code Coverage tool
31
The End
32