这是需要测试的类,其中只有两个简单的方法,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方法已经测试成功,谢谢大虾们指点
解决方案 »
- 怎么更改默认的JRE版本
- 内部类在主方法中的实例化为什么用的是 .new
- 要看懂以下代码,要看什么书的哪些章节?多谢了!
- 有两个不同时区的Calendar对象,如何计算它们之间的时间差。
- java连接SQL SERVER 2000出错!
- 请问在多线程环境中,如果得到一个线程的真实运行时间??
- 关于java正则表达式的问题!着急
- 关于System.in.read()的问题
- JAVA中的2维数组赋值问题。
- WEB 服务器的市场占有情况,哪位兄弟可以给谈谈!
- 关于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);}