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管理项目 ...
随机推荐
- C#版--简单工厂模式
为什么要用设计模式? 1.设计模式是前人根据经验总结出来的,使用设计模式,就相当于是站在了前人的肩膀上. 2.设计模式使程序易读.熟悉设计模式的人应该能够很容易读懂运用设计模式编写的程序. 3.设计模 ...
- 字符串的长度超过了为 maxJsonLength 属性设置的值
当出现类似标题的错误时,可以按照如下方法解决: 1. 检查是否传递的JSON字符串长度过长 2.增加JSON串的长度设置,设置如下: <system.web.extensions> ...
- Redis缓存数据之简单逻辑
并发不高的情况: 读: 读redis->没有,读DataBase->把DB数据写回redis,有的话直接从redis中取: 写: 写DataBase->成功,再写redis: 并发高 ...
- Angularjs^1.2.9 搜索关键字高亮显示
需求分析: 根据关键字搜索网页内容,并且高亮显示内容中的关键字细节分析: 1.每次执行搜索操作,需清空上一次结果 2.需区分html标签和正常文本内容,否则为关键字添加样式以后会出现标签内容被显示的情 ...
- 【G】开源的分布式部署解决方案(二) - 好项目是从烂项目基础上重构出来的
分析目前项目结构 眼前出现这么一坨坨的文件夹,相信很多人已经看不下去了.是的,首先就是要把它给做掉. 按照这个项目文件夹的命名意图,大概可以划分如下: 1.Business:业务代码 2.Data:数 ...
- json-lib之复杂数据类型的转换
在json字符串转java bean时,一般的对象,可以直接转,如:一个学生类,属性有姓名.年龄等 public class Student{ private String sname; privat ...
- ArcGIS API for JavaScript 4.2学习笔记[5] 官方API大章节概述与内容转译
内容如上,截图自ESRI官网,连接:ArcGIS API for JavaScript 4.2 [Get Started] 类似于绪论一样的东西,抽取了最需要关注的几个例子.如:加载Map和View, ...
- spring 定时任务 taskScheduler详解
spring 3.0版本后,自带了一个定时任务工具,而且使用简单方便,不用配置文件,可以动态改变执行状态.也可以使用cron表达式设置定时任务. 被执行的类要实现Runnable接口 TaskSche ...
- Jasmine基础语法
简介 Jasmine 是JavaScript的测试框架,它不依赖其他框架,也不依赖DOM,更重要的是它语法简单.以下实例都是基于Jasmine 2.5.2的,并且来自官网:https://jasmin ...
- nodejs笔记1 ----关于express不是本地命令
1,npm install -g express安装express框架 2,npm install -g express-generator安装命令工具 3,express --help 安装完成