Data access object unit test using Mokito framework to follow TDD approach:
Download MockIto from:
https://code.google.com/p/mockito/downloads/detail?name=mockito-1.9.5.zip&can=2&q
UserInfoDao .java:
package com.dao;
import java.sql.SQLException;
import com.model.UserModel;
public interface UserInfoDao {
String registerUser(UserModel userInfo) throws SQLException;
}
Download MockIto from:
https://code.google.com/p/mockito/downloads/detail?name=mockito-1.9.5.zip&can=2&q
UserInfoDao .java:
package com.dao;
import java.sql.SQLException;
import com.model.UserModel;
public interface UserInfoDao {
String registerUser(UserModel userInfo) throws SQLException;
}
UserInfoDaoImpl.java:
package com.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.dao.UserInfoDao;
import com.model.UserModel;
public class UserInfoDaoImpl implements UserInfoDao {
private Connection connection;
public UserInfoDaoImpl(Connection connection) {
super();
this.connection = connection;
}
@Override
public String registerUser(UserModel userInfo) throws SQLException {
System.out.println("dao_registerUser(..) invoked..");
String queryResp = "Failed";
PreparedStatement preStmt = connection
.prepareStatement("insert into user_info(userName,password,firstName,lastName) values (?,?,?,?)");
preStmt.setString(1, userInfo.getUserName());
preStmt.setString(2, userInfo.getPassword());
preStmt.setString(3, userInfo.getFirstName());
preStmt.setString(4, userInfo.getLastName());
if (preStmt.executeUpdate() > 0) {
queryResp = "Success";
}
return queryResp;
}
public Connection getConnection() {
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
}
UserModel.java:
package com.model;
public class UserModel {
private String userName;
private String password;
private String firstName;
private String lastName;
private int id;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "UserModel [userName=" + userName + ", password=" + password
+ ", firstName=" + firstName + ", lastName=" + lastName
+ ", id=" + id + "]";
}
}
UserInfoService.java:
package com.service;
import com.model.UserModel;
public interface UserInfoService {
String registerUser(UserModel userInfo) throws Exception;
}
UserInfoServiceImpl.java:
package com.service.impl;
import com.dao.UserInfoDao;
import com.model.UserModel;
import com.service.UserInfoService;
public class UserInfoServiceImpl implements UserInfoService {
private UserInfoDao userInfoDao;
public UserInfoServiceImpl(UserInfoDao userInfoDao) {
super();
this.userInfoDao = userInfoDao;
}
@Override
public String registerUser(UserModel userInfo) throws Exception {
System.out.println("service_registerUser(..) invoked..");
return getUserInfoDao().registerUser(userInfo);
}
public UserInfoDao getUserInfoDao() {
return userInfoDao;
}
public void setUserInfoDao(UserInfoDao userInfoDao) {
this.userInfoDao = userInfoDao;
}
}
DaoClient:
package com.client;
import java.sql.Connection;
import com.dao.UserInfoDao;
import com.dao.impl.UserInfoDaoImpl;
import com.model.UserModel;
import com.service.UserInfoService;
import com.service.impl.UserInfoServiceImpl;
import com.util.DBMockUtils;
public class DaoClient {
public static void main(String[] args) throws Exception {
UserModel userInfo = new UserModel();
userInfo.setFirstName("Abhinav");
userInfo.setLastName("Mishra");
userInfo.setPassword("admin");
userInfo.setUserName("admin");
Connection conn = DBMockUtils.getConnection("com.mysql.jdbc.Driver",
"jdbc:mysql://localhost:3306/users", "root", "root");
UserInfoDao dao= new UserInfoDaoImpl(conn);
UserInfoService userServ=new UserInfoServiceImpl(dao);
System.out.println(userServ.registerUser(userInfo));
}
}
import java.sql.Connection;
import com.dao.UserInfoDao;
import com.dao.impl.UserInfoDaoImpl;
import com.model.UserModel;
import com.service.UserInfoService;
import com.service.impl.UserInfoServiceImpl;
import com.util.DBMockUtils;
public class DaoClient {
public static void main(String[] args) throws Exception {
UserModel userInfo = new UserModel();
userInfo.setFirstName("Abhinav");
userInfo.setLastName("Mishra");
userInfo.setPassword("admin");
userInfo.setUserName("admin");
Connection conn = DBMockUtils.getConnection("com.mysql.jdbc.Driver",
"jdbc:mysql://localhost:3306/users", "root", "root");
UserInfoDao dao= new UserInfoDaoImpl(conn);
UserInfoService userServ=new UserInfoServiceImpl(dao);
System.out.println(userServ.registerUser(userInfo));
}
}
UserServiceTest : JUnit Test case.
package com.service.test;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import com.dao.UserInfoDao;
import com.model.UserModel;
import com.service.UserInfoService;
import com.service.impl.UserInfoServiceImpl;
public class UserServiceTest extends TestCase {
private UserInfoService userInfoService;
private UserModel userInfo;
@Mock
private UserInfoDao userInfoDao;
@BeforeClass
public static void oneTimeSetUp() {
// one-time initialization code
System.out.println("@BeforeClass - oneTimeSetUp");
}
@AfterClass
public static void oneTimeTearDown() {
// one-time cleanup code
System.out.println("@AfterClass - oneTimeTearDown");
}
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
userInfoService = new UserInfoServiceImpl(userInfoDao);
userInfo = new UserModel();
userInfo.setFirstName("Abhinav");
userInfo.setLastName("Mishra");
userInfo.setPassword("admin");
userInfo.setUserName("admin");
System.out.println("@Before - setUp");
}
@After
public void tearDown() {
System.out.println("@After - tearDown");
}
@Test
public void testUserInfoSuccess() throws Exception{
System.out.println("@Test testUserInfoSuccess()...");
Mockito.when(userInfoDao.registerUser(userInfo)).thenReturn("Success");
final String status = userInfoService.registerUser(userInfo);
System.out.println("Expected Result: " + status);
// Verify state
assertEquals("Success", status);
// Verify that method invoked actually or not
Mockito.verify(userInfoDao).registerUser(userInfo);
// verify the object with no. of times invocation
Mockito.verify(userInfoDao, Mockito.timeout(1)).registerUser(userInfo);
}
@Test
public void testUserInfoFailure() throws Exception {
System.out.println("@Test testUserInfoFailure()...");
Mockito.when(userInfoDao.registerUser(userInfo)).thenReturn("Failed");
final String status = userInfoService.registerUser(userInfo);
System.out.println("Expected Result: " + status);
// Verify state
assertEquals("Failed", status);
// Verify that method invoked actually or not
Mockito.verify(userInfoDao).registerUser(userInfo);
// verify the object with no. of times invocation
Mockito.verify(userInfoDao, Mockito.timeout(1)).registerUser(userInfo);
}
}
Output:
@Before - setUp
@Test testUserInfoSuccess()...
service_registerUser(..) invoked..
Expected Result: Success
@After - tearDown
@Before - setUp
@Test testUserInfoFailure()...
service_registerUser(..) invoked..
Expected Result: Failed
@After - tearDown
No comments:
Post a Comment
Thanks for your comments/Suggestions.