Вы находитесь на странице: 1из 3

WORKING WITH JUNIT

Junit

Junit is a unit testing tool for java code testing.

We can install Junit as standalone application; in eclipse we by default get Junit.

Annotation: feature which is added to junit 4, it’s a Meta tab which gives information regarding the
methods placed to next of it.

Different Available Annotations in Junit4

Annotation Description

Annotation @Test identifies that this method is a test


@Test public void method() method. For running a unit test we must have at least
1test method.

Will perform the method() before each test. This


@Before public void method() method can prepare the test environment, e.g. read
input data, initialize the class)

@After public void method() Will perform the method after each teat method.

Will perform the method before the start of all tests.


@BeforeClass public void method() This can be used to perform time intensive activities
for example be used to connect to a database

Will perform the method after all tests have finished.


@AfterClass public void method() This can be used to perform clean-up activities for
example be used to disconnect to a database

Will ignore the test method, e.g. useful if the


underlying code has been changed and the test has
@Ignore
not yet been adapted or if the runtime of this test is
just to long to be included.

@Test(expected=IllegalArgumentException.class) Tests if the method throws the named exception

@Test(timeout=100) Fails if the method takes longer then 100 milliseconds


Difference between Junit3 and Junit4

Junit3 Junit4
1 Import junit.framework.* package 1 Import org.junit.* package
2 Need to Extend Junit.framwork.TestCase class 2 Need Not to extend any class.
3 Don’t use annotation for execution order 3 Use annotation to decode execution order .
we need at least 1 @test method to execute the
class

4 Assertion work same 4 Assertion work same


5 Fixture: are used to initializing and releasing, 5 In Junit for we have separate annotation
in Junit 3, we override TestCase class setup and @before and @after for that, we need not to
teardown method using override override anything

6 Test Method need to follow naming 6 Here we use @test annotation to define our
convention like method name should start with test method
test prefix with no argument and return type
7 Not Available 7 We can ignore a test to participate in
execution just by adding @ignore front or after
@test annotation
8 Not Available 8 We can verify expected annotation using
@Test(expected=ArithmeticException.class)

Advanced Fixture
Both classes use the new annotations @BeforeClass and @AfterClass as well as @Before and
@After. The main differences between these annotations are shown in Table 2.

Table 2. @BeforeClass/@AfterClass vs. @Before/@After

@BeforeClass and @AfterClass @Before and @After


Only one method per class can be annotated. Multiple methods can be annotated.
Order of execution is unspecified.
Overridden methods are not run.
Method names are irrelevant Method names are irrelevant
Runs once per class Runs before/after each test method
@BeforeClass methods of superclasses will be run @Before in superclasses are run before
before those of the current class. @AfterClass methods those in subclasses. @After in
declared in superclasses will be run after those of the superclasses are run after those in
current class. subclasses.
Must be public and static. Must be public and non static.
All @AfterClass methods are guaranteed to run even if a All @After methods are guaranteed to
@BeforeClass method throws an exception. run even if a @Before or @Test
method throws an exception.

Fixture Object (Object of interest)

 setUp method( we will find it in selenium test case, it override setup() method of
SeleniumTestCase(inherited to selenium test case) class, it is used for doing configuration
setting before running test case script.(We set before annotation in that to tell junit this method
must be executed before test method.
 tearDown() method(we will find it in selenium test case, it override tearDown() method of
SeleniumTestCase(inherited to selenium test case) class, it is used for doing configuration
setting after running test case script.(with after annotation)

Вам также может понравиться