`

MyEclipse中的JUnit的使用

阅读更多
MyEclipse是一个很强大的IDE,因为安装文件太大,不能放到附件中,请需要的同志联系我
MyEclipse自带了单元测试的包,无需安装,使用的是JUnit 4包
待测试的类:
package testedClass;

public class Calculator {

	public int add(int a,int b){
		return a+b;
	}
	public int multiply(int a,int b){
		return a*b;
	}
	
	public int substraction(int a,int b){ //新增的
		return a - b;
	}
}


测试用例:
这里用的是JUnit4,它的类存放在org.junit包中,与早期的JUnit 3.8不同,使用JUnit 4开发的测试用例类不必再扩展junit.framework.TestCase,不必扩展任何类,但是必须使用注解,为了一个测试用例能够执行,必须至少有一个@Test注解
package test;
import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import testedClass.Calculator;


public class CalculatorTest {
	
	private int a;
	private int b;
	private Calculator instance ;

	@Before
	public void setUp() throws Exception { //实现初始化测试中的变量
		a = 8;
		b = 9; //初始化测试中的变量
		instance = new Calculator();
	}

	@After
	public void tearDown() throws Exception { //释放测试中所用的变量
		
	}

	@Test
	public void testAdd() { 
		int r = instance.add(a, b);
		int expect = 17;
		assertEquals(r,expect);
	}

	@Test
	public void testMultiply() {

		int r = instance.multiply(a, b);
		int expect = 72;
		assertEquals(r,expect);
	
	}

}

执行测试用例,显示绿条,即类的功能无误,红条即出错
再增加一个减法的测试用例,如下所示:
package test;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import testedClass.Calculator;

public class AnotherCalculatorTest {
	private Calculator instance;

	@Before
	public void setUp() throws Exception {
		instance = new Calculator();
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testSubstraction() {

		int a = 90;
		int b = 78;
		int expect = 12;
		assertEquals(expect, instance.substraction(a, b));
	}

}


将两个测试用例组合成一个测试套件,
package test;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

import junit.framework.Test;
import junit.framework.TestSuite;

@RunWith(Suite.class) //新增的,在JUnit 4中为了让测试套件来运行测试用例,需要使用@RunWith和@Suite注解编写一个空类,
//@RunWith将告诉将使用org.junit.runners.Suite,测试用例类的名称都定义在@Suite.SuiteClasses注解中
@Suite.SuiteClasses({CalculatorTest.class,AnotherCalculatorTest.class}) //新增的

public class AllTests {

	public static Test suite() {
		TestSuite suite = new TestSuite("Test for test");
		//$JUnit-BEGIN$
		
		//$JUnit-END$
		return suite;
	}

}

测试结果见下图:



  • 大小: 32.3 KB
分享到:
评论

相关推荐

    教会你如何使用junit,以及myeclipse+junit 使用

    教会你如何使用junit,以及myeclipse+junit 使用

    Myeclipse中的JUnit测试详细

    Myeclipse中的JUnit测试详细

    myEclipse下使用Junit

    最快速的学习myEclipse下使用Junit的方法; 详细介绍了junit使用方法

    Junit在myEclipse使用方法

    很不错的细节解说,有图文解释哦。大家在运用myEclipse是经常用的Junit测试,此时需要的就是测试模板,这里给出了细节模板哦。

    myeclipse测试工具junit4

    junit是java中书写unit test的framework,目前一些流行的unit test工具大都都是在junit上扩展而来的。

    在Eclipse中使用JUnit4进行单元测试.doc

    在Eclipse中使用JUnit4进行单元测试 在Eclipse中使用JUnit4进行单元测试

    JUnit如何使用介绍

    3, 把你释放文件中的如:org.junit4_4.3.1文件拷贝Eclipse的\plugins目录下,如果你的电脑上的环境安装了MyEclipse6.0插件了,这个文件也就存在\plugins目录下了,也就是说你要写JUnit的测试类环境已经好了,可以...

    junit测试安装包

    这是junit测试的安装包 ,在开发过程中使用myeclipse的junit难免不方便 而且版本过于低 不适合 这个是目前最新版本的

    Myeclipse Junit测试

    NULL 博文链接:https://robinsoncrusoe.iteye.com/blog/810200

    junit-4.7和4.8和4.9和4.10和4.11和4.11文档和源码

    junit junit-4.7 junit-4.8 junit-4.9 junit-4.10 junit-4.11 junit文档 junitAPI junit源码 junit.jar 包含了junit 5个版本的jar包。各个版面自己感觉差距挺大的,坑到我了,我最后用的4.10,4.9也行,4.11听说不太...

    junit 单元测试

    myeclipse 的 junit 单元测试文档

    使用MyEclipse创建Spring Boot项目demo

    使用 Spring Boot 的优势 使用 Spring Boot 开发项目,会给我们带来非常美妙的开发体验,可以从以下几个方面展开来说明 ...Spring Boot Test 中包含了我们需要使用的各种测试场景,满足我们日常项目的测试需求。

    book(junit ant myeclipse 运动英语).rar

    book(junit ant myeclipse 运动英语).rar.........

    junit-4.12.jar

    该jar包是junit版本为4.12,如果在Spring中使用了注解配置ApplicationContext.xml的话,就必须使用junit4.12版本或者更高

    junit-4.9.jar

    junit-4.9.jar是在MyEclipse集成开发环境中作单元测试所需要引入的jar包,该资源为4.9版本。

    struts-junit spring-mock spring-test junit的javadoc.jar文档

    struts-junit spring-mock spring-test junit等的javadoc.jar格式的API文档,直接导入Eclipse/MyEclipse/Netbeans等IDE即可实现快速API查询。 包含以下文件: File name -------------------------------------- ...

    Ant+JUnit+EMMA集成实例

    本实例主要展示了在myEclipse或Eclipse中如何集成Ant、Junit和EMMA技术,有三个文件夹,在导入myEclipse或Eclipse中时,要Link Source,把三个都选上。详细地讲解了ant的构建文件build.xml

    JAVA程序开发大全---上半部分

    10.5 使用MyEclipse实现Spring框架中的AOP编程 173 10.5.1 向项目添加AOP支持包 173 10.5.2 创建Bean类 173 10.5.3 创建前置通知类 174 10.5.4 装配拦截qi和配置Bean类 175 10.5.5 编写测试代码 176 10.6 Spring整合...

    基于MyEclipse搭建maven+springmvc整合图文教程(含源码0

    本教程是基于MyEclipse环境下基于maven搭建的springmvc步骤(图文教程),文章结尾含有源码下载地址: 新建maven project File->New->Project 选择Maven Project 选择项目路径 Use default Workspace location默认...

Global site tag (gtag.js) - Google Analytics