Junit Project

JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks collectively known as xUnit that originated with JUnit.

This tutorial will teach you how to use JUnit in your day-2-day life of any project unit testing while working with Java programming language.

Audience

This tutorial has been prepared for the beginners to help them understand basic functionality of JUnit tool. After completing this tutorial you will find yourself at a moderate level of expertise in using JUnit testing framework from where you can take yourself to next levels.

Prerequisites

We assume you are going to use JUnit to handle all levels of Java projects development. So it will be good if you have knowledge of software development using any programming language specially Java programming and software testing process.

Testing is the process of checking the functionality of the application whether it is working as per requirements and to ensure that at developer level, unit testing comes into picture. 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.

Unit testing can be done in two ways

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.

What is JUnit ?

JUnit is a unit testing framework for the Java Programming Language. It is important in the test driven development, and is one of a family of unit testing frameworks collectively known as xUnit.

JUnit promotes the idea of "first testing then coding", which emphasis on setting up the test data for a piece of code which can be tested first and then can be implemented . This approach is like "test a little, code a little, test a little, code a little..." which increases programmer productivity and stability of program code that reduces programmer stress and the time spent on debugging.

Features

  • JUnit is an open source framework which is used for writing & running tests.

  • Provides Annotation to identify the test methods.

  • Provides Assertions for testing expected results.

  • Provides Test runners for running tests.

  • JUnit tests allow you to write code faster which increasing quality

  • JUnit is elegantly simple. It is less complex & takes less time.

  • JUnit tests can be run automatically and they check their own results and provide immediate feedback. There's no need to manually comb through a report of test results.

  • 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.

What is a Unit Test Case ?

A Unit Test Case is a part of code which ensures that the another part of code (method) works as expected. To achieve those desired results quickly, test framework is required .JUnit is perfect unit test framework for java programming language.

A formal written unit test case is characterized by a known input and by an expected output, which is worked out before the test is executed. The known input should test a precondition and the expected output should test a postcondition.

There must be at least two unit test cases for each requirement: one positive test and one negative test. If a requirement has sub-requirements, each sub-requirement must have at least two test cases as positive and negative.

 

    
package com.cv.java.junit;

/**
 @author Chandra Vardhan
 
 */
public class Calc {
  
  public int add(int a, int b) {
    return a + b;
  }

  public int sub(int a, int b) {
    return a - b;
  }

  public int mul(int a, int b) {
    return a * b;
  }
}


 

    
package com.cv.java.junit;

import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;

/**
 @author Chandra Vardhan
 
 */
public class CalcTest extends TestCase {
  Calc calc;

  @Before
  public void setUp() throws Exception {
    calc = new Calc();
  }

  @After
  public void tearDown() throws Exception {
    calc = null;
  }

  public void testAdd() {
    assertEquals(2, calc.add(11));
  }

}


 

    
package com.cv.java.junit;

/**
 @author Chandra Vardhan
 
 */
public class Money {

  private int fAmount;
  private String fCurrency;

  public Money(int amount, String currency) {
    fAmount = amount;
    fCurrency = currency;
  }

  public Money add(Money m) {
    Money m1 = null;
    if (m.currency().equals(currency())) {
      m1 = new Money(amount() + m.amount(), currency());

    }
    return m1;
  }

  public int amount() {
    return fAmount;
  }

  public String currency() {
    return fCurrency;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + fAmount;
    result = prime * result + ((fCurrency == null: fCurrency.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    Money other = (Moneyobj;
    if (fAmount != other.fAmount)
      return false;
    if (fCurrency == null) {
      if (other.fCurrency != null)
        return false;
    else if (!fCurrency.equals(other.fCurrency))
      return false;
    return true;
  }

  @Override
  public String toString() {
    return "Money [fAmount=" + fAmount + ", fCurrency=" + fCurrency + "]";
  }
  
  
}


 

    
package com.cv.java.junit;

import junit.framework.TestCase;

/**
 @author Chandra Vardhan
 
 */
public class MoneyTest extends TestCase {

  public void testSimpleAdd() {
    Money m12CHF = new Money(12"CHF")// (1)
    Money m14CHF = new Money(14"CHF");
    Money expected = new Money(26"CHF");
    Money result = m12CHF.add(m14CHF)// (2)
    assertEquals(expected.amount(), result.amount());
    assertEquals(expected, result);// (3)
  }
}

No comments:

Post a Comment