Mock junit framework uses pre- populated mock java objects instead of live database connection for executing the unit test cases.

Slides:



Advertisements
Similar presentations
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Advertisements

JUnit Tutorial Hong Qing Yu Nov JUnit Tutorial The testing problems The framework of JUnit A case study JUnit tool Practices.
J-Unit Framework.
Testing Object Oriented Programs CSE 111 4/28/20151.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
14-Jul-15 JUnit 4. Comparing JUnit 3 to JUnit 4 All the old assertXXX methods are the same Most things are about equally easy JUnit 4 makes it easier.
Unit Testing Discussion C. Unit Test ● public Method is smallest unit of code ● Input/output transformation ● Test if the method does what it claims ●
Spring-Batch Tutorial Guide for Application Developers.
An Introduction to Hibernate Matt Secoske
Programmer Testing Testing all things Java using JUnit and extensions.
Struts 2.0 an Overview ( )
UNIT-V The MVC architecture and Struts Framework.
Rice KRAD Data Layer JPA Design Eric Westfall July 2013.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
Training - Day 3 OJB. What is OR Mapping? OR Mapping is the mapping of relational database tables to objects (Java Objects in our case) Many OR Mapping.
UPortal Developers MIT August 2004 Persistence Strategy for uPortal 3 Mike DeSimone the r-smart group
Agenda What is Hibernate Spring Integration Questions Overview
Maven for building Java applications By Nalin De Zoysa
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Testing with Android Part I of II. Android Testing Framework Based on JUnit The Android JUnit extensions provide component-specific test case classes.
Enterprise JavaBeans. What is EJB? l An EJB is a specialized, non-visual JavaBean that runs on a server. l EJB technology supports application development.
EJB Framework.  As we know, EJB is the center of the J2EE architecture that provides a sturdy framework for building enterprise applications. The major.
Spring core v3.x Prepared by: Nhan Le. History v3.0 Spring Expression Language Java based bean metadata v3.1 Cache Abstraction Bean Definition Profile.
Standalone Java Application vs. Java Web Application
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Continuous QA Sewit Adams (Colorado State University) Bin Gao (Michigan State University) Jerry Neal (Indiana University)
DEPENDENCY INJECTION & INVERSION OF CONTROL. WHAT’S GOING TO BE COVERED Quick intro to C# for Java developers Dependency Injection Inversion of Control.
Spring Data Access By, Srinivas Reddy.S
CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.
JUnit test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.
A Presentation By V AIBHAV S AHARAN Web-enHanced Information Management COMS E6125.
Tuscany: a SOA framework Jeffrey Guo Accelrys, Inc.
Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Sakai Montreal CRIM Workshop Sakai code exercises Aaron Zeckoski.
JUnit Don Braffitt Updated: 10-Jun-2011.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Testing Spring Applications Unit Testing.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
JUnit A Unit Testing Framework for Java. The Objective Introduce JUnit as a tool for Unit Testing Provide information on how to: Install it Build a test.
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Data Access with Spring.
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
Loader Tutorial. Set Up Mapping Coding Scheme Values to LexGrid Model Creating a Loader – Step by Step Loader Review.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Overview of the Spring Framework Introducing.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
Enterprise JavaBeans: Fundamentals. EJB Fundamentals(c)CDAC(Formerly NCST)2 Contents Introduction Technology Overview EJB Architecture EJB Specification.
Introduction – ORM, Helloworld Application
Fundamentals of MyBATIS
Class Inheritance. The biggest advantage of object oriented design is that these objects can be repeatedly used and redefined as needed. As programmers,
Hibernate Thuy, Le Huu. Pentalog VN. Agenda Hibernate Annotations Improving performance – Lazy loading – Fetching Strategies – Dynamic insert, dynamic.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Development of the EDEX plug-in Ingest overview Manual Endpoint LDM DistributionSrv Plugin decoder Plugin Data Object PersistIndexSrv NotificationSrv.
ATS Application Programming: Java Programming
Unit Testing.
CS520 Web Programming Spring – Inversion of Control
SQL – Application Persistence Design Patterns
Sakai WebApp Structure
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Developing a Model-View-Controller Component for Joomla Part 2
Testing a persistence layer
Developing and testing enterprise Java applications
Joel Adams and Jeremy Frens Calvin College
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
plus content providers, loaders, recyclerview
Aspect Oriented Programming
SQL – Application Persistence Design Patterns
Junit Tests.
Plug-In Architecture Pattern
Presentation transcript:

Mock junit framework uses pre- populated mock java objects instead of live database connection for executing the unit test cases.

The service Layer invokes the DAO layer for getting data from the Database. The DAO pulls data from Database and constructs objects and returns it to Service Objects. Service LayerDAO Layer DataBas e

public interface StockItemCountService { public int getStockItemCount(String warehouseCode); …………………… } public class StockItemCountServiceImpl implements StockItemCountService { private StockItemCountDao stockItemCountDao; } public void setStockItemCountDao(StockItemCountDao stockItemCountDao) { this.stockItemCountDao = stockItemCountDao; public StockCount getStockItemCountData(StockCount data){ return stockItemCountDao.getStockItemCountData(data); Service Interface Service Implementati on This dao is a dependency of the service Object. At runtime spring injects a DAO implementation depending upon the spring XML files.

The following is an excerpt from spring-beans.xml injecting a DAO implementation. <bean id= "stockItemCountDao" class="org.kuali.ext.mm.dataaccess.impl.StockItemCountDaoOjb" /> DAO dependency injection

The service Layer invokes the DAO layer for getting data from the Database. The DAO instead of pulling data from Database it pulls data from property files using Fixture objects. The service layer remains the same in both the cases. only the Service LayerDAO Layer Object Fixtures

All the testcases should be a subclass of org.kuali.ext.mm.sys.context.KualiMockTestBase. public class StockItemCountServiceImplTest extends KualiMockTestBase { private org.kuali.ext.mm.service.StockItemCountService public void setUp() throws Exception { serviceObject = (org.kuali.ext.mm.service.StockItemCountService) SpringContext. getBean(StockItemCountServiceImpl. class); } In the setup() method get the service object to be unit tested from the spring context.

Create a DAO object in test folder using the same package structure of its corresponding DAO in src folder(both the DAOs should implement the same DAO interface). In your testcase prepare the input data object (data object to be passed to the service layer) and output data object (data object that would be returned from the DAO layer if it connects to the database)

If a new DAO or service is to be tested then corresponding entries should be made in mm-dev\src\conf\project\spring-mm- test.xml. <bean id= "stockItemCountDao" class="org.kuali.ext.mm.dataaccess.impl.TestStockItemCountDaoOjb" /> Service entry should be same for test and src TestDAOs are injected instead of Real DAOs

Populate the test DAO cache with the prepared output Data Object. If the service layer invokes multiple DAOs then caches of all the corresponding test DAOs need to be populated with the output data object. Invoke the testcase.

public class xxxDAO implements IxxxDAO{ protected cache Map public void addData(Object data){ cache.add(data.getId(), data); } public void clear(){ cache.clear() } ……… DAO interface methods implementation } Cache contains data object keyed by Id of the object. DAO interface implementations using cache for data instead of database Method for adding data to cache. Common interface for both mock and database DAOs.

public class xxxTestCase extends KualiTestBase{ StockCount inputData = prepareStockCountData(); StockCount outputData = prepareStockCountData(); inputData.setBeforeItemQty( new KualiDecimal("1")); outputData.setStockCountItemQty( new KualiDecimal("1")); org.kuali.ext.mm.dataaccess.impl.TestStockItemCountDaoOjb dao = new org.kuali.ext.mm.dataaccess.impl.TestStockItemCountDaoOjb(); dao.clear(); dao.addStockData(outputData); serviceObject.updateStockItemCountStatus(inputData); StockCount iinputData = serviceObject.getStockItemCountData(inputData); assertEquals("Correct Code", iinputData.getStockTransReasonCd(),TestConstants.STOCK_TRANS_REASON_CODE_GOOD); Input and output data objects are prepared. Invoke the service layer methods with the prepared input data Populate the cache of the DAO with the prepared output data.

There two ways of constructing Data Objects for input and output. Automatically construct the Object using Fixture objects which pull data from property files. Manually creating object and populating it using its constructor or setter methods.

Provide a private method in your testcase for creating the test data object and invoke it for getting the data. Private StockCount prepareStockCount(){ StockCount aStockCount = new StockCount(); aStockCount.setId(hfjdf); ………………… return aStockCount; }

public enum StockItemCountFixture { STOCKITEMCOUNT(1), STOCKITEMCOUNT1(2), STOCKITEMCOUNT2(3), STOCKITEMCOUNT3(4); private int testDataPos; String propertiesFileName = "org/kuali/ext/mm/service/data/stock_item_count_service.properties"; properties.load(ClassLoader.getSystemResourceAsStream(propertiesFileName)); private StockItemCountFixture(int dataPos) { this.testDataPos = dataPos; } public StockCount newStockCount() { String propertyKey = "stockCount.testData" + testDataPos; String deliminator = properties.getProperty("deliminator"); String fieldNames = properties.getProperty("stockCount.fieldNames"); StockCount stockCountData = TestDataPreparator. buildTestDataObject(StockCount. class, properties, propertyKey, fieldNames, deliminator); return stockCountData; }

Advantages of Mock Objects Doesnt need expensive database connections for running the test cases Test case execution will be very fast and easy as there is no database connection. Test cases are independent of changes in database tables. Test cases can be executed anywhere at anytime.

Disadvantages of Mock Objects Since database is not used, we cant implement test cases for database (transaction) and OJB (persistence APIs) layer properties. Requires coding of DAOs with cache of objects. Requires a very good understanding of data model and the service layer.