Thursday, April 19, 2012

JUnit Fundamentals






  • JUnit was created as a framework for writing automated, self-verifying tests in Java, which in JUnit are called test cases.
  • JUnit provides a natural grouping mechanism for related tests, which is called a test suite.
test case Vs TestCase class
  • test case generally refers to single test, verifying a specfic path through code.
  • To collect multiple test cases into a single class, itself a subclass of TestCase, with each test case implemented as a method on TestCase.
  • test case class is called fixture.
  • The TestCase class provides a default mechanism for identifying which methods are tests, but U can collect the tests urself in customized suites.
Points to Remember
  • As long as you follow a few simple rules, JUnit will find ur tests and execute them:
  • ur test methods must be instace level, take no parameteras, and return nothing. must declare them as a public void testMethodName().
  • The name of ur test method must start with 'test' all lowercase.
How To write simple JUnit
  • Create a subclass of TestCase
  • To create a test, write a method that expresses the test. Using a JUnit naming conventio that allows JUnit to find and execute ur test automatically.
  • U place the method in a class that extends the JUnit framework class TestCase.
  • To express how U expect the object to behave, you make assertions. (An assertion is simply a statement of ur expectation).
  • TestCase class is the center of the JUnit framework. TestCase in the package junit.framework.
TestSuite
  • A TestSuite is a Composite of Tests. It runs a collection of test cases.
  • A TestSuite can extract the tests to be run automatically. To do so you pass the class of your TestCase class to the TestSuite constructor.
TestCase
  • A test case defines the fixture to run multiple tests. To define a test case
  • implement a subclass of TestCase
  • define instance variables that store the state of the fixture
  • initialize the fixture state by overriding setUp()
  • clean-up after a test by overriding tearDown().
  • Each test runs in its own fixture so there can be no side effects among test runs.
Assertions
  • The class TestCase extends a utility class named Assert in the JUnit framework.
  • The Assert class provides the methods you will use to make assertions about the state of ur objects.
methods of Assert class is
  1. assertTrue(boolen condition)
  2. assertEquals(Object expected,object actual)
  3. assertEquals(int expected,int actual)
  4. assertSame(Object expected,object actual)
  5. assertNull(Object object).
Failure msgs
  • When assertion fails, it is worth including a short msg that indicates the nature of the failure, even a reason for it.
  • Each of the assertion methods accepts as an optional first parameter a String containing a msg to display when the assertion fails.
  • also assertion methods has its own customized failure msgs.
  • when an assertion fails, the assertion method throws an error: an assertionFailedError.
Failure Vs Error
  • JUnit throws an error rather than an expection because in Java, errors are unchecked:
  • When ur test contains a failed assertion, JUnit counts that as a failed test; but when ur test throws an expection(and does not catch it), JUnit counts that as an error.
Test has three basic parts
  1. Create objects.
  2. Invoke some objects.
  3. Check the results.
Note
  • In JUnit, the smallest unit of "test execution" is the test suite. JUnit doesn't actually execute individual tests,
  • but only executes test suites, so in order to execute ur test, U neeed to collect them into a test suite.
  • note that each test runs in its own instance of your test case class.
  • This is one of the fundamental aspects of JUnit's design.
TestResult
  • A TestResult collects the results of executing a test case. It is an instance of the Collecting Parameter pattern. 
  • The test framework distinguishes between failures and errors. 
  • A failure is anticipated and checked for with assertions.
  • Errors are unanticipated problems like an ArrayIndexOutOfBoundsException.
TestFailure
  • A TestFailure collects a failed test together with the caught exception.
How to create testsuite?
  • Create a method on ur test case class called suite(). IT must declared as public static Test suite(), taking no parameters and returning a Test object.
  • In the body of suite method, crete a TestSuite object, and then add Test objects to suite:(either individual tests or other test suites).
  • Return this TestSuite object.
==>manually building a test suite
TestSuite suite=new TestSuite();
suite.addTest(new MoneyTest("testEquals"));
suite.addTest(new MoneyTest("testSimpleAdd"));
==>Passing the TestCase class to TestSuite
return new TestSuite(MoneyTest.class);
  •  JUnit searches testCase class for declared methods that follows the rules.




2 comments:

  1. Hello Santosh,

    I have a few questions wrt Assertions in Selenium Webdriver.
    I Want to verify the following in a webpage(Mozilla Browser)
    a) Existence of a text "Order Summary" in page
    b) Existence of a button "Continue" button and the text "Continue" in the button.

    Can you please help me doing this.
    Thanks a lot in advance.

    Regards
    Prashanth

    ReplyDelete
    Replies
    1. For checking webelement existence use below logic
      http://santoshsarmajv.blogspot.in/2012/07/iselementpresent.html

      -------------------------------------------------------------------

      isTextPresent() method implementation
      if(driver.findElement(By.tagname("body")).getText().contains("Order Summary"))
      {
      return true;
      }
      else
      {
      return false;
      }

      [OR]
      Selenium sel=new WebDriverBackedSelenium(driver, "");
      return sel.isTextPresent(literal);

      Delete

Note: Only a member of this blog may post a comment.