求助含有Junit测试代码的Java源代码,用来学习Junit下面是我做项目时的例子,希望对你有所帮助 。
/*
*@author ougaoyan ,date:2008-10-19
*/
package test;
import java.util.Date;
import junit.framework.TestCase;
import app.DA.BookDA;
import app.PD.Book;
public class TestBookDA extends TestCase {
public TestBookDA(String name){
super(name);
}
// public Book(int bookID, String cip, String name, String author,String press, String category, int quantity, int reborrowable,int borrowerID,Date startDate)
public void testEditBook(){
Book book1 = new Book(1,"123456","信号","张建","某出版社","电信",1,1,1,new Date());
Book book2 = new Book(-1,"123456","信号","张建","某出版社","电信",1,1,1,new Date());
Book book3 = new Book(99,"123456","信号","张建","某出版社","电信",1,1,1,new Date());
Book book4 = new Book(1,"123456","信号","张建","某出版社","电信",1,1,1,new Date());
assertEquals(true,BookDA.editBook(book1));
assertEquals(false,BookDA.editBook(book2));
assertEquals(true,BookDA.editBook(book3));
assertEquals(false,BookDA.editBook(book4));
}
////Book(int borrowerID, Date startDate, int reBorrowable, String cip)
public void testAddBook(){
Book book1 = new Book(0,"234567","信发号","张建","某出版社","电信",1,1,1,new Date());
Book book2 = new Book(0,"123456","信的号","张建","某出版社","电信",1,1,1,new Date());
Book book3 = new Book(0,"99999","信i号","张建","某出版社","电信",1,1,1,new Date());
assertEquals(true,BookDA.addBook(book1));
assertEquals(true,BookDA.addBook(book2));
assertEquals(false,BookDA.addBook(book3));
}
public static void main(String[] args) {
junit.textui.TestRunner.run(TestBookDA.class);
System.out.println(new TestBookDA("TestBookDA").countTestCases());
}
}
/*
*@author ougaoyan ,date:2008-10-19
*/
package test;
import java.util.Date;
import java.util.Vector;
import junit.framework.TestCase;
import app.DA.CipDA;
import app.PD.Cip;
public class TestCipDAextends TestCase {
public TestCipDA (String name){
super(name);
}
public void testFindBooksByName(){
String name1 = "数据库";
String name2 = "小小";
Vector vector1 = CipDA.findBooksByName(name1);
Vector vector2 = CipDA.findBooksByName(name2);
assertNotNull(vector1);
assertNull(vector2);
}
public void testFindBooksByAuthor(){
String name1 = "欧阳";
String name2 = "小小";
Vector vector1 = CipDA.findBooksByAuthor(name1);
Vector vector2 = CipDA.findBooksByAuthor(name2);
assertNotNull(vector1);
assertNull(vector2);
}
public void testFindBooksByCategory(){
String name1 = "计算机";
String name2 = "计 算 机";
String name3 = "wucimin";
Vector vector1 = CipDA.findBooksByCategory(name1);
Vector vector2 = CipDA.findBooksByCategory(name2);
Vector vector3 = CipDA.findBooksByCategory(name3);
assertNotNull(vector1);
assertNotNull(vector2);
assertNull(vector3);
}
// public void testEditCip(){
// }
//public Cip(String cip, String name, String author, String press,String category, int quantity, int reserverID,Date reservedDate)
public void testAddCip(){
Cip cip1 = new Cip("2244","新加书","新者","出版社","计算机",3,0,new Date());
//Cip cip2 =new Cip(null,"新加书","新者","出版社","计算机",3,0,new Date());
Cip cip3 =new Cip("22er3rr3rt4t43t43t3t34t34t34t44","新加书","新者","出版社","计算机",3,0,new Date());
assertEquals(true,CipDA.addCip(cip1));
//assertEquals(false,CipDA.addCip(cip2));
assertEquals(false,CipDA.addCip(cip3));
}
public void testEditCip(){
Cip cip1 = new Cip("2244","新加书","新者","出版社","计算机",3,0,new Date());
Cip cip2 =new Cip(null,"新加书","新者","出版社","计算机",3,0,new Date());
Cip cip3 =new Cip("22er3rr3rt4t43t43t3t34t34t34t44","新加书","新者","出版社","计算机",3,0,new Date());
assertEquals(true,CipDA.editCip(cip1));
assertEquals(false,CipDA.editCip(cip2));
assertEquals(false,CipDA.editCip(cip3));
}
public static void main(String[] args) {
junit.textui.TestRunner.run(TestCipDA.class);
System.out.println(new TestCipDA("TestCipDA").countTestCases());
}
}
/*
*@author ougaoyan ,date:2008-10-19
*/
package test;
import java.util.Date;
import java.util.Vector;
import junit.framework.TestCase;
import app.DA.LibManagerDA;
import app.PD.LibManager;
import app.PD.Student;
public class TestLibManagerDA extends TestCase {
public TestLibManagerDA(String name){
super(name);
}
public void testCheck(){
String userName1 = "1234";
String password1 = "111111";
String userName2 = "";
String password2= "";
String userName3 = "1234";
String password3 = "111";
LibManager libmanager = new LibManager(1,userName1,password1,"w");
assertEquals(libmanager.getID(), LibManagerDA.check(userName1,password1).getID());
assertEquals(libmanager.getName(), LibManagerDA.check(userName1,password1).getName());
assertEquals(null, LibManagerDA.check(userName2,password2));
assertEquals(null, LibManagerDA.check(userName3,password3));
}
public void testUpdatePwd(){
int id1 =1,id2=99;
String qpwd="234567";
assertEquals(true,LibManagerDA.updatePwd(id1, qpwd));
assertEquals(false,LibManagerDA.updatePwd(id2, qpwd));
}
public void testSearchLibManager(){
LibManager libmanager1 = new LibManager(1,"1234","111111","w");
LibManager libmanager2 = new LibManager(2,"2345","23456","x");
assertEquals(libmanager1.getID(), LibManagerDA.searchLibManager("1234").getID());
assertEquals(libmanager2.getID(), LibManagerDA.searchLibManager("2345").getID());
assertNull(LibManagerDA.searchLibManager(""));
}
public void testQuantity(){
String cip1 = "123456";
String cip2 = "234567";
assertEquals(1,LibManagerDA.getBookQuantity(cip1));
assertEquals(3,LibManagerDA.getBookQuantity(cip2));
assertEquals(-1,LibManagerDA.getBookQuantity(""));
}
public void testEditLibManager(){
LibManager libmanager1 = new LibManager(1,"1234","111111","w");
LibManager libmanager2 = new LibManager(2,"2345","23456","x");
//LibManager libmanager3 = new LibManager(90,"123456677","0","y");
LibManager libmanager4 = new LibManager(3,"123456677","0","e");
assertEquals(true, LibManagerDA.editLibManager(libmanager1));
assertEquals(true,LibManagerDA.editLibManager(libmanager2));
//assertEquals(false,LibManagerDA.editLibManager(libmanager3));
assertEquals(false,LibManagerDA.editLibManager(libmanager4));
}
public void testAddLibManager(){
LibManager libmanager1 = new LibManager(-1,"1234","0","z");
LibManager libmanager2 = new LibManager(-1,"2345","0","x");
LibManager libmanager3 = new LibManager(-1,"12hh3456677","0","y");
assertEquals(true, LibManagerDA.addLibManager(libmanager1));
assertEquals(true,LibManagerDA.addLibManager(libmanager2));
assertEquals(false,LibManagerDA.addLibManager(libmanager3));
}
public void testGetBookCip(){
int bookid1 =1;
int bookid2 =4;
int bookid3 = -1;
String exceptedcip1 = "123456";
String exceptedcip2="234567";
String exceptedcip3 = null;
assertEquals(exceptedcip1, LibManagerDA.getBookCip(bookid1));
assertEquals(exceptedcip2, LibManagerDA.getBookCip(bookid2));
assertEquals(exceptedcip3, LibManagerDA.getBookCip(bookid3));
}
public void testCheckReserved(){
String cip1 = "123456";
String cip2 = "234567";
int studentid1 = 1;
int studentid2 = 2;
int studentid3 = 99;
assertEquals(true, LibManagerDA.checkReserved(cip1,studentid1));
assertEquals(true, LibManagerDA.checkReserved(cip2,studentid2));
assertEquals(false, LibManagerDA.checkReserved(cip1,studentid2));
assertEquals(false,LibManagerDA.checkReserved(cip2,studentid1));
assertEquals(false,LibManagerDA.checkReserved(cip2,studentid3));
assertEquals(false,LibManagerDA.checkReserved(cip1,studentid3));
}
//public static boolean borrowOPeration(int quantity, int bookID, int borrowerID,int reservedNum, int borrowedNum,Date startDate)
public void testBorrowOPeration(){
int quantity1 = 1;
int bookID1 = 2;
int borrowerID1 = 3;
int reservedNum1 = 1;
int borrowedNum1 = 4;
int quantity2 = 1;
int bookID2 = 1;
int borrowerID2 = 3;
int reservedNum2= 0;
int borrowedNum2 = 7;
assertEquals(true,LibManagerDA.borrowOperation(quantity1, bookID1, borrowerID1, reservedNum1, borrowedNum1, new Date()));
assertEquals(false,LibManagerDA.borrowOperation(quantity2, bookID2, borrowerID2, reservedNum2, borrowedNum2, new Date()));
}
//public static boolean returnOperation (int quantity, int bookID, int borrowerID, int borrowedNum, Date startDate, int reborrowable)
public void testReturnOperation(){
int quantity1 = 1;
int bookID1 = 1;
int borrowerID1 = 1;
int borrowedNum1 = 4;
int reborrowable1 = 0;
int quantity2 = 1;
int bookID2 = 2;
int borrowerID2 = 2;
int borrowedNum2 = 7;
int reborrowable2 = 0;
assertEquals(true,LibManagerDA.returnOperation(quantity1, bookID1, borrowerID1, borrowedNum1, new Date(),reborrowable1));
assertEquals(true,LibManagerDA.returnOperation(quantity2, bookID2, borrowerID2, borrowedNum2,new Date(), reborrowable2));
}
public void testGetBookQuantity(){
String cip1 = "123456";
String cip2 = "234567";
int quantity1 = 5;
int quantity2 = 3;
assertEquals(quantity1,LibManagerDA.getBookQuantity(cip1));
assertEquals(quantity2,LibManagerDA.getBookQuantity(cip2));
}
public void testFindBill(){
String userName1 = "111111";
String userName2 = "222222";
float bill1 = 0;
float bill2 = 0;
assertEquals(bill1,LibManagerDA.findBill(userName1),0.000001);
assertEquals(bill2,LibManagerDA.findBill(userName2),0.000001);
}
public void testClearBill(){
String userName1 = "111111";
String userName2 = "222222";
assertEquals(true,LibManagerDA.clearBill(userName1));
assertEquals(true,LibManagerDA.clearBill(userName2));
}
public void testSelectAllLibManager(){
Vector allLibManager = new Vector();
allLibManager = LibManagerDA.selectAllLibManager();
for(int i = 0;i allLibManager.size();i){
LibManager libManager = (LibManager)allLibManager.get(i);
System.out.println(libManager.getName());
}
}
public void testSecelctAllStudent(){
Vector allStudent = new Vector();
allStudent = LibManagerDA.secelctAllStudent();
for(int i = 0;i allStudent.size();i){
Student student = (Student)allStudent.get(i);
System.out.println(student.getName());
}
}
public void testStartTimes(){
Vector times = new Vector();
times = LibManagerDA.startTimes(0);
for(int i= 0;itimes.size();i)
System.out.println((Date)times.get(i));
}
public void testUpdateStuBill() {
}
public static void main(String[] args) {
junit.textui.TestRunner.run(TestLibManagerDA.class);
System.out.println(new TestLibManagerDA("dd ").countTestCases());
}
}
/*
*@author ougaoyan ,date:2008-10-19
*/
package test;
import java.util.Date;
import java.util.Vector;
import junit.framework.TestCase;
import app.DA.StudentDA;
import app.PD.Book;
import app.PD.Cip;
import app.PD.Student;
public class TestStudentDA extends TestCase{
public TestStudentDA(String name){
super(name);
}
/*public Student(int id, int borrowedNum, int reservedNum, float bill,
String userName, String password, String name, String department,
String unit, String sex) */
public void testCheck(){
Student student1 = new Student(1,0,0,0,"111111","123456","张三","软件","0602","男");
//Student student2 = new Student(1,0,0,0,"111111","123","张三","软件","0602","男");
//Student student3 = new Student(2,0,0,0,"11111","123456","张三","软件","0602","男");
assertEquals(student1.getId(),StudentDA.check("111111","123456").getId());
assertEquals(true,student1.getId()==StudentDA.check("111111","123456").getId());
assertEquals(null,StudentDA.check("111111","12356"));
//assertEquals(false,);
//assertEquals(false,);
}
public void testUpdatePwd(){
int id1 = 1;
int id2 = 2;
int id3 = -1;
String password1 = "123456";
String password2 = "234567";
String password3 = "1234555";
assertEquals(true,StudentDA.updatePwd(id1, password1));
assertEquals(true,StudentDA.updatePwd(id2, password2));
assertEquals(false,StudentDA.updatePwd(id3, password3));
}
public void testSearchStudent(){
Student student1 = new Student(1,0,0,0,"111111","123456","张三","软件","0602","男");
assertEquals(true,student1.getId()==StudentDA.searchStudent("111111").getId());
assertEquals(null,StudentDA.searchStudent("111"));
}
public void testEditStudent(){
Student student1 = new Student(1,0,0,0,"111111","123456","张三","软件","0602","女");
Student student2 = new Student(1,0,0,0,"11111333555555555556631","123456","张三","软件","0602","女");
assertEquals(true,StudentDA.editStudent(student1));
assertEquals(false,StudentDA.editStudent(student2));
}
public void testAddStudent(){
Student student1 = new Student(5,0,0,0,"111111","123456","张三","软件","0602","女");
Student student2 = new Student(1,0,0,0,"1111133331","123456","张三","软件","0602","女");
assertEquals(true,StudentDA.addStudent(student1));
//assertEquals(false,StudentDA.addStudent(student2));
}
public static void testGetBorrowBookInfor(){
int studentid1 = 1;
int studentid2 = 2;
int studentid3 = 5;
Vector vector1 = new Vector();
Vector vector2 = new Vector();
Vector vector3 = new Vector();
vector1 = StudentDA.getBorrowBookInfor(studentid1);
vector2 = StudentDA.getBorrowBookInfor(studentid2);
vector3 = StudentDA.getBorrowBookInfor(studentid3);
//System.out.println(vector1);
assertNotNull(((Book)vector1.get(0)).getName());
}
public void testGetReserveBookInfor(){
int studentid1 = 1;
Vector vector1 = new Vector();
vector1 = StudentDA.getReserveBookInfor(studentid1);
assertEquals("数据库",((Cip)vector1.get(0)).getCategory());
}
//public static boolean reserveOperation(int reserverID1, String cip1, int quantity1, int reservedNum1, Date reservedDate1 )
public void testReserveOperation(){
assertEquals(true,StudentDA.reserveOperation(1, "123456", 1, 2, new Date()));
}
//void cancelReservation(int reserverID1,String cip1, int quantity1, int reserveNum1)
public void testCancelReservation(){
assertTrue(StudentDA.cancelReservation(1, "1234567", 1, 3));
}
//public static boolean updateReborrowable(int bookID, int reborrowable1, int studentID,Date startDate1)
public void testUpdateReborrowable(){
assertTrue(StudentDA.updateReborrowable(2,0,1,new Date()));
}
public static void main(String[] args) {
//junit.textui.TestRunner.run(TestStudentDA.class);
//System.out.println(new TestStudentDA("TestStudentDA").countTestCases());
testGetBorrowBookInfor();
}
}
/*
*@author ougaoyan ,date:2008-10-19
*/
package test;
import junit.framework.Test;
import junit.framework.TestSuite;
public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite("Test for test");
suite.addTestSuite(TestLibManagerDA.class);
suite.addTestSuite(TestBookDA.class);
suite.addTestSuite(TestCipDA.class);
suite.addTestSuite(TestStudentDA.class);
//suite.addTestSuite(TestLibManagerDA.class);
//$JUnit-END$
return suite;
}
}
jmeter用java代码怎样编写接口测试源码我们在做性能测试时,有时需要自己编写测试脚本,很多测试工具都支持自定义编写测试脚本,比如LoadRunner就有很多自定义脚本的协议,比如"C Vuser","Java Vuser"等协议.同样,Jmeter也支持自定义编写的测试代码,不过与LoadRunner不同的是,Jmeter没有自带编译器,需要借助第三方编译器才能实现.下面举一个简单的Java自定义测试代码例子,使用Java编译器编写测试代码(Java编译器可以用Eclipse,JBulider等),实现功能为:在测试前输入任意一个字符串,然后判断该字符串的长度是否大于5,如果大于则测试结果成功,否则测试结果位失败,然后在放到Jmeter中模拟10个用户测试,同时运行这段代码,具体实现如下:
1.打开Java编译器,新建一个项目"TestLength",然后新建一个包"app".
2.从Jmeter的安装目录lib/ext中拷贝两个文件"ApacheJMeter_core.jar"和"ApacheJMeter_java.jar"到"Tester"的项目中,然后引入这两个JAR文件.(具体的引入方法参考各个Java编译器的使用方法)
3.在"app"包中新建一个类,名字叫"TestLength",不过这个类要继承"AbstractJavaSamplerClient"类,如果项目引入步骤二中的两个文件,就可以找到"AbstractJavaSamplerClient"类了.
4."TestLength"类在继承"AbstractJavaSamplerClient"类的同时也会继承四个方法,分别是"getDefaultParameters","setupTest","runTest"和"teardownTest"方法."getDefaultParameters"方法主要用于设置传入的参数;"setupTest"方法为初始化方法,用于初始化性能测试时的每个线程."runTest"方法为性能测试时的线程运行体;"teardownTest"方法为测试结束方法,用于结束性能测试中的每个线程.
5.具体实现代码如下:
package app;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import com.passpod.core.t8.*;
/**
* @author乐以忘忧
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class TestLength extends AbstractJavaSamplerClient{
private SampleResult results;
private String testStr;
//初始化方法,实际运行时每个线程仅执行一次,在测试方法运行前执行,类似于LoadRunner中的init方法
public void setupTest(JavaSamplerContext arg0) {
results = new SampleResult();
testStr = arg0.getParameter("testString", "");
if (testStr != nulltestStr.length()0) {
results.setSamplerData(testStr);
}
}
//设置传入的参数 , 可以设置多个,已设置的参数会显示到Jmeter的参数列表中
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
params.addArgument("testStr", "");//定义一个参数,显示到Jmeter的参数列表中,第一个参数为参数默认的显示名称,第二个参数为默认值
return params;
}
//测试执行的循环体,根据线程数和循环次数的不同可执行多次 , 类似于LoadRunner中的Action方法
public SampleResult runTest(JavaSamplerContext arg0) {
int len = 0;
results.sampleStart();//定义一个事务 , 表示这是事务的起始点,类似于LoadRunner的lr.start_transaction
len = testStr.length();
results.sampleEnd();//定义一个事务,表示这是事务的结束点,类似于LoadRunner的lr.end_transaction
if(len5){
System.out.println(testStr);
results.setSuccessful(false);//用于设置运行结果的成功或失败,如果是"false"则表示结果失败,否则则表示成功
}else
results.setSuccessful(true);
return results;
}
//结束方法,实际运行时每个线程仅执行一次,在测试方法运行结束后执行,类似于LoadRunner中的end方法
public void teardownTest(JavaSamplerContext arg0) {
}
}
6.把上面的例子打包,然后把生成的"TestLength.jar"文件拷贝到Jmeter的安装目录lib/ext下.
7.运行Jmeter,添加一个线程组,然后在该线程组下面添加一个Java请求(在Sampler中),在Java请求的类名称中选择咱们刚创建的类"app.TestLength",在下面参数列表的"testStr"后面输入要测试的字符串,然后添加一个监听器(聚合报告),设置一下模拟的用户数就可以测试了.如果测试不成功,Jmeter会在它自己个输出框中抛出这个字符串.
通过上面的例子我们可以发现,使用Jmeter自定义Java测试代码,配合Jmeter自带的函数,就可以实现出LoadRunner中"Java Vuser"协议的绝大多数功能,而且是没有用户数限制和完全免费的(嘿嘿).上面的例子非常简单,而且没有任何实际意义,只是一个简单的Jmeter测试代码示例,用于抛砖引玉,希望大家一起交流,共同 进步.
求Java的在线学习系统源代码Java 程序员必须收藏的资源大全
古董级工具
这些工具伴随着Java一起出现,在各自辉煌之后还在一直使用 。
Apache Ant:基于XML的构建管理工具 。
cglib:字节码生成库 。
GlassFish:应用服务器,由Oracle赞助支持的Java EE参考实现 。
Hudson:持续集成服务器 , 目前仍在活跃开发 。
JavaServer Faces:Mojarra是JSF标准的一个开源实现,由Oracle开发 。
JavaServer Pages:支持自定义标签库的网站通用模板库 。
Liquibase:与具体数据库独立的追踪、管理和应用数据库Scheme变化的工具 。
2.构建工具
构建及应用依赖关系处理工具 。
Apache Maven:Maven是一款声明式构建及依赖管理工具,采用约定优于配置方式进行管理 。相对Apache Ant更推荐使用Maven , 前者采用了过程式管理,维护相对困难 。
Bazel:来自Google的构建工具 , 可以快速、可靠地构建代码 。
Gradle:使用Groovy(非XML)进行增量构建,可以很好地与Maven依赖管理配合工作 。
需要这些学习资料和工具的可以自己下载哦
使用JAVA编写一个测试系统针对java在线测试系统代码你java在线测试系统代码的问题java在线测试系统代码,给java在线测试系统代码你一些建议
1、在数据库设计方面 ,
应针对已给出的问题,进行设计 , 我认为应设计两张表
首先a、登录表,比如命名为
logintbl
id varchar2
username varchar2
password varchar2
答案统计表
answertbl
id varchar2 //这里与登录表中的id也就是学好相对应
anonevarchar2(1)
......
point int
具体的表中字段长度设置你可以自行根据你的要求进行设置
2、在页面设计方面
网上有很多类似的界面可以做参考,唯一需要注意的是
a、在答题时间上,读取的是服务器时间,如果读取的为本地时间那么在刷新后,js代码会重新计算倒计时时间
b、当对已答毕的题目修改时需要点击上一页时,需要读取信息,如果将已答完的结果存入数据库 , 在对其进行修改,这是最简单的方法,但是这样势必将使与数据库交互频率增加,为数据库增添压力,建议将已答完的问题答案 , 保存到一个数组中,每次点击下一页的时候 , 将其存入数组,再点击上一页的时候,可以读取数组中的内容,最后点击保存的时候,整体对数组中的答案进行保存
给java在线测试系统代码了你一点建议,另外这个应该编写不难,所以建议你自行编写
有了java代码,如何测试你这说的太笼统了,应该具体些 。
有了Java代码之后
1、可以在代码中写些测试的方法 , 把你想要验证的东西,输出看一下 。
2、可以用Debug的形式,跟踪代码执行 , 验证程序的逻辑是否正确 。
3、利用JUnit写断言,测试结果
【java在线测试系统代码的简单介绍】关于java在线测试系统代码和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。
推荐阅读
- 苹果CMS不用模板可以吗,苹果cms默认模板
- 关于niushop直播教程的信息
- python函数括号 python函数括号里的
- 苹果cms自检测模板,iphone自检测试
- 公众号加个管理员怎么弄,公众号怎样加管理员
- 容量linux命令 linux磁盘容量
- sap电线的简单介绍
- 如何用鸿蒙系统打开开关,鸿蒙系统怎么开启
- 看头条直播需要什么条件,头条上直播要哪些条件