Tuesday, January 25, 2011

Junit Best Practices with detailed examples - Must for every Java Developer

This is second in our tutorial series of junit.
If you wish to retouch junit basics : http://codingbasics.blogspot.com/2011/01/junit-tutorial-1-junit-with-example.html
In this blog, we will touch up some very important best practices for junits.

Unit Test cases need to be independent:

Each unit test should be independent of all other tests.  A unit test should execute one specific behavior for a single method.
Validating behavior of multiple methods is problematic as such coupling can increase refactoring time and effort.

 Consider the following example:

void testAdd(){
     int val1 = myClass.add(1,2);
     int val2 = myclass.add(-1,-2);
     assertTrue (val1 - val2 == 0);
}

If the assertions fails in this case, it will be difficult to determine which invocation of add() caused the problem.

If you want to test a method for multiple behaviors then one test method must be written for each scenario.
This will make the unit test case focus on the particular behavior of the method with great clarity and maintainability.
Please see the self-explanatory following example in testing a method called divide()

public void testNegativeValues()
{
// This method is used to test for negative values scenario.
}

public void testDivideByZero()
{
// The name very well explains what is this test all about.
}

Lets see some more

Junit Tutorial 1 : Junit with an example


Junit is very useful for  unit testing java code.
Let us understand with a simple example that will illustrate the basic concepts involved in testing with JUnit.


The Goal :

Creating a java file named Calculator.java which has a method named sum() which takes two int parameters
and return addition of these two numbers.
So here we have to check whether this method is functioning well in all the conditions or not.

Creating Calculator.java :
This class has sum method which takes two int parameters to add them and return it . Save and compile this file.

public class Calculator{
    int sum(int num1,int num2){
        return num1+num2;
    }
}
Creating CalculatorTest.java :
To test that method sum() is working fine we need to check it.
For this we create another class named CalculatorTest. Save and compile this file.
Before proceeding further we should first have a look over JUnit coding convention.

        Coding Convention :
                1. Name of the test class must end with "Test".
                2. Name of the method must begin with "test".
                3. Return type of a test method must be void.
                4. Test method must not throw any exception.
                5. Test method must not have any parameter.

In our example, the class name which we are going to test is "Calculator" so we have created class "CalculatorTest" here.
Let us write the test case.

Sunday, January 23, 2011

Test Driven Development . Does Your code have GUTs


Lets try to put in simpler sentences on what is Test Driven Development .

  1. Understand the scope of your feature or requirement
  2. Create a test that you know will fail
    • Call a method that doesn't exist or behave correctly yet
    • This is important because you don't want your tests to pass by accident
  3. Make your failed test pass by adding a minimal amount of code
    • Change the code so that it will pass, but don't go overboard
    • Writing minimal code helps enforce not writing code you don't need
  4. Modify your code to make any necessary improvements
    • This is the point at which you can refactor into methods, rename variables, redesign your interfaces, and so on

Characterstics of Good Unit Test ( GUTs)
---------------------------------------------
  • Runs fast ..simple it should run really fast.
  • Keep the unit test  scope simple and clear. If the test fails, it's obvious where to look for the problem. Use few Assert calls so that the offending code is obvious. It's important to only test one thing in a single test.
  • "It works on my machine " syndrome : It should Run and pass in isolation. If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests. Change them for simplicity and reliability. Tests should run and pass on any machine. It
  • Mock and Stub : Often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces.

Qunit- Unit Testing JQuery and JavaScript


QUnit is a powerful JavaScript unit testing framework that helps you to debug code

HOW TO SET-UP THE UNIT TESTS

So what do you need to get started with jQuery Unit Testing? To begin with download both of the following files, Qunit.js, & Qunit.css, and include them in the head of your HTML document along with the latest version of jQuery. Then add the following HTML to the body of your document so that Qunit can render the test results properly:
2
3
4
5
<h1 id="qunit-header">QUnit example</h1>
<h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
As you can see, getting set up with a basic testing environment is easy and pretty simple. With this set-up you can then include your own Qunit tests either in a SCRIPT tag or in your own JavaScript external include.

WRITING YOUR OWN UNIT TESTS

So now you’re ready to start writing your own tests, lets keep it clean and simple. The unit testing API is split into 3 sections:
  1. Setup – The basics for defining tests, grouping them and getting them started.
  2. Assertions – The Yay and the Nay of whether conditions have the met with boolean tests, comparison tests and recoursive comparison assertions for arrays, objects, etc.
  3. Asynchronous Testing – Functions to stop and start the test runner
Read More..

subversion video