Monday, 29 February 2016

Junit is an open source unit test framework used by developers(no restriction for QA, they too can use it :) ). It  uses Java as the programming platform(But that does not mean you need to have deep knowledge of java language).
Untitled drawing (10).jpg In this blog I would be focusing only on the Annotations being used in JUNIT. Would try to explain them with the help of an example in detail.

JUNIT annotations

Annotations1

 

Now let me take each annotation in detail followed by a code snippet.
  1.  @BeforeClass

    public static void method()

    This method is executed once before the start of all he test cases.It is used to perform time intensive activities, for example - to connect the database.Method annotated with this annotation need to be static.

    2. @AfterClass

    public static void method()

    This method is executed once,after all tests have been finished.It is performed to do cleanup activities, for example - to clean database connections. Method annotated with this annotation need to be static.

    3. @Before

    Public void method()

    This method is executed before each test.It is used to prepare the test environment(eg - reading the input data/file, initialize the variable).

    4. @After

    Public void method()

    This method is executed after each test.It is used to cleanup the test environment(eg - deleting temporary data, restore defaults etc. ).

    5. @Test

          This annotation identifies a method as a test method. This annotation indicates         JUNIT that these are the test cases that needs to run.Following is code snippet for five JUNIT annotations as discussed above....
Untitled drawing (11)

 

 Result/output from console for the above code snippet -

Untitled drawing (13)6. @Test(Timeout=1000)

Fails if the method takes longer than 1000 milliseconds.
Untitled drawing (14).jpg

7. @Test(expected = Exception.class)

Fails if the method does not throw the named Exception
Untitled drawing (15)

8. @Ignore

In case you want to ignore any particular test case from execution,the use @Ignore.This is useful when underlying code has been changed and the test case has not yet been adapted.
Untitled drawing (16).jpg

9. @Parameterized

@Parameterized test means using same methods again and again by passing different values.
Untitled drawing (17)

10. @Runwith and @Suite

The @suite means bundle few unit test cases and run them together. In JUNIT, both @Runwith and @suite annotations are used to run the suite test.
Untitled drawing (18)

11. @FixMethodorder(MethodSorters.Name_ascending)

This annotation is to define that the test methods are sorted by method name in lexicographic order.
To activate this feature,annotate your test class with @FixMethodorder(MethodSorters.Name_ascending).

Basic Interview question around JUNIT

Most of the questions you would be able to answer after reading the above blog. But if not then I will give you answers along , below –
  1. What is JUnit?
2. What is Unit Testing?
Unit testing is the testing of single entity (class or method). Unit testing is very essential to every software company to give a quality product to their customers.
3. What are important features of JUnit?
Following are the important features of JUnit −
  • It is an open source framework.
  • Provides Annotation to identify the test methods.
  • Provides Assertions for testing expected results.
  • Provides Test runners for running tests.
  • JUnit tests can be run automatically and they check their own results and provide immediate feedback.
  • JUnit tests can be organized into test suites containing test cases and even other test suites.
  • JUnit shows test progress in a bar that is green if test is going fine and it turns red when a test fails.
4. Name the tools with which JUnit can be easily integrated.
JUnit Framework can be easily integrated with either of the followings −
  • Eclipse
  • Ant
  • Maven
5. What are the core features of JUnit?
JUnit test framework provides following important features −
  • Fixtures
  • Test suites
  • Test runners
  • JUnit classes
6. What are the different annotations used in JUNIT?
7. What is a fixture?
Fixture is a fixed state of a set of objects used as a baseline for running tests. The purpose of a test fixture is to ensure that there is a well known and fixed environment in which tests are run so that results are repeatable. It includes following methods −
  • setUp() method which runs before every test invocation.
  • tearDown() method which runs after every test method.
8. What is a test suite?
Test suite means bundle a few unit test cases and run it together. In JUnit, both @RunWith and @Suite annotation are used to run the suite test.
9. What is a test runner?
Test runner is used for executing the test cases.
10. What are JUnit classes? List some of them.
JUnit classes are important classes which are used in writing and testing JUnits. Some of the important classes are−
  • Assert − It contains a set of assert methods.
  • TestCase − It contains a test case defines the fixture to run multiple tests.
  • TestResult − It contains methods to collect the results of executing a test case.
  • TestSuite − It is a Composite of Tests.
11.What is the purpose of @Test annotation in JUnit?
12. What is the purpose of @Before annotation in JUnit?
13. What is the purpose of @After annotation in JUnit?
14.What is the purpose of @BeforeClass annotation in JUnit?
15.What is the purpose of @AfterClass annotation in JUnit?
16.What is @Ignore annotation and how is this useful?
JUnit always creates one instance of the test class for each @Test method.
Good tests do not have any order-of-run dependencies  and creating fresh instances of the test class and its instance variables for each test is crucial in achieving this. Some testing frameworks reuse the same test class instance for all tests, which leads to more possibilities of accidentally creating side-effects between tests.
And because each test method has its own instance, it makes no sense for the @BeforeClass/@AfterClass methods to be instance methods. Otherwise, on which of the test class instances should the methods be called? If it would be possible for the @BeforeClass/@AfterClass methods to reference instance variables, then only one of the @Test methods would have access to those same instance variables - the rest would have the instance variables at their default values - and the @Test method would be randomly selected, because the order of methods in the .class file is unspecified/compiler-dependent.
So enforcing those methods to be static is the only reasonable solution.
 

No comments:

Post a Comment