Junit Presentation
Junit Presentation
Prepared By:
Priya Sharma Trivedi
Introduction
What is JUnit?
Downloading and installing JUnit
Writing JUnit tests
Creating a test suite
Output of a JUnit test
Textual and graphical Test Runners
Demo
Conclusions
What is JUnit?
JUnit is an open source framework that has been designed for the
purpose of writing and running tests in the Java programming
language.
Originally written by Erich Gamma and Kent Beck. There are many
ways to write test cases.
Regression-Testing Framework
JUnit framework
An example
package org.example.antbook.junit;
import junit.framework.TestCase;
Fixtures
A fixture is just a some code you want run before every test
You get a fixture by overriding the method
protected void setUp() { }
The general rule for running a test is:
protected void runTest() {
setUp(); <run the test> tearDown();
}
so we can override setUp and/or tearDown, and that code will
be run prior to or after every test case
Test suites
Sample Assertions
Create test cases in the same package as the code under test.
For each Java package in your application, define a TestSuite
class that contains all the tests for validating the code in the
package.
Define similar TestSuite classes that create higher-level and
lower-level test suites in the other packages (and sub-packages)
of the application.
Make sure your build process includes the compilation of all tests.
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
We write the test methods before we write the code
This has the advantages described earlier
Depending on the JUnit tool we use, we may have to create the class
first, and we may have to populate it with stubs (methods with empty
bodies)
Dont be alarmed if, in this simple example, the JUnit tests are more code
than the class itself
Result????
The name of the method should start with the word 'test'.
The return type of the method should be null.
The method shouldn't have any parameter.
You should write a test case for the method which is less dependent on sub
method. If you write a test case for dependent methods, your test may fail
because the sub method can return wrong value. So, it may take longer to find
out the source of the problem.
Each unit test should be independent of other test. If you write one unit test for
multiple methods then the result may be confusing.
Test behavior is more important than methods. Each test case has to be in the
same package as the code to be tested. For example if we want to create
classes in root.baseclasses, then test cases must be in the same package.
Although, it is nice to cover most of the code in the test cases, it is not
advisable to use 100% of the code. If the method is too simple then there is no
point of writing unit test for it.
You should use object oriented best practice for your test code. You
should bring all of the common functionalities in the base class. That
way you can eliminate duplicate code, long methods etc.
You should use proper method name. it is going to be easier for the
team to maintain the code in future.
The Junit framework automatically handles the exception. It is good
idea not to throw an exception in a test that shouldn't throw an
exception. Otherwise, it can hide bugs which may be discovered
much later stage.
JUnit doesn't provide all of the functionalities to test your code. So,
you need an additional test tool. Some of the tools are DBunit,
Selenium etc that can be used as an extension of junit.
Instead of rely on System.out.println() method to figure out the
success of the test, you can rely upon the 'assert' method of JUnit so
that you can run the automated test and that way the tester doesn't
need to monitor the test in person all the time.
The End