Saturday, 14 March 2015

Junit

Junit is a testing framework for java programming language. Junit has been important in the development of  test driven programming, and it is one of family in testing frameworks.
Testing is the process of checking the functionality of s/w weather it is working as per requirements or not. Unit testing is the testing of single entity. Unit testing is the essential to every company to give quality product to their consumers.
Manual testing
Automated testing
Executing the test cases manually without any tool support is known as manual testing.
·         Time consuming and tedious: Since test cases are executed by human resources so it is very slow and tedious.
·         Huge investment in human resources: As test cases need to be executed manually so more testers are required in manual testing.
·         Less reliable: Manual testing is less reliable as tests may not be performed with precision each time because of human errors.
·         Non-programmable: No programming can be done to write sophisticated tests which fetch hidden information.
Taking tool support and executing the test cases by using automation tool is known as automation testing.
·         Fast Automation runs test cases significantly faster than human resources.
·         Less investment in human resources:Test cases are executed by using automation tool so less tester are required in automation testing.
·         More reliable: Automation tests perform precisely same operation each time they are run.
·         Programmable: Testers can program sophisticated tests to bring out hidden information.

Features:

·         It is open source tool for writing and running tests.
·         Provides the annotations to identify the tests.
·         Provides the assertions to expected results
·         Provides the test runners to running the tests
·         Junit allows you to write faster which increase the quality
·         Junit runs automatically. Which provides faster results.
Junit provides the following important features.
·         Fixtures
·         Test suites
·         Test runners
·         Junit classes
Fixtures:
Fixtures is a fixed state of set of objects used to balancing the test scripts. These will runs before and after test method execution.
1.       setup()
2.       teardown()
       public class Fixtures
       {
              int a;
              int b;
             
              @Before
              public void setUp()
              {
                     a = b =3;
              }
      
              @After
              public void tearDown()
              {
                     a = b = 0;
              }
      
              @Test
              public void add()
              {
                     int result = a+b;
                     Assert.assertEquals(6,result);
              }
       }
Test suites:
When bundle test cases runs together those are test suites. In Junit @RunWith and @Sute are used for running the suite tests.
Test Runner:
It is used to execute the tests.
public class FirstSampleJunitRunner
{
       public static void main(String[] args)
       {
              Result result = JUnitCore.runClasses(FirstSampleJunit.class);
              for (Failure failure : result.getFailures())
              {
                     System.out.println(failure.toString());
              }
              System.out.println(result.wasSuccessful());
       }
}
Test classes:
These are the main classes which are used for writing Junit and executing them.
public class FirstSampleJunit
{
        @Test
          public void testAdd() {
             String str= "Junit is working fine";
             assertEquals("Junit is working fine",str);
          }
}

Basic Application

Procedure:
1.       create class program
2.       create test class which is having testing methods
3.       create test runner class
4.       execute test runner class
EX:
1.Create MessageUtils class which contain method for printing the message.
public class MessageUtils
{

       private String message;

       @SuppressWarnings("unused")
       private MessageUtils()
       {
       }

       public MessageUtils(String message) {
              this.message = message;
       }
      
       public String printMessage()
       {
              System.out.println(message);
              return message;
       }
}
2.create the test class for printMessage() method.
public class MessageUtlsTestCase
{
       private static String message = "Welcome Junit";
       private static MessageUtils mes;

       @BeforeClass
       public static void setUpBeforeClass()
       {
              mes = new MessageUtils(message);
       }
      
       @AfterClass
       public static void tearDownAfterClass()
       {
              mes = null;
       }

       @Test
       public void checkprintMethod()
       {
              Assert.assertEquals(message, mes.printMessage());
       }
}
3. create the test runner class
public class MessageutilsTestRunnrer
{

       public static void main(String[] args)
       {
              Result result = JUnitCore.runClasses(MessageUtlsTestCase.class);
              for (Failure failure : result.getFailures())
              {
                     System.out.println(failure.toString());
              }
              System.out.println(result.wasSuccessful());
       }

}













No comments:

Post a Comment