Approach of Unit Testing With The Help of Junit
Approach of Unit Testing With The Help of Junit
Approach of Unit Testing With The Help of Junit
Why testing?
Improve software design
Make software easier to understand
Reduce debugging time
Catch integration errors
In short, to Produce Better Code
Preconditions
Working code
Good set of unit tests
Input Output
Input Output
Unit
Individual classes or types
Component
Group of related classes or
types
Integration
Interaction between classes
assertNotSame(Object expected, Object actual)
assertNull(Object object)
Unit testing with JUnit 30
assertX methods
assertNotNull(Object object)
fail()
Causes the test to fail and throw an AssertionFailedError
Useful as a result of a complex test, when the other assert
methods aren’t quite what you want .
TestCase TestSuite
run(TestResult) run(TestResult)
TestResult
setUp() addTest(Test)
runTest()
tearDown()
setUp()
TestedClass runTest()
action()
tearDown()
test1()
test2()
JUnit
fName
framework
test1()
runTest() or
test2()
Unit testing with JUnit 33
Example: Counter class
For the sake of example, we will create and test a
trivial “counter” class
The constructor will create a counter and set it to zero
The increment method will add one to the counter and
return the new value
The decrement method will subtract one from the counter
and return the new value
import example.SimpleTest;
import example.HtmlDocumentTest; Demo
ITextSelection selection =
(ITextSelection) yourEditor.
getSelectionProvider().
getSelection();
selection.getOffset());