Junit test database with

2010-11-09  来源:本站原创  分类:Java  人气:111 

package com.test.db;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.junit.BeforeClass;
import org.junit.Test;

import com.test.bean.Person;

public class PersonDBTest
{
        private static PersonDB personDB;

        @BeforeClass
        public static void init()
        {
                personDB = new PersonDB();
        }

        @Test
        public void testInsert()
        {
                Person person = new Person();

                person.setUsername("lisi");
                person.setPassword("654321");
                person.setAge(20);

                personDB.insert(person);

                Person person2 = this.getPersonByMaxId();

                this.comparePersons(person, person2);

                personDB.removeById(person2.getId());

        }

        @Test
        public void testUpdate()
        {
                Person person = new Person();

                person.setUsername("zhangsan");
                person.setPassword("langsin");
                person.setAge(40);

                // 1. insert
                personDB.insert(person);

                // 2. get
                Person person2 = this.getPersonByMaxId();

                this.comparePersons(person, person2);

                person2.setUsername("wangwu");
                person2.setPassword("abcdefg");
                person2.setAge(90);

                // 3. update. what we want to test
                personDB.update(person2);

                // 4. get
                Person person3 = this.getPersonByMaxId();

                this.comparePersons(person2, person3);

                personDB.removeById(person3.getId());
        }

        @Test
        public void testGetById()
        {
                Person person = new Person();

                person.setUsername("zhangsan");
                person.setPassword("123456");
                person.setAge(40);

                personDB.insert(person);

                int maxId = this.getMaxId();

                Person person2 = personDB.getById(maxId);

                this.comparePersons(person, person2);

                personDB.removeById(maxId);
        }

        @Test
        public void testRemoveById()
        {
                Person person = new Person();

                person.setUsername("zhangsan");
                person.setPassword("123456");
                person.setAge(40);

                personDB.insert(person);

                int maxId = this.getMaxId();

                personDB.removeById(maxId);

                Person person2 = personDB.getById(maxId);

                assertNull(person2);

        }

        private int getMaxId()
        {
                Connection conn = null;

                int maxId = 0;

                try
                {
                        conn = Conn.getConnection();

                        String sql = "select max(id) as maxId from person";

                        PreparedStatement ps = conn.prepareStatement(sql);

                        ResultSet rs = ps.executeQuery();

                        if (rs.next())
                        {
                                maxId = rs.getInt("maxId");
                        }

                }
                catch (Exception ex)
                {
                        ex.printStackTrace();
                }
                finally
                {
                        try
                        {
                                if (null != conn)
                                {
                                        conn.close();
                                }

                        }
                        catch (Exception ex)
                        {
                                ex.printStackTrace();
                        }
                }

                return maxId;
        }

        private Person getPersonByMaxId()
        {
                Connection conn = null;

                Person person = null;

                try
                {
                        conn = Conn.getConnection();

                        String sql = "select max(id) as maxId from person";

                        PreparedStatement ps = conn.prepareStatement(sql);

                        ResultSet rs = ps.executeQuery();

                        int maxId = 0;

                        if (rs.next())
                        {
                                maxId = rs.getInt("maxId");
                        }

                        String sql2 = "select * from person where id = " + maxId;

                        ps = conn.prepareStatement(sql2);

                        rs = ps.executeQuery();

                        if (rs.next())
                        {
                                person = new Person();

                                person.setId(maxId);
                                person.setUsername(rs.getString("username"));
                                person.setPassword(rs.getString("password"));
                                person.setAge(rs.getInt("age"));
                        }

                }
                catch (Exception ex)
                {
                        ex.printStackTrace();
                }
                finally
                {
                        try
                        {
                                if (null != conn)
                                {
                                        conn.close();
                                }

                        }
                        catch (Exception ex)
                        {
                                ex.printStackTrace();
                        }
                }

                return person;
        }

        // helper method
        private void comparePersons(Person person1, Person person2)
        {
                assertEquals(person1.getUsername(), person2.getUsername());
                assertEquals(person1.getPassword(), person2.getPassword());
                assertEquals(person1.getAge(), person2.getAge());
        }

}
相关文章
  • Junit test database with 2010-11-09

    package com.test.db; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import org.junit.BeforeClass; import org.junit.Test

  • Introduction to JUnit and Unit Testing Introduction 2010-04-18

    1 white-box testing of several related concepts - the test object as an open box, the program's internal logic structure and other information on the test personnel are public. Regression testing - Software or the environment after the repair or corr

  • Introduction to JUnit and unit testing 2010-12-23

    1, several related concepts White-box testing - the test object as an open box, the program logic of the internal structure and other information is open for testers. Regression testing - Software or the environment, repair or correction after the "r

  • Project development process - is reproduced 2010-08-04

    Software process, a software engineer summary Simple design The stability of the current development framework, the specific services designed to delayed, developers can design a simple interface and quick access to specific versions of iteration, th

  • Project development process - reproduced 2010-08-04

    A software engineer software process summary Simple Design The basic stability of the current development framework, the design of specific business delayed, developers can design a simple interface and quick access to specific versions of iterations

  • JUnit Unit Testing Summary 2009-03-01

    Read some of the unit test article, from the article in the interception of a number of information, which could be considered in the learning process, a summary of it! See future! Unit testing code is not used to prove you are right, but in order to

  • Development of my SSH configuration database 2009-04-10

    Recently used at Struts1.x + Spring2.5 + Hibernate3.2 the development of an online examination system, a lot of feel! One of configuration are dependent on at a time when packet is not good hands, because the habit of using IDE, anything can be autom

  • Unit testing tool JUnit 4 2009-04-23

    Reproduced to http://javaee.dahe.cn/bbs/frame.php?frameon=yes&referer=http% 3A / / javaee.dahe.cn / bbs / index.php Unit testing tool JUnit 4 JUnit best practices You intend to put the code on unit testing, what places? Put it and test code mixed tog

  • Learn Ajax, JSP, Servlet, Web connection, database, JAVA, J2EE of good information and good video 2009-05-01

    The following is a list of books: (but some will have to scan them book, http://www.ibeifeng.com/?u=15133 support this website) Lying design patterns JQuery basic tutorial JSP.2.0 technical manuals (high-definition full version) Authoritative guide t

  • JAVA popular open source database 2009-07-15

    Jakarta common: Commons Logging Jakarta Commons Logging (JCL) to provide a log of (Log) interface (interface), both lightweight and is not dependent on the achievement of specific tools log. Available to the middle of it Pieces / log tool for develop

  • jbpm query process, the database, obviously has this process, but could not find one, please help solve the master 2010-03-29

    I have put processes deployed to the database, the database has helloworld in this process, However, when I use this statement to find time this process was found not found, the statement is as follows: private ProcessDefinition processDefinition = c

  • junit (learn from others code) 2010-03-29

    The development process for unit testing, to find out as soon as it will be BUG is the developer supposed to do. JUNIT provided for JAVA developers an excellent test system, the following is a simple JUNIT application examples are also available for

  • Fundamentals - the concept of summing up (spring security.Quartz.JUnit tests, etc.) 2010-03-29

    Interviews may be useful 1, Spring Security framework and the general principles of (1) in the web.xml to configure filters so that you can control this item for each request. (2) In the applicationContext.xml configuration, in which a user http tab

  • Junit 4 2010-03-29

    I. Introduction Junit 4 is the biggest ever Junit framework for improvements to its main objective is to use Java 5 features to simplify the Annotation test preparation. In Eclipse 3.2 has built-in Junit 4.1 (I use the development environment is Ecli

  • java unit testing (JUnit) (change) 2010-04-29

    Class Name: TestGetOneData.java TestGetOneData.java code is as follows: package com.whh.test.test; import static org.junit.Assert. assertEquals; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Sta

  • (Original) Ibatis2 call database stored procedures related to sample 2008-09-17

    The first method, passing in a parameter (non-custom java type), return a single value: oracle code: CREATE OR REPLACE PROCEDURE testPro (bidObjectId NUMBER, projectId OUT NUMBER) AS BEGIN SELECT bo.project_id INTO projectId FROM bm_t_bid_object bo W

  • JUnit experience <reserved> 2010-02-03

    J Original: Andy Schneider Richard Dallaway other compiler: PMT test group -------------------------------------------------- ------------------------------ Translator's Note: Many testers have had the experience of the preparation of testing framewo

  • Database to achieve the level of segmentation theory analysis --- sub-libraries, points table, master and slave, cluster, load balancer 2010-03-19

    Keywords: level of segmentation, sub-libraries, sub-tables, master-slave, cluster Chapter 1 Introduction With the wide spread of Internet applications, massive data storage and access system design has become a bottleneck. For a large-scale Internet

  • Database to achieve the level of segmentation theory analysis (change) 2010-06-03

    http://lishuaibt.javaeye.com/blog/409294 Chapter 1 Introduction <br /> With the wide spread of Internet applications, massive data storage and access system design has become a bottleneck. For a large-scale Internet applications, every day billions

  • Database to achieve the level of segmentation theory analysis 2010-06-04

    Chapter 1 Introduction With the wide spread of Internet applications, massive data storage and access system design has become a bottleneck. For a large-scale Internet applications, every day billions of PV will undoubtedly cause a very high database