这是需要测试的类,其中只有两个简单的方法,set与get
package Bug;
public class Report {
protected int ProductId;
public Report() {
}
public void setProductId(int ProductId){
this.ProductId=ProductId;
}
public int getProductId(){
return this.ProductId;
}
}
这是本人对Report类的测试代码
package Bug;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class ReportTest extends TestCase {
private Report a; protected void setUp() throws Exception {
super.setUp();
if (a == null) {
a = new Report();
} } /*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
a = null;
} public static TestSuite suite() {
return new TestSuite(ReportTest.class);
} public static void main(String[] args) {
junit.swingui.TestRunner.run(ReportTest.class);
}
public void testSetProductId() { int ProductId = 2; a.setProductId(ProductId);
} public void testGetProductId() {
int expResult = 2;
int result = a.getProductId();
assertEquals(expResult, result);
}}碰到的问题是我无法在get方法测试中取得我在上面测试set方法时的值,在testGetProuductId方法中的result值总是0,不知道是为什么,set方法已经测试成功,谢谢大虾们指点
package Bug;
public class Report {
protected int ProductId;
public Report() {
}
public void setProductId(int ProductId){
this.ProductId=ProductId;
}
public int getProductId(){
return this.ProductId;
}
}
这是本人对Report类的测试代码
package Bug;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class ReportTest extends TestCase {
private Report a; protected void setUp() throws Exception {
super.setUp();
if (a == null) {
a = new Report();
} } /*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
a = null;
} public static TestSuite suite() {
return new TestSuite(ReportTest.class);
} public static void main(String[] args) {
junit.swingui.TestRunner.run(ReportTest.class);
}
public void testSetProductId() { int ProductId = 2; a.setProductId(ProductId);
} public void testGetProductId() {
int expResult = 2;
int result = a.getProductId();
assertEquals(expResult, result);
}}碰到的问题是我无法在get方法测试中取得我在上面测试set方法时的值,在testGetProuductId方法中的result值总是0,不知道是为什么,set方法已经测试成功,谢谢大虾们指点
解决方案 »
- Java se中,三个按钮切换是否可用,求助
- 一个面试题
- 无法从静态上下文中引用非静态变量 this
- 有关Java类中方法的一个问题
- 用java转换xmlt结果跟用Microsoft.XMLDOM结果不同。而后者是我预期的。不知何故?
- 引用 JAVA 包的问题~~~~~~~~~~~
- 讨论:垃圾回收问题
- 好多不明白之:接口为什麽可以作为方法的参数,它传递的是什麽
- 关于java编译不通过的简单问题!烦死了!
- 我的javah为什么编译jni头文件时候,总是报错
- 关于eclipse的图形界面开发的问题,在线等
- java socket编程 怎么把信息发到 在局域网中的对方 socket 好像只能得到对方公网ip的
至于set因为没有返回值,所以一直是正确的。
setUp
testSetProductId
tearDown
setUp
testGetProductId
tearDown因此在testGetProductId中a.getProductId()取得的是默认值0
可以这样写测试点:
public void testSetProductId() {
int ProductId = 2;
a.setProductId(ProductId);
assertTrue(a.ProductId,2);}