JUnitIntro

JUnitIntro - JUnit Introduction to Unit Testing in Java...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: JUnit Introduction to Unit Testing in Java Testing, 1 2 3 4, Testing... 1 What Does a Unit Test Test? The term "unit" predates the O-O era. Unit "natural" abstraction unit of an O-O system: class or its instantiated form, object. Unit Tests verify a small chunk of code, typically a path through a method or function. Not application level functionality. How Do We Unit Test? Print Statements (diffs against benchmarks) Debuggers examine variables, observe execution paths. Typically done by unit developer. Best benefit if running of tests is automated. Tests best run in isolation from one another. Tests built incrementally as product code is developed. 2 The Typical Test Cycle Develop a suite of test cases Create some test fixtures to support the running of each test case. Run the test capture test results. Clean-up fixtures, if necessary. Report and analyze the test results. Why is Unit Testing Good? Identifies defects early in the development cycle. Many small bugs ultimately leads to chaotic system behavior Testing affects the design of your code. Successful tests breed confidence. Testing forces us to read our own code spend more time reading than writing Automated tests support maintainability and extendibility. 3 Why Don't We Unit Test? "Coding unit tests takes too much time" "I'm to busy fixing bugs to write tests" "Testing is boring it stifles my creativity" "My code is virtually flawless..." "Testing is better done by the testing department" "We'll go back and write unit tests after we get the code working" What is JUnit? JUnit is an open source Java testing framework used to write and run repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include: Assertions for testing expected results Test fixtures for sharing common test data Test suites for easily organizing and running tests Graphical and textual test runners 4 JUnit Under the Hood Originally written by Kent Beck and Erich Gamma. design patterns. An offspring of a similar framework for Smalltalk (SUnit) A common xUnit architecture has evolved and has been implemented in a variety of languages. The JUnit Test Template Create a test class that extends TestCase Create a testxxx() method for each individual test to be run. Create a test fixture resources needed to support the running of the test. Write the test, collect interesting test behavior Tear down the fixture (if needed) Run the tests with a text or Swing interface. 5 SimpleTest import java.util.*; java.util.*; import junit.framework.*; junit.framework.*; public class SimpleTest extends TestCase{ public void testEmptyCollection() { testEmptyCollection() Collection testCollection = new ArrayList(); ArrayList(); assertTrue( testCollection.isEmpty()); assertTrue( testCollection.isEmpty()); } public static void main(String args){ args){ junit.textui.TestRunner.run(SimpleTest.class); junit.textui.TestRunner.run(SimpleTest.class); } } Key JUnit Concepts assert assertEquals( expected, actual ) also NotEquals assertEquals( assertNull( actual result ) also NotNull assertNull( assertTrue( actual result) - also False assertTrue( failures Exceptions raised by asserts (expected) errors Java runtime exceptions (not expected) 6 Test Hierarchies JUnit supports test hierarchies Test Suite-A SuiteTest Case1 Test Case2 Test Suite-B SuiteTest Case3 Test Suite-C Suite( and so on ...) 7 ...
View Full Document

This note was uploaded on 05/06/2010 for the course SE 4010-361 taught by Professor Yedduladoddi during the Winter '10 term at RIT.

Ask a homework question - tutors are online