1 HttpUnit
package HttpUnit.com.jerry; import static org.junit.Assert.*; import java.io.IOException; import org.junit.Before; import org.junit.Test; import org.xml.sax.SAXException; import com.meterware.httpunit.GetMethodWebRequest; import com.meterware.httpunit.HttpUnitOptions; import com.meterware.httpunit.PostMethodWebRequest; import com.meterware.httpunit.WebConversation; import com.meterware.httpunit.WebLink; import com.meterware.httpunit.WebRequest; import com.meterware.httpunit.WebResponse; public class WebTest { private WebConversation wc; @Before public void setUp(){ wc = new WebConversation(); } @Test public void testEBusiness() throws IOException, SAXException { HttpUnitOptions.setScriptingEnabled(false); String username = "cindy"; String password = "123456"; WebRequest req = new PostMethodWebRequest("http://127.0.0.1:8000/login_action/"); //给请求加上参数 req.setParameter("username",username); req.setParameter("password",password); //获取响应对象 WebResponse resp = wc.getResponse(req); //用getText方法获取相应的全部内容 assertTrue(resp.getText().contains("购物车")); } @Test public void test3testing() throws IOException, SAXException { HttpUnitOptions.setScriptingEnabled(false); String CheckWord = "测试"; WebResponse req = wc.getResponse("http://www.3testing.com/include/head.htm"); WebLink link = req.getLinkWith("我的课程"); link.click(); WebResponse nextLink = wc.getCurrentPage(); assertTrue(nextLink.getText().contains(CheckWord)); } }
2 DbUnit
AbstractDbUnitTestCase.java
package DbUnit.com.jerry; import static org.junit.Assert.*; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.sql.Connection; import java.sql.DriverManager; import org.dbunit.dataset.IDataSet; import org.dbunit.dataset.ReplacementDataSet; import org.dbunit.dataset.xml.FlatXmlDataSet; import org.dbunit.ext.hsqldb.HsqldbConnection; import org.junit.AfterClass; import org.junit.BeforeClass; public abstract class AbstractDbUnitTestCase { public static final String USER_FIRST_NAME = "Jeffrey"; public static final String USER_LAST_NAME = "Lebowsky"; public static final String USER_USERNAME = "ElDuderino"; protected static UserDaoJdbcImpl dao = new UserDaoJdbcImpl(); protected static Connection connection; protected static HsqldbConnection dbunitConnection; @BeforeClass public static void setupDatabase() throws Exception { String driverName="com.mysql.jdbc.Driver"; String userName="root"; String userPasswd="123456"; String dbName="hr"; String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd; Class.forName(driverName).newInstance(); connection = DriverManager.getConnection(url); dbunitConnection = new HsqldbConnection(connection,null); dao.setConnection(connection); dao.createTables(); } @AfterClass public static void closeDatabase() throws Exception { if ( dbunitConnection != null ) { dao.dropTables(); dbunitConnection.close(); dbunitConnection = null; } } public static IDataSet getDataSet(String name) throws Exception { InputStream inputStream = new FileInputStream(name); assertNotNull("file " + name + " not found in classpath", inputStream ); Reader reader = new InputStreamReader(inputStream); FlatXmlDataSet dataset = new FlatXmlDataSet(reader); return dataset; } public static IDataSet getReplacedDataSet(String name, long id) throws Exception { IDataSet originalDataSet = getDataSet(name); return getReplacedDataSet(originalDataSet, id); } public static IDataSet getReplacedDataSet(IDataSet originalDataSet, long id) throws Exception { ReplacementDataSet replacementDataSet = new ReplacementDataSet(originalDataSet); replacementDataSet.addReplacementObject("[ID]", id); replacementDataSet.addReplacementObject("[NULL]", null); return replacementDataSet; } public static User newUser() { User user = new User(); user.setFirstName(USER_FIRST_NAME); user.setLastName(USER_LAST_NAME); user.setUsername(USER_USERNAME); return user; } public static void assertUser(User user) { assertNotNull(user); assertEquals(USER_FIRST_NAME, user.getFirstName()); assertEquals(USER_LAST_NAME, user.getLastName()); assertEquals(USER_USERNAME, user.getUsername()); } }
UserDaoJdbcImplTest.java
package DbUnit.com.jerry; import static org.junit.Assert.*; import org.dbunit.Assertion; import org.dbunit.dataset.IDataSet; import org.dbunit.operation.DatabaseOperation; import org.junit.Test; public class UserDaoJdbcImplTest extends AbstractDbUnitTestCase{ @Test public void testGetUserById() throws Exception { IDataSet setupDataSet = getDataSet("Test/DbUnit/com/jerry/user.xml"); DatabaseOperation.CLEAN_INSERT.execute(dbunitConnection, setupDataSet); User user = dao.getUserById(1); assertNotNull( user); assertEquals( "Jeffrey", user.getFirstName() ); assertEquals( "Lebowsky", user.getLastName() ); assertEquals( "ElDuderino", user.getUsername() ); } @Test public void testAddUseIgnoringId() throws Exception { IDataSet setupDataSet = getDataSet("Test/DbUnit/com/jerry/user.xml"); DatabaseOperation.DELETE_ALL.execute(dbunitConnection, setupDataSet); User user = newUser(); int id = dao.addUser(user); assertTrue(id>0); IDataSet expectedDataSet = getDataSet("Test/DbUnit/com/jerry/user.xml"); IDataSet actualDataSet = dbunitConnection.createDataSet(); Assertion.assertEqualsIgnoreCols( expectedDataSet, actualDataSet, "users", new String[] { "id" } ); } @Test public void testGetUserByIdReplacingIds() throws Exception { int id = 42; IDataSet setupDataset = getReplacedDataSet("Test/DbUnit/com/jerry/user-token.xml", id ); DatabaseOperation.INSERT.execute(dbunitConnection, setupDataset); User user = dao.getUserById(id); assertUser(user); } @Test public void testAddUserReplacingIds() throws Exception { IDataSet setupDataSet = getDataSet("Test/DbUnit/com/jerry/user-token.xml"); DatabaseOperation.DELETE_ALL.execute(dbunitConnection, setupDataSet); User user = newUser(); int id = dao.addUser(user); assertTrue(id>0); IDataSet expectedDataSet = getReplacedDataSet(setupDataSet, id ); IDataSet actualDataSet = dbunitConnection.createDataSet(); Assertion.assertEquals( expectedDataSet, actualDataSet ); } }
user-token.xml
<?xml version="1.0"?> <dataset> <users id="[ID]" username="ElDuderino" first_name="Jeffrey" last_name="Lebowsky" /> </dataset>
user.xml
<?xml version="1.0"?> <dataset> <users id="1" username="ElDuderino" first_name="Jeffrey" last_name="Lebowsky" /> </dataset>