Software Unit Testing 实验指导书
实验内容:
- 1.阅读理解Junit的工作机制。
- 2.Eclipse环境下的单元测试。
- 3.利用Junit编写测试用例,并对被测试类进行相关功能测试。
a)验证Book类的equals方法;
b)设计Triangle类的测试用例,并进行测试。
实现代码:
Book.java
package cn.edu.njupt;
public class Book {
private String title;
private double price;
/**
* Constructor
* @param title
* @param price
*/
public Book(String title,double price){
this.title = title;
this.price = price;
}
public boolean equals(Object object){
if(object instanceof Book){
Book book = (Book) object;
return getTitle().equals(book.getTitle())
&& getPrice() == book.getPrice();
}
return false;
}
public double getPrice(){
return price;
}
public void setPrice(double price){
this.price = price;
}
public String getTitle(){
return title;
}
public void setTitle(String title){
this.title = title;
}
}
BookTest.java
package cn.edu.njupt;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class BookTest {
private Book book1;
private Book book2;
@Before
public void setUp() throws Exception {
book1 = new Book("ES",12.99);
book2 = new Book("The Gate",11.99);
}
@After
public void tearDown() throws Exception {
book1 = null;
book2 = null;
}
@Test
public void testEqualsObject() {
assertFalse(book2.equals(book1));
assertTrue(book1.equals(book1));
}
}
Triangle.java
package cn.edu.njupt;
public class Triangle {
//定义三角形的三边
protected long lborderA =0;
protected long lborderB = 0;
protected long lborderC= 0;
//构造函数
public Triangle(long lborderA, long lborderB, long lborderC){
this.lborderA = lborderA;
this.lborderB= lborderB;
this.lborderC= lborderC;
}
/**
*判断是否是三角形
*是返回ture;不是返回false
**/
public static boolean isTriangle(Triangle triangle){
boolean isTriangle = false;
//判断边界,大于0小于或等于Long.MAX_VALUE,出界返回false
if((triangle.lborderA > 0 && triangle.lborderA <=Long.MAX_VALUE / 2)
&& (triangle.lborderB >0 && triangle.lborderB <=Long.MAX_VALUE/2)
&& (triangle.lborderC >0 && triangle.lborderC <= Long.MAX_VALUE/ 2)){
//判断两边之和大于第三边
if ((triangle.lborderA <(triangle.lborderB + triangle.lborderC))
&& (triangle.lborderB<(triangle.lborderA + triangle.lborderC))
&& (triangle.lborderC <(triangle.lborderA + triangle.lborderB))) {
isTriangle = true;
}
}
return isTriangle;
}
/**
*判断三角形类型
*
*只有两条边相等的三角形返回字符串“等腰三角形”;
*三边均相等的三角形返回字符串“等边三角形”;
*三边均不等的三角形返回字符串“不等边三角形”;
**/
public static String getType(Triangle triangle){
String strType="不是三角形";
//判断是否是三角形
if (isTriangle(triangle)){
//判断是否是等边三角形
if (triangle.lborderA == triangle.lborderB
&& triangle.lborderB == triangle.lborderC){
strType="等边三角形";
}
//判断是否是不等边三角形
else if ((triangle.lborderA != triangle.lborderB)
&& (triangle.lborderB !=triangle.lborderC)
&& (triangle.lborderA != triangle.lborderC)){
strType ="不等边三角形";
}
//三角形既非三边全部相等,又非全部不等,只能是部分相等即等腰三角形
else {
strType ="等腰三角形";
}
}
return strType;
}
}
TriangleTest.java
package cn.edu.njupt;
import static org.junit.Assert.*;
import junit.framework.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TriangleTest extends TestCase{
private Triangle t1;
private Triangle t2;
private Triangle t3;
private Triangle t4;
public TriangleTest(String n){
super(n);
}
@Before
public void setUp() throws Exception {
super.setUp();
t1 = new Triangle(1,3,6);
t2 = new Triangle(3,3,3);
t3 = new Triangle(4,4,2);
t4 = new Triangle(3,4,5);
}
@After
public void tearDown() throws Exception {
super.tearDown();
t1 = null;
t2 = null;
t3 = null;
t4 = null;
}
@Test
public void testGetType() {
assertEquals(Triangle.getType(t1),"不是三角形");
assertEquals(Triangle.getType(t2),"等边三角形");
assertEquals(Triangle.getType(t3),"等腰三角形");
assertEquals(Triangle.getType(t4),"不等边三角形");
}
public void testIsType() {
assertFalse(Triangle.isTriangle(t1));
assertTrue(Triangle.isTriangle(t2));
assertTrue(Triangle.isTriangle(t3));
assertTrue(Triangle.isTriangle(t4));
}
}
google-chrome与driver下载👇:
Chrome与Chromedriver版本对应表(最新)【附下载链接】
小伙伴们,强烈安利一波👇:
推荐一款轻便实用的Chrome插件(限时免费下载,赠去广告特权)
以上属个人见解。
❤️希望对您有帮助,您的支持是我创作最大的动力!
推荐你可能喜欢的其他精彩内容:
CDN是什么?一分钟带你了解CDN
【Linux学习】CentOS6.8安装步骤的详细教程
第十届蓝桥杯大赛软件类省赛C/C++大学B组 题解
Windows10下利用DOSBOX和MASM32搭建汇编语言环境
最新Chromedriver与Chrome版本对应参照表【附下载链接】