ST Lab1 junit test
代码地址: https://github.com/newff/st-lab1
Tasks:
- Install Junit(4.12), Hamcrest(1.3) with Eclipse
- Install Eclemma with Eclipse
- Write a java program for the triangle problem and test the program with Junit.
a) Description of triangle problem:
Function triangle takes three integers a,b,c which are length of triangle sides; calculates whether the triangle is equilateral(等边), isosceles(等腰), or scalene(斜角体).
1. Install Junit(4.12), Hamcrest(1.3) with Eclipse
To install jar package into Eclipse, we can use Maven or download the jar package the add to Eclipse. In my earlier blogs I have telled how to add jar package through Maven, in this passage I tell how to download and add directly.
Online websites:
Junit: http://junit.org/junit4/
hamcrest: http://search.maven.org/#search|ga|1|g%3Aorg.hamcrest


hamcrest-all.* is recommended.
Choose the proper version and download it.
Open Eclipse, file->new->Java Project, name it "javaPro", it finished to new a new project. Then, on your "javaPro", right click->properties->Java Build Path->Libraries->Add External JARs, select the directory of your jar packages. click Apply and OK. You can see the two packages in your Referenced Library.


2. Install Eclemma with Eclipse
website: http://www.eclemma.org/installation.html#marketplace
There are three options for us to install the Eclemma on this website, you can choose anyone of them. I chose the first one and then restart my eclipse, it was already installed.
In your "javaPro", help->Eclipse MarketPlace, search "Eclemma ",and then install. It finished to install the Eclemma.

3.Write a java program for the triangle problem and test the program with Junit.
The java program is on the github. To test the program with junit, we have to right click the "Triangle.java", and then new->Junit Test Case,
Browse the functions that we want to test, then finish.


Write the test function for the Triangle function, then, add some test cases, click the button to run and we will see the result.

4.Result of the test
When I test all of the four conditions, the coverage is 100%.


One error I have made in my lab is that when I judge whether it is a riangle, I wrote this"if((a+b>c && a-b<c) || (a+c>b && a-c<b) || (b+c>a && b-c<a))",this program get the wrong answer. Then I fixed the error and the program become right.

5.Prolongation
What's more, I tried to use Parameters in my junit test.
These are my codes:
package javaPro; import static org.junit.Assert.*; import java.util.Arrays;
import java.util.Collection; import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class)
public class TriangleTest {
private int a;
private int b;
private int c;
private int expected;
private Triangle triangle; public TriangleTest(int a, int b, int c, int expected){
this.a = a;
this.b = b;
this.c = c;
this.expected = expected;
}
@Before
public void setUp() throws Exception {
System.out.println("Before test");
triangle = new Triangle();
} @Parameters
public static Collection<Object[]>getData(){
return Arrays.asList(new Object[][]{
{1, 2, 3, 3},
{1, 1, 1, 0},
{2, 2, 3, 1},
{3, 4, 5, 2}
});
}
@Test
public void testTriangles() {
assertEquals (this.expected, triangle.triangles(a, b, c));
// assertEquals (0, triangle.triangles (1,1,1));
// assertEquals (1, triangle.triangles (2,2,3));
// assertEquals (2, triangle.triangles (3,4,5));
} }
And this is the result, I have also put my code on the Github.

I made a mistake that the name of the constructed function must be the same with the name of the class. It worked after I have fixed this mistake.

ST Lab1 junit test的更多相关文章
- 软件测试:lab1.Junit and Eclemma
软件测试:lab1.Junit and Eclemma Task: Install Junit(4.12), Hamcrest(1.3) with Eclipse Install Eclemma wi ...
- Software Testing, Lab 1
1.Install Junit(4.12), Hamcrest(1.3) with Eclipse 2.Install Eclemma with Eclipse 3.Write a java prog ...
- ST第三次作业Junit安装
一.Junit安装 安装eclipse,右键“项目”文件——>java build path——>导入jar包junit-4.12.jar和hamcrest-all-1.3.jar包. 二 ...
- ST 单元测试之maven引入junit包
按照上篇博客,已经完成了mavne以及eclipse的安装配置,新建好了一个maven项目. 接下来打开项目,双击打开pom.xml,可以看到如下所示, 点击下方的pom.xml,然后添加如下代码,即 ...
- 【原创】Junit4详解一:Junit总体介绍
Junit是一个可编写重复测试的简单框架,是基于Xunit架构的单元测试框架的实例.Junit4最大的改进是大量使用注解(元数据),很多实际执行过程都在Junit的后台做完了,而且写test case ...
- JavaSE学习总结(十九)—— Java Web 综合应用(JSP、Servlet、IDEA、MySQL、JUnit、AJAX、JSON)
一.使用Servlet+JDBC+MySQL+IDEA实现商品管理 1.1.创建项目 打开IntelliJ IDEA开发工具.点击Create New Project创建一个新的Web项目 选择Jav ...
- Junit参数化测试Spring应用Dubbo接口
一.创建基础类. package com.tree.autotest; import org.junit.Before;import org.springframework.context.annot ...
- 软件测试技术第一次试验之——JUnit的安装与使用
众所周知,在一个大型的软件项目中,测试是必不可少的.传统的测试方法往往要自己编写测试函数再结合测试用例进行验证,这样会显得比较繁琐.所以我们可以使用JUnit框架进行测试. 使用junit的好处就是这 ...
- 软件测试入门 1—— junit 单元测试
一.实验主要内容: 1. 2.EclEmma安装 见: http://www.cnblogs.com/1995hxt/p/5291465.html 二.对与 Junit 安装,使用 maven管理项目 ...
随机推荐
- 利用apache的mod_rewrite做URL规则重写
使用mod_rewrite做url重写,伪静态,做过很多次,这次用几个例子记下来,便于后面查用. 使用方法: 1.在conf目录的httpd.conf文件中找到: LoadModule rewrite ...
- Struct 和 Union 的详细区别
Union: 共用体 Struct:结构体 两者的区别: 1:共用体和结构体都是由多个不同的数据类型成员组成, 但在任何同一时刻, 共用体只存放一个被选中的成员, 而结构体则存放所有的成员变量. 2: ...
- C++的输入和输出
C++是一种常用的编程语言.一个完整的程序至少要有一个输出,而我们也经常需要在程序内进行大量输入和输出.所以今天,我和大家谈一谈输入和输出. 1.cin和cout.可以连续输入,使用流(>> ...
- 论SNAPSHOT包的危害性
先介绍一下背景:我们应用是一个标准的spring+webx工程,博主在一次项目发布前为了再次测试一下自己的代码,将分支部署到日常环境中,但是项目启动的时候报错: 第一眼看到这个堆栈后有点懵逼 第一是上 ...
- 康复计划#1 再探后缀自动机&后缀树
本篇口胡写给我自己这样的东西都忘光的残废选手 以及那些刚学SAM,看了其他的一些东西并且没有完全懵逼的人 (初学者还是先去看有图的教程吧,虽然我的口胡没那么好懂,但是我觉得一些细节还是讲清楚了的) 大 ...
- ODBC
ODBC是80年代末90年代初出现的技术,它为编写关系数据库的客户软件提供了统一的接口.ODBC只提供单一的API,可用于处理不同数据库的客户应用程序.使用ODBC API的应用程序可以与任何具有OD ...
- static的加载先后顺序
1.静态变量的声明和赋值是分开的,静态变量会先被声明,赋值操做被放在了静态代码块中. 2.静态变量的赋值和静态代码块的执行顺序和代码的先后书写顺序相关. 3.静态代码块优先执行,其次构造方法,最后普通 ...
- Java 文件句柄泄露问题解决小记
维护 WebIDE 免不了要管理很多的文件, 自从我们线上系统增加了资源回收功能,便一直受一个问题困扰:后台线程解绑目录时偶尔报错,看症状因为是某些文件被占用了,目录不能解绑.但是由于系统中很多地方都 ...
- php in_array语法
bool in_array ( mixed $needle , array $haystack [, bool $strict ] ) 返回值为直或假 var_dump(in_array( ...
- Spring装配Bean---使用xml配置
声明Bean Spring配置文件的根元素是<beans>. 在<beans>元素内,你可以放所有的Spring配置信息,包括<bean>元素的声明. 除了Bean ...