Test Driven Development in Java

April 12, 2011

Java

«»

 

Examples of Unit Testing Framework

 

 

JUnit

  • Expected results can be tested with the help of assertions
  • During development of the applications itself we can code and test
  • Easily Understandable
  • Provision of a GUI enables the writing and testing of the code more easily and quickly
  • Helps the developer to write and execute repeatable automated tests
  • Eclipse IDE comes with both JUnit and a plug-in for creating and working with JUnit tests
  • Junit today is a de facto standard framework for developing unit tests in Java

Mock Objects

  • Developing around dependencies i.e. code depending on classes that have not yet been developed
  • Mock Objects can be used to simulate the functionality of a production class.
  • Use a mock object framework that allows you to specify the behavior of the dependant class through API.
  • How Mock objects are used
    • mock object instance to be created
    • name of the mock object to be set
    • expected behavior of the mock object to be set
    • Use the mock object as a parameter in the test
    • At the end of the test,verify the mock object was used correctly.
  • The idea behind all Mock Object frameworks is the same.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Eg: EasyMock a framework 
	//instance of Mock Object
	Interface is the name of the un implemented class.All the methods of the class is declared in 
	an interface.
	MockControl control= MockControl.createControl(interface.class);
	//Getting a mock object.i.e an dummy implementation of our interface
	Interfaceimpl mockimpl=(interfacename) control.getMock();
	//Training the mock object .i.e  for a given method if a certain input is passed what should be expected
	Mockimpl.method1(“arguments”);
	Control.setReturnValue(“return value”);
	//using the mock object
	Control.replay();
	//verifying the mock object
	Control.verify();

HttpUnit

  • HttpUnit is a framework based on JUnit, which allows the creation of automated test cases for Web applications.
  • It is suitable for the implementation of automated functional tests, or acceptance tests

Cactus

  • Cactus is a simple test framework that implements an in-container strategy, meaning that tests are executed in the container .
  • Cactus is a framework for unit testing server-side java code (like Servlets, EJBs, Tag Libs, Filters, …).
  • HttpUnit does not do an in container testing.

StrutsTestCase

  • unit testing a part of your Struts application
  • StrutsTestCase extends the JUnit framework to allow testing the Action class.StrutsTestCase is an open source project from SourceForge.
  • It even offers two approaches: one that uses the servlet container and utilizes Cactus framework from Jakarta, and one that simulates the container.

Summary

TDD is an approach to build working software from day one. Enables developers to write reliable & error free code at the same time .It is a technique quickly being adopted by Agile software developers

email

«»

Comments

comments