Monday, February 5, 2018

Junits Example


public class EmployeeService {

@Autowired
private EmployeeDao employeeDao;

public EmployeeService(EmployeeDao employeeDao){
this.employeeDao=employeeDao;
}

public void register(Employee employee) throws Exception {
validateEmployee(employee);
employeeDao.register(employee);
}

public boolean employeeExist(Employee employee) {
return employeeDao.employeeExist(employee);
}

public List<Employee> findAllEmployee() {
return employeeDao.findAllEmployee();
}

public List<Employee> findEmployeeUsingCriteia() {
return employeeDao.findEmployeeUsingCriteia();
}

public void deleteEmployee(int employeeId ) {
employeeDao.deleteEmployee(employeeId);
}

private void validateEmployee(Employee employee) throws Exception{
if(employeeDao.isUserNameExist(employee.getUsername())){
throw new Exception();
}
}



}


package com.bebo.service;

import junit.framework.Assert;

import org.junit.*;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import com.bebo.dao.EmployeeDao;
import com.bebo.model.Employee;

/*
 * @author Anil.Thakur
 */

@RunWith(MockitoJUnitRunner.class)
public class EmployeeServiceTest {
@Mock
private EmployeeDao employeeDao;
private EmployeeService employeeService;
@Before
public void setUp(){
employeeService = new EmployeeService(employeeDao);
Mockito.when(employeeDao.employeeExist(Matchers.any(Employee.class))).thenReturn(true);
}

@Test
public void testEmployeeExist() {
Employee employee =new  Employee();
employee.setAge(25);

boolean result = employeeService.employeeExist(employee);
Assert.assertEquals(true, result);
}


}

Junits Example 1

Calculator.java

public class Calculator {

public static int findMax(int arr[]) {
int max = 0;
for (int i = 1; i < arr.length; i++) {
if (max < arr[i])
max = arr[i];
}
return max;
}

public static int cube(int n) {
return n * n * n;
}

public static int add(int a, int b){
return a+b;
}

public static int divide(int a, int b){
return a/b;
}
}




CalculatorTest.java

public class CalculatorTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("before class");
}

@Before
public void setUp() throws Exception {
System.out.println("before");
}

@Test
public void testFindMax() {
System.out.println("test case find max");
assertEquals(4, Calculator.findMax(new int[] { 1, 3, 4, 2 }));
assertEquals(0, Calculator.findMax(new int[] { -12, -3, -4, -2 }));
}

@Test
public void testCube() {
System.out.println("test case cube");
assertEquals(27, Calculator.cube(3));
}

@After
public void tearDown() throws Exception {
System.out.println("after");
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("after class");
}
}

Sunday, February 4, 2018

Junits




What is Test Driven Development?
  • write test, 
  • write code 
  • Refactor.

JUnit

  • JUnit is an open source Java testing framework used in TDD. 


Why testing?
  • Improve software design
  • Make software easier to understand
  • Reduce debugging time
  • Catch integration errors
  • In short, to Produce Better Code


What should be tested ?

  • Test for boundary conditions
  • Test for both success and failure
  • Test for general functionality
  • Etc..

What is a test case?

  • A test case is a document that describes an input, action, or event and an expected response, to determine if a feature of an application is working correctly 


What is a testing framework?

  • A test framework provides reusable test functionality which is easier to use (e.g. don’t have to write the same code for each class) is standardized and reusable 
  • Provides a base for regression tests


Why use a testing framework?

  • Each class must be tested when it is developed
  • Each class needs a regression test
  • Regression tests need to have standard interfaces
  • Thus, we can build the regression test when building the class and have a better, more stable product for less work


What is jUnit?

  • JUnit is a framework for writing unit tests
  • A unit test is a test of a single class
  • A test case is a single test of a single method
  • A test suite is a collection of test cases
  • Unit testing is particularly important when software requirements change frequently
  • Code often has to be refactored to incorporate the changes
  • Unit testing helps ensure that the refactored code continues to work


JUnit helps the programmer:
  • Define and execute tests and test suites
  • Formalize requirements and clarify architecture
  • Write and debug code
  • Integrate code and always be ready to release a working version

Key Concepts in jUnit:                                           
  • Test interface
  • Assert
  • TestCase
    • assertTrue
    • assertEquals
    • fail
  • TestSuite
    • TestDecorator/TestSetup
  • Failures vs Errors

What JUnit does?
  • JUnit runs a suite of tests and reports results
  • For each test in the test suite:
  • JUnit calls setUp()
  • This method should create any objects you may need for testing
  • JUnit calls one test method
  • The test method may comprise multiple test cases; that is, it may make multiple calls to the method you are testing
  • In fact, since it’s your code, the test method can do anything you want
  • The setUp() method ensures you entered the test method with a virgin set of objects; what you do with them is up to you
  • JUnit calls tearDown()
  • This method should remove any objects you created

Creating a test class in JUnit:
  • Define a subclass of TestCase
  • Override the setUp() method to initialize object(s) under test.
  • Override the tearDown() method to release object(s) under test.
  • Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.
  • Define a static suite() factory method that creates a TestSuite containing all the testXXX() methods of the TestCase.
  • Optionally define a main() method that runs the TestCase in batch mode.

Why to use jUnit?
  • Its free!
  • It is simple and elegant to use.
  • It is easy and inexpensive to write tests using the JUnit testing framework.
  • JUnit tests checks their own result and provide quick visual feedback.
  • Tests can be composed into TestSuites.
  • It is integrated into IDE’s like Eclipse and NetBeans.


Design of j unit:


  • JUnit is designed around two key design patterns:  the Command pattern and  the Composite pattern.
  • A TestCase is a Command object. It can define any number of public testXXX() methods and any test class that has test methods should extend the TestCase class.
  • The test class can use the setUp() and tearDown() methods to initialize and release common objects under test, referred to as the test fixture.
  • TestCase instances can be composed into TestSuites that can automatically invoke all the testXXX() in each TestCase instance. TestSuite can also have other TestSuite instances and this allows the test to run automatically and provide test status.


How to write JUnit Test Case?
To write a test case follows these steps:
Define a subclass of TestCase.
Override the setUp() method to initialize objects under test.
Optionally override tearDown() method to release object under test.
Define one or more public testXXX() methods  that exercise the objects under test.




How to write JUnit Test Suite?


A TestSuite comprises of various TestCase instances.
To write a test suite follow these steps:
Create a class that defines a static suite() factory method which creates TestSuites for all tests.
Optionally define a main() method that runs the TestSuite in batch mode.


Assertions for JUnit:


JUnit uses assertion methods to test conditions:
  • assertEquals(a,b) – a and b must be primitives or have an equals method for comparison
  • assertFalse(a) - a must be boolean
  • assertNotNull(a) - a is either object or null
  • assertNotSame(a,b) – test for object equality
  • assertNull(a) - a is either object or null
  • assertSame(a,b) – test for object equality
  • assertTrue(a) - a must be boolean
  • JUnit uses assertion methods to test conditions:
  • assertEquals(a,b) – a and b must be primitives or have an equals method for comparison
  • assertFalse(a) - a must be boolean
  • assertNotNull(a) - a is either object or null
  • assertNotSame(a,b) – test for object equality
  • assertNull(a) - a is either object or null
  • assertSame(a,b) – test for object equality
  • assertTrue(a) - a must be boolean




JUnit annotations you should understand:


@BeforeClass – Run once before any of the test methods in the class, public static void
@AfterClass – Run once after all the tests in the class have been run, public static void
@Before – Run before @Test, public void
@After – Run after @Test, public void
@Test – This is the test method to run, public void
@ignore
@Test(expected = ArithmeticException.class)
@Test(timeout = 1000)


Mockito
@RunWith(MockitoJUnitRunner.class)
or
@Before
public void init() {
   MockitoAnnotations.initMocks(this);
}


when(mockito.findBookByAuthor("mkyong")).thenReturn(books);


Power Mockito
@RunWith(PowerMockRunner.class)


Mocking Static Method
PowerMockito.mockStatic(SomeSystem.class);


PowerMockito.when(SomeSystem.startServiceStaticWay(service)).thenReturn(1);
Mocking static void Method
PowerMockito.doNothing().when(SomeSystem.class);

Examples:

public class Money {
private int fAmount;
private String fCurrency;

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

public int getAmount() {
return fAmount;
    }

public String getCurrency() { 
return fCurrency;
    }

public Money add(Money m) {
return new Money(getAmount() + m.getAmount(), getCurrency());
}
}





public class MoneyTest extends TestCase {
public void testSimpleAdd() {
         Money m12CHF= new Money(12, "CHF");

         Money m14CHF= new Money(14, "CHF");

         Money expected= new Money(26, "CHF");

         Money result=  m12CHF.add(m14CHF);

         assertEquals(expected.getAmount(), result.getAmount());
         assertEquals(expected.getCurrency(), result.getCurrency());
    }

}

JUnit annotations you should understand:

@BeforeClass – Run once before any of the test methods in the class, public static void
@AfterClass – Run once after all the tests in the class have been run, public static void
@Before – Run before @Test, public void
@After – Run after @Test, public void
@Test – This is the test method to run, public void
@ignore
@Test(expected = ArithmeticException.class)
@Test(timeout = 1000)

Mockito
@RunWith(MockitoJUnitRunner.class)
or 
@Before
public void init() {
    MockitoAnnotations.initMocks(this);
}

when(mockito.findBookByAuthor("mkyong")).thenReturn(books);

Power Mockito
@RunWith(PowerMockRunner.class)


Mocking Static Method
PowerMockito.mockStatic(SomeSystem.class);

PowerMockito.when(SomeSystem.startServiceStaticWay(service)).thenReturn(1);

Mocking static void Method
PowerMockito.doNothing().when(SomeSystem.class);




















package com.bebo.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.bebo.dao.EmployeeDao;
import com.bebo.model.Employee;

/*
 * @author Anil.Thakur
 */
@Service
@Transactional(propagation=Propagation.REQUIRED)
public class EmployeeService {

@Autowired
private EmployeeDao employeeDao;

public void register(Employee employee) throws Exception {
validateEmployee(employee);
employeeDao.register(employee);
}

public boolean employeeExist(Employee employee) {
return employeeDao.employeeExist(employee);
}

public List<Employee> findAllEmployee() {
return employeeDao.findAllEmployee();
}

public List<Employee> findEmployeeUsingCriteia() {
return employeeDao.findEmployeeUsingCriteia();
}

public void deleteEmployee(int employeeId ) {
employeeDao.deleteEmployee(employeeId);
}

private void validateEmployee(Employee employee) throws Exception{
if(employeeDao.isUserNameExist(employee.getUsername())){
throw new Exception();
}
}



}









package com.bebo.service;

import junit.framework.Assert;

import org.junit.*;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.internal.matchers.Any;
import org.mockito.runners.MockitoJUnitRunner;

import com.bebo.dao.EmployeeDao;
import com.bebo.model.Employee;

/*
 * @author Anil.Thakur
 */

@RunWith(MockitoJUnitRunner.class)
public class EmployeeServiceTest {
@Mock
private EmployeeDao employeeDao;
private EmployeeService employeeService;

@Before
public void setUp(){
employeeService = new EmployeeService();
Mockito.when(employeeDao.employeeExist(any(Employee.class))).thenReturn(true);
}


@Test
public void testEmployeeExist() {
Employee employee =new  Employee();
employee.setAge(25);
boolean result = employeeService.employeeExist(employee);
Assert.assertEquals(true, result);
}
}