谁有Junit测试一个类的例子,给兄弟一个,跟贴有分 (50分)

  • 主题发起人 fuzhimin
  • 开始时间
F

fuzhimin

Unregistered / Unconfirmed
GUEST, unregistred user!
最近要搞项目测试,哪位做过给兄弟点代码,最好测试类和被测试类越简单越好,比如说我测试下面一个类:
//被测试类
public class {
public get_hello(){
return "hello";
}
}
Junit测试工具,这个类的测试类该怎么写,欢迎跟贴,up有分
 
我也来学习.
 
Jb7集成了Junit,可以自动生成部分代码。
 
我也在项目中用jb7环境中的junit,对自己的模块做单元测试。
现在只在用其中的 TestCase,其他还不会用。
目前使用Junit下来的感觉,不仅对自己的代码强度的提高,
还是在与别人做代码集成,都很有好处。
顺便说一句,Junit是在主要在项目的开发过程中使用,
用XP的方法论来说就是,测试优先 和 持续测试。
等编码结束后在来用Junit,我对这种使用方式的效果
表示怀疑。
具体使用方法介绍可以参见:
http://www.5xsoft.com/data/200112/2508112001.htm
 
jb7中TestCase是生成一个测试类
Test Suite 是把一些TestCase放在一起执行。相当于批处理。
JDBC Fixture 是提供JDBC的connections,可以提供预先创建好的数据库连接。
 
JUNIT 学习笔记一 (JUnit 3.7)
--write by delfan
JUnit是一个Open source的自动化测试框架,只要遵循它的框架,就可以把测试的工作变的轻松,偶也是初学,只是记录一些学习中的痕迹罢了.
1. 下载 : http://download.sourceforge.net/junit/junit3.7.zip
2. 安装 : 将junit3.7.zip解压缩.例如 d:/junit3.7
3. 设置CLASSPATH : CLASSPATH = %CLASSPATH%;d:/junit3.7/junit.jar
4. 启动界面 :
[Text UI] java junit.textui.TestRunner
[AWT UI] java junit.awtui.TestRunner
[Swing UI] java junit.swingui.TestRunner
----------------------------------------------------------------------
最简单的例子:
// --- hello.java
public class hello
{
public hello(){}
public String say() {return "hello";
}
}
// --- hellotest.java
import hello;
import junit.framework.TestCase;
import junit.framework.AssertionFailedError;
public class hellotest extends TestCase {
public hellotest(String name)
{
super(name);
}
public static void main(String args[])
{
junit.textui.TestRunner.run(hellotest.class);
}
public void testsay() {
hello world = new hello();
assertTrue( world!=null );
assertEquals("hello", world.say() );
}
}
操作: javac hello.java
javac hellotest.java
java hellotest
返回结果:
.hello
Time: 0.02
OK (1 tests)
修改testhello类中的testsay方法
assertEquals("hello 1", world.say() );
此处修改为错误的数据后进行操作:
javac hellotest.java
java hellotest
返回结果:
.hello
F
Time: 0.02
There was 1 failure:
1) testsay(hellotest)junit.framework.AssertionFailedError: expected:<hello 1> but was:<hello>
at hellotest.testsay(hellotest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at hellotest.main(hellotest.java:17)
FAILURES!!!
Tests run: 1, Failures: 1, Errors: 0
---------------------------------------------------------------------------
与JB5集成 工具一 utest.jar
1.下载JB5插件包 utest.jar并放入JB5的安装目录下的lib/ext,重新启动JB5
2.从代码导航列表中选择要测试的类,右键选择Synchronize test...
3.在出现的对话框中选择要测试的函数,添加到Test methods列表中
4.点 [synchronize] 按钮
5.点 [continue] 按钮
6.此时已经生成测试框架. 修改一下测试用例,编译后选择Run菜单中的Test Project即可得到测试结果.
与JB5集成 工具二 unittest.jar
1.安装与utest一样
2.启动JB后,选择Tools菜单下Editor options.
3.选择UnitTest.进行设置
4.从File菜单中选择New JUnitTest...产生测试代码
5.从Run菜单中选择Run all JunitTests进行测试
注:utest.jar和unittest.jar在http://www.junit.org/news/ide/index.htm上有下载
 
顶部