我知道junit是好东西,最近也在看junit recipes,不过感觉里面框架还很多,比如httpunit,dbunit,servletunit等等,感觉学的有点儿吃力,不知道大家用不用,怎么学的,还有就是有没有看过junit recipe的,里面第十章的开头的例子总感觉看不太懂,有没有理解的,给我说下,谢了,第三,到网上找了半天,想找个好的论坛,但是测试论坛有个叫51test的,看里面的发帖和回帖量少的可怜,不知道有没有什么好的论坛可以推荐下,多谢了!!!
调试欢乐多
junit和dbunit之类的不是同一个framework吧
junit个人认为以测方法为主,不需要什么环境,但是servlet因为要容器支持,没法直接用junit测,所以大概servletunit,httpunit之类的就是mock一个环境后再对方法进行测试这个东西一般的用用不是很复杂吧,看几个例子就会了,无非就是代码运行结果是不是和预期结果相同
其他的东西基本不用,完全可以用sysout来代替,简单方便清楚
Junit只是测试下是否正确,错误的报错,实现目标和sysout一样,但一个junit编译执行后看的累
不如sysout清晰的,而且写这个也麻烦,没必要学的。
岂是sysout能做到的,
更何况,逻辑稍微复杂一点,你得写多少sysout
测试的方法不全是给个参数,看看返回结果是多少对不对这么简单的而且往有效代码里放调试性质的sysout本身就是不推荐的
log4j功能不是比sysout更强?但从来也没听说这个东西还能代替junit功能的我总结一下就是:
1,junit还是有他的价值所在的
2,好多兄弟推崇的sysout,其实是对sysout的滥用,不太正规
看确实很好用,sysout复杂的项目有复杂的用法,并不是不能用,
找到一个错误junit有时候还不如sysout来的快,
除了spring用测试框架其他的基本可以不用的
junit 用于单元测试分TestCase,TestSuite. TestSuite是TestCase集合,合成模式。运行已test开始的方法,
例子:
package IO;import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;public class FileTestCases extends TestCase { public FileTestCases(String methodname) {
super(methodname);
} public static void main(String[] args) {
junit.textui.TestRunner.run(FileTestCases.suite());
} public static Test suite() {
TestSuite suite = new TestSuite("testMethod of I chonse!");
suite.addTest(new FileTestCases("testGirlphoto"));
suite.addTest(new FileTestCases("testPhoto"));
return suite;
} public void testPhoto(){
FileInputStream in=null;
FileOutputStream out=null;
long startTime,elapsedTime;
try
{
startTime=System.nanoTime();
in=new FileInputStream("D:\\girl.jpg");
out=new FileOutputStream("D:\\girlother.jpg");
int content;
while((content=in.read())!=-1){
out.write(content);
}
out.flush();
elapsedTime=System.nanoTime()-startTime;
System.out.println("time pass="+elapsedTime/1000000+"毫秒");//秒
}catch(Exception e){
System.err.println(e);
}
finally{
try
{
if(in!=null) in.close();
if(out!=null) out.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
public void testGirlphoto(){
BufferedInputStream in=null;
BufferedOutputStream out=null;
long startTime,elapsedTime;
try
{
startTime=System.nanoTime();
in=new BufferedInputStream(new FileInputStream("D:\\girl.jpg"));
out=new BufferedOutputStream(new FileOutputStream("D:\\girlother.jpg"));
int content;
while((content=in.read())!=-1){
out.write(content);
}
out.flush();
elapsedTime=System.nanoTime()-startTime;
System.out.println("time pass="+elapsedTime/1000000+"毫秒");//秒
}catch(Exception e){
System.err.println(e);
}
finally{
try
{
if(in!=null) in.close();
if(out!=null) out.close();
}catch(Exception e) {
e.printStackTrace();
}
}
} public void testFile() {
java.net.URL url=FileTestCases.class.getResource("D:\\testFileone.txt");
assertNotNull(url);
}
public void testFileFilter(){
File file=new File(".");
if(file.isDirectory()){
File[] fils=file.listFiles();
for(File f:fils){
System.out.println(f.getName());
}
}
else{
System.out.println(file.getName());
} }
public void testFileInput() throws IOException {
String FileName = "D:\\testFileone.txt";
File file = FileInfo.createFile(FileName);
Writer filewriter = FileInfo.getFileWriter(file, "UTF-8");
try {
for (int i = 0; i < 10; i++) {
filewriter.write("阳光");
}
filewriter.flush();
filewriter.close();
}
catch (IOException e) {
System.err.println(e);
}
//文件内容读取。
Reader in = FileInfo.getFileReader(file, "UTF-8");
try {
char buffer[] = new char[1024]; //字符缓冲区大小。
while (in.read(buffer) != -1) {
String s = String.valueOf(buffer);
System.out.println(s);
}
in.close(); }
catch (IOException e) {
System.err.println(e);
}
}}
用的不多