An Introduction To Junit: Animesh Kumar
An Introduction To Junit: Animesh Kumar
Animesh Kumar
Toyota
http://blog.crisp.se/henrikkniberg/2010/03/16/1268757660000.h
tml
Reality turned out to be worse, a lot worse! Toyota’s problems with the Prius braking
systems is costing them over $2 000 000 000 (2 billion!) to fix because of all the recalls and
lost sales. “Toyota announced that a glitch with the software program that controls the
vehicle's anti-lock braking system was to blame".
Impetus Confidential 2
Why write tests?
Write a lot of code, and one day something will stop working!
Fixing a bug is easy, but how about pinning it down?
You never know where the ripples of your fixes can go.
Are you sure the demon is dead?
Impetus Confidential 3
Test Driven Development
Impetus Confidential 4
What is Unit Testing?
Impetus Confidential 5
What do we get?
Impetus Confidential 6
JUnit – An introduction
Impetus Confidential 7
Why choose JUnit?
Of course there are many tools like JUnit, but none like it.
Impetus Confidential 8
Design of JUnit
Impetus Confidential 9
Design of JUnit
Impetus Confidential 10
Write a test case
Impetus Confidential 11
Write a test case
Impetus Confidential 12
Asserting expectations
Impetus Confidential 13
Failure?
JUnit uses the term failure for a test that fails expectedly.
That is
An assertion was not valid or
A fail() was encountered.
Impetus Confidential 14
Write a test case
Impetus Confidential 15
Write a test suite
Impetus Confidential 16
Run your tests
Impetus Confidential 17
Run your tests
Textual Runner
>> java junit.textui.TestRunner AllTests
or,
junit.textui.TestRunner.run(AllTests.class);
Graphical Runner
>> java junit.swingui.TestRunner AllTests
or,
junit.swingui.TestRunner.run(AllTests.class);
Impetus Confidential 18
Test maintenance
> com -
> impetus - AllTests
> Book - BookTests
. AddBook - AddBookTest
. DeleteBook - DeleteBookTest
. ListBooks - ListBooksTest
> Library - LibraryTests
> User - UserTests
Impetus Confidential 19
Mocks
Impetus Confidential 20
EasyMock
Impetus Confidential 21
JUnit and Eclipse
Impetus Confidential 22
JUnit and build tools
Apache Maven
> mvn test
> mvn -Dtest=MyTestCase,MyOtherTestCase test
Apache Ant
<junit printsummary="yes" haltonfailure="yes">
<test name="my.test.TestCase"/>
</junit>
Impetus Confidential 23
Side effects – good or bad?
Impetus Confidential 24
How to approach?
Impetus Confidential 25
Resources
http://www.junit.org/index.htm
http://www.cs.umanitoba.ca/~eclipse/10-JUnit.pdf
http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsy
stem/build/tutorials/junit/junit.pdf
http://junit.sourceforge.net/javadoc/junit/framework/
Impetus Confidential 26