java测试代码和源代码,java最简单的测试代码怎么写
java语言写一个文本编辑器的源代码 要能测试通过的!用Eclipse能运行通过的!
一. 高亮的内容:
10多年的浠水网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。网络营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整浠水建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联从事“浠水网站设计”,“浠水网站推广”以来,每个客户项目都认真落实执行。
需要高亮的内容有:
1. 关键字, 如 public, int, true 等.
2. 运算符, 如 +, -, *, /等
3. 数字
4. 高亮字符串, 如 "example of string"
5. 高亮单行注释
6. 高亮多行注释
二. 实现高亮的核心方法:
StyledDocument.setCharacterAttributes(int offset, int length, AttributeSet s, boolean replace)
三. 文本编辑器选择.
Java中提供的多行文本编辑器有: JTextComponent, JTextArea, JTextPane, JEditorPane等, 都可以使用. 但是因为语法着色中文本要使用多种风格的样式, 所以这些文本编辑器的document要使用StyledDocument.
JTextArea使用的是PlainDocument, 此document不能进行多种格式的着色.
JTextPane, JEditorPane使用的是StyledDocument, 默认就可以使用.
为了实现语法着色, 可以继承自DefaultStyledDocument, 设置其为这些文本编辑器的documet, 或者也可以直接使用JTextPane, JEditorPane来做. 为了方便, 这里就直接使用JTextPane了.
四. 何时进行着色.
当文本编辑器中有字符被插入或者删除时, 文本的内容就发生了变化, 这时检查, 进行着色.
为了监视到文本的内容发生了变化, 要给document添加一个DocumentListener监听器, 在他的removeUpdate和insertUpdate中进行着色处理.
而changedUpdate方法在文本的属性例如前景色, 背景色, 字体等风格改变时才会被调用.
@Override
public void changedUpdate(DocumentEvent e) {
}
@Override
public void insertUpdate(DocumentEvent e) {
try {
colouring((StyledDocument) e.getDocument(), e.getOffset(), e.getLength());
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
@Override
public void removeUpdate(DocumentEvent e) {
try {
// 因为删除后光标紧接着影响的单词两边, 所以长度就不需要了
colouring((StyledDocument) e.getDocument(), e.getOffset(), 0);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
五. 着色范围:
pos: 指变化前光标的位置.
len: 指变化的字符数.
例如有关键字public, int
单词"publicint", 在"public"和"int"中插入一个空格后变成"public int", 一个单词变成了两个, 这时对"public" 和 "int"进行着色.
着色范围是public中p的位置和int中t的位置加1, 即是pos前面单词开始的下标和pos+len开始单词结束的下标. 所以上例中要着色的范围是"public int".
提供了方法indexOfWordStart来取得pos前单词开始的下标, 方法indexOfWordEnd来取得pos后单词结束的下标.
public int indexOfWordStart(Document doc, int pos) throws BadLocationException {
// 从pos开始向前找到第一个非单词字符.
for (; pos 0 isWordCharacter(doc, pos - 1); --pos);
return pos;
}
public int indexOfWordEnd(Document doc, int pos) throws BadLocationException {
// 从pos开始向前找到第一个非单词字符.
for (; isWordCharacter(doc, pos); ++pos);
return pos;
}
一个字符是单词的有效字符: 是字母, 数字, 下划线.
public boolean isWordCharacter(Document doc, int pos) throws BadLocationException {
char ch = getCharAt(doc, pos); // 取得在文档中pos位置处的字符
if (Character.isLetter(ch) || Character.isDigit(ch) || ch == '_')
return false;
}
所以着色的范围是[start, end] :
int start = indexOfWordStart(doc, pos);
int end = indexOfWordEnd(doc, pos + len);
六. 关键字着色.
从着色范围的开始下标起进行判断, 如果是以字母开或者下划线开头, 则说明是单词, 那么先取得这个单词, 如果这个单词是关键字, 就进行关键字着色, 如果不是, 就进行普通的着色. 着色完这个单词后, 继续后面的着色处理. 已经着色过的字符, 就不再进行着色了.
public void colouring(StyledDocument doc, int pos, int len) throws BadLocationException {
// 取得插入或者删除后影响到的单词.
// 例如"public"在b后插入一个空格, 就变成了:"pub lic", 这时就有两个单词要处理:"pub"和"lic"
// 这时要取得的范围是pub中p前面的位置和lic中c后面的位置
int start = indexOfWordStart(doc, pos);
int end = indexOfWordEnd(doc, pos + len);
char ch;
while (start end) {
ch = getCharAt(doc, start);
if (Character.isLetter(ch) || ch == '_') {
// 如果是以字母或者下划线开头, 说明是单词
// pos为处理后的最后一个下标
start = colouringWord(doc, start);
} else {
//SwingUtilities.invokeLater(new ColouringTask(doc, pos, wordEnd - pos, normalStyle));
++start;
}
}
}
public int colouringWord(StyledDocument doc, int pos) throws BadLocationException {
int wordEnd = indexOfWordEnd(doc, pos);
String word = doc.getText(pos, wordEnd - pos); // 要进行着色的单词
if (keywords.contains(word)) {
// 如果是关键字, 就进行关键字的着色, 否则使用普通的着色.
// 这里有一点要注意, 在insertUpdate和removeUpdate的方法调用的过程中, 不能修改doc的属性.
// 但我们又要达到能够修改doc的属性, 所以把此任务放到这个方法的外面去执行.
// 实现这一目的, 可以使用新线程, 但放到swing的事件队列里去处理更轻便一点.
SwingUtilities.invokeLater(new ColouringTask(doc, pos, wordEnd - pos, keywordStyle));
} else {
SwingUtilities.invokeLater(new ColouringTask(doc, pos, wordEnd - pos, normalStyle));
}
return wordEnd;
}
因为在insertUpdate和removeUpdate方法中不能修改document的属性, 所以着色的任务放到这两个方法外面, 所以使用了SwingUtilities.invokeLater来实现.
private class ColouringTask implements Runnable {
private StyledDocument doc;
private Style style;
private int pos;
private int len;
public ColouringTask(StyledDocument doc, int pos, int len, Style style) {
this.doc = doc;
this.pos = pos;
this.len = len;
this.style = style;
}
public void run() {
try {
// 这里就是对字符进行着色
doc.setCharacterAttributes(pos, len, style, true);
} catch (Exception e) {}
}
}
七: 源码
关键字着色的完成代码如下, 可以直接编译运行. 对于数字, 运算符, 字符串等的着色处理在以后的教程中会继续进行详解.
import java.awt.Color;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class HighlightKeywordsDemo {
public static void main(String[] args) {
JFrame frame = new JFrame();
JTextPane editor = new JTextPane();
editor.getDocument().addDocumentListener(new SyntaxHighlighter(editor));
frame.getContentPane().add(editor);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
/**
* 当文本输入区的有字符插入或者删除时, 进行高亮.
*
* 要进行语法高亮, 文本输入组件的document要是styled document才行. 所以不要用JTextArea. 可以使用JTextPane.
*
* @author Biao
*
*/
class SyntaxHighlighter implements DocumentListener {
private SetString keywords;
private Style keywordStyle;
private Style normalStyle;
public SyntaxHighlighter(JTextPane editor) {
// 准备着色使用的样式
keywordStyle = ((StyledDocument) editor.getDocument()).addStyle("Keyword_Style", null);
normalStyle = ((StyledDocument) editor.getDocument()).addStyle("Keyword_Style", null);
StyleConstants.setForeground(keywordStyle, Color.RED);
StyleConstants.setForeground(normalStyle, Color.BLACK);
// 准备关键字
keywords = new HashSetString();
keywords.add("public");
keywords.add("protected");
keywords.add("private");
keywords.add("_int9");
keywords.add("float");
keywords.add("double");
}
public void colouring(StyledDocument doc, int pos, int len) throws BadLocationException {
// 取得插入或者删除后影响到的单词.
// 例如"public"在b后插入一个空格, 就变成了:"pub lic", 这时就有两个单词要处理:"pub"和"lic"
// 这时要取得的范围是pub中p前面的位置和lic中c后面的位置
int start = indexOfWordStart(doc, pos);
int end = indexOfWordEnd(doc, pos + len);
char ch;
while (start end) {
ch = getCharAt(doc, start);
if (Character.isLetter(ch) || ch == '_') {
// 如果是以字母或者下划线开头, 说明是单词
// pos为处理后的最后一个下标
start = colouringWord(doc, start);
} else {
SwingUtilities.invokeLater(new ColouringTask(doc, start, 1, normalStyle));
++start;
}
}
}
/**
* 对单词进行着色, 并返回单词结束的下标.
*
* @param doc
* @param pos
* @return
* @throws BadLocationException
*/
public int colouringWord(StyledDocument doc, int pos) throws BadLocationException {
int wordEnd = indexOfWordEnd(doc, pos);
String word = doc.getText(pos, wordEnd - pos);
if (keywords.contains(word)) {
// 如果是关键字, 就进行关键字的着色, 否则使用普通的着色.
// 这里有一点要注意, 在insertUpdate和removeUpdate的方法调用的过程中, 不能修改doc的属性.
// 但我们又要达到能够修改doc的属性, 所以把此任务放到这个方法的外面去执行.
// 实现这一目的, 可以使用新线程, 但放到swing的事件队列里去处理更轻便一点.
SwingUtilities.invokeLater(new ColouringTask(doc, pos, wordEnd - pos, keywordStyle));
} else {
SwingUtilities.invokeLater(new ColouringTask(doc, pos, wordEnd - pos, normalStyle));
}
return wordEnd;
}
/**
* 取得在文档中下标在pos处的字符.
*
* 如果pos为doc.getLength(), 返回的是一个文档的结束符, 不会抛出异常. 如果pos0, 则会抛出异常.
* 所以pos的有效值是[0, doc.getLength()]
*
* @param doc
* @param pos
* @return
* @throws BadLocationException
*/
public char getCharAt(Document doc, int pos) throws BadLocationException {
return doc.getText(pos, 1).charAt(0);
}
/**
* 取得下标为pos时, 它所在的单词开始的下标. ±wor^d± (^表示pos, ±表示开始或结束的下标)
*
* @param doc
* @param pos
* @return
* @throws BadLocationException
*/
public int indexOfWordStart(Document doc, int pos) throws BadLocationException {
// 从pos开始向前找到第一个非单词字符.
for (; pos 0 isWordCharacter(doc, pos - 1); --pos);
return pos;
}
/**
* 取得下标为pos时, 它所在的单词结束的下标. ±wor^d± (^表示pos, ±表示开始或结束的下标)
*
* @param doc
* @param pos
* @return
* @throws BadLocationException
*/
public int indexOfWordEnd(Document doc, int pos) throws BadLocationException {
// 从pos开始向前找到第一个非单词字符.
for (; isWordCharacter(doc, pos); ++pos);
return pos;
}
/**
* 如果一个字符是字母, 数字, 下划线, 则返回true.
*
* @param doc
* @param pos
* @return
* @throws BadLocationException
*/
public boolean isWordCharacter(Document doc, int pos) throws BadLocationException {
char ch = getCharAt(doc, pos);
if (Character.isLetter(ch) || Character.isDigit(ch) || ch == '_')
return false;
}
@Override
public void changedUpdate(DocumentEvent e) {
}
@Override
public void insertUpdate(DocumentEvent e) {
try {
colouring((StyledDocument) e.getDocument(), e.getOffset(), e.getLength());
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
@Override
public void removeUpdate(DocumentEvent e) {
try {
// 因为删除后光标紧接着影响的单词两边, 所以长度就不需要了
colouring((StyledDocument) e.getDocument(), e.getOffset(), 0);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
/**
* 完成着色任务
*
* @author Biao
*
*/
private class ColouringTask implements Runnable {
private StyledDocument doc;
private Style style;
private int pos;
private int len;
public ColouringTask(StyledDocument doc, int pos, int len, Style style) {
this.doc = doc;
this.pos = pos;
this.len = len;
this.style = style;
}
public void run() {
try {
// 这里就是对字符进行着色
doc.setCharacterAttributes(pos, len, style, true);
} catch (Exception e) {}
}
}
}
五个方法让你做更好的java单元测试?
单元测试是我们在软件开发过程中经常用到的一种软件测试的方法,而今天我们就一起来了解一下,一个好的单元测试都是如何来编辑完成的。
1.使用框架来用于单元测试
Java提供了若干用于单元测试的框架。TestNG和JUnit是流行的测试框架。JUnit和TestNG的一些重要功能:
易于设置和运行。
支持注释。
允许忽略或分组并一起执行某些测试。
支持参数化测试,即通过在运行时指定不同的值来运行单元测试。
通过与构建工具,如Ant,Maven和Gradle集成来支持自动化的测试执行。
EasyMock是一个模拟框架,是单元测试框架,如JUnit和TestNG的补充。EasyMock本身不是一个完整的框架。它只是添加了创建模拟对象以便于测试的能力。例如,我们想要测试的一个方法可以调用从数据库获取数据的DAO类。在这种情况下,EasyMock可用于创建返回硬编码数据的MockDAO。这使我们能够轻松地测试我们意向的方法,而不必担心数据库访问。
2.谨慎使用测试驱动开发!
测试驱动开发(TDD)是一个软件开发过程,在这过程中,在开始任何编码之前,我们基于需求来编写测试。由于还没有编码,测试初会失败。然后写入小量的代码以通过测试。然后重构代码,直到被优化。
目标是编写覆盖所有需求的测试,而不是一开始就写代码,却可能甚至都不能满足需求。TDD是伟大的,因为它导致简单的模块化代码,且易于维护。总体开发速度加快,容易发现缺陷。此外,单元测试被创建作为TDD方法的副产品。
然而,TDD可能不适合所有的情况。在设计复杂的项目中,专注于简单的设计以便于通过测试用例,而不提前思考可能会导致巨大的代码更改。此外,TDD方法难以用于与遗留系统,GUI应用程序或与数据库一起工作的应用程序交互的系统。另外,测试需要随着代码的改变而更新。
因此,在决定采用TDD方法之前,应考虑上述因素,并应根据项目的性质采取措施。
3.测量代码覆盖率
代码覆盖率衡量(以百分比表示)了在运行单元测试时执行的代码量。通常,高覆盖率的代码包含未检测到的错误的几率要低,因为其更多的源代码在测试过程中被执行。云南电脑培训发现测量代码覆盖率的一些佳做法包括:
使用代码覆盖工具,如Clover,Corbetura,JaCoCo或Sonar。使用工具可以提高测试质量,因为这些工具可以指出未经测试的代码区域,让你能够开发开发额外的测试来覆盖这些领域。
求助含有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 TestCipDA extends 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;i times.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;
}
}
JAVA中代码和源代码的区别?
源代码和代码不一样的,一般代码有编译过的和没编译的,相对于已编译过的代码,未编译的代码就是源代码,比如Java编译过的代码Class文件,用记事本打开它也是一些代码,不过我们看不懂而已。
有哪些方法可以进行Java单元测试
在软件开发过程中,每个单元的运行都是非常关键的,并且直接关系到后期程序员的运行。那么在进行软件开发过程中,经常使用到的单元测试方法有哪些呢,一个好的单元测试是如何进行实现的?下面云南电脑培训为大家介绍进行Java单元测试的具体方法。
1、使用框架进行单元测试
Java能够提供单元测试方法的框架,在测试过程中,测试NG和JUnit是现在流行的测试框架。JUnit和TestNG框架测试有几个重要功能:设置和运行很容易;允许忽略或分组,并一起运行多个测试;支持参数化测试,并且云南IT培训发现能够通过在运行时指定不同的值来执行单元测试。
2、谨慎使用测试驱动开发
测试驱动开发是一个软件开发的过程。在整个开发过程中,在开始编码的时候,应该根据程序的需求进行编程测试。但是昆明IT培训发现由于这个时候还没有进行编程,所以初次测试会面临失败,只需要写入少量的代码就能通过测试,进行重置代码。
3、测试代码的覆盖率
代码覆盖率是以百分比测定执行单元测试时进行的代码量。通常,高覆盖率的代码包含未被检测出的错误的概率较低,因为更多的源代码在测试中被执行。测试代码覆盖率的工具有:Clover,Corbetura,JaCoCo。使用工具测试能够更好的提高测试质量。
4、将测试数据外部优化
在JUnit4之前,测试用例执行的数据必须被测试用例硬编码,这会引起限制。为了使用不同的数据执行测试,必须修正测试用例代码。但是,昆明电脑培训认为JUnit4以及TestNG支持外部化测试数据,无需变更源代码,就可以对不同的数据组执行测试用例。
JAVA中: 编译过的代码和源代码的区别是?
源代码就是你在电脑里手敲进去的那些;
编译过的代码其实是你的编译软件将你手敲进去的那些代码“翻译”成了计算机可以识别读懂的机器语言,换句话说也就是一堆二进制的代码。
你写程序的时候会用到某种软件(比如java的eclipse,.NET的vs2005、vs2008等),当你要编译时只需点下软件的编译按钮,软件会自动给你编译。
当前题目:java测试代码和源代码,java最简单的测试代码怎么写
网页链接:http://ybzwz.com/article/hoggos.html