提醒:测试单元中的每一个方法必须独立测试,每个测试方法之间不能有依赖。
实体类:
package Gg;
/**
* Created by ASUS on 2016/12/5.
*/
public class FF {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void toString(String name){
this.name=name;
System.out.println("name");
}
}
创建好实体类FF后,在idea中导入junit插件,在FF.class中Alt+Inset,这样会出现一个test包下的
package test.Gg;
import Gg.FF;
import org.junit.Assert;
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
/**
* FF Tester.
*
* @author <Authors name>
* @since
* @version 1.0
*/
public class FFTest {
@Before
public void before() throws Exception {
}
@After
public void after() throws Exception {
}
/**
*
* Method: getName()
*
*/
@Test
public void testGetName() throws Exception {
//TODO: Test goes here...
}
/**
*
* Method: setName(String name)
*
*/
@Test
public void testSetName() throws Exception {
//TODO: Test goes here...
}
/**
*
* Method: toString(String name)
*
*/
@Test
public void testToString() throws Exception {
//TODO: Test goes here...
FF ff= new FF();
ff.setName("sff");
Assert.assertEquals("sff123", ff.getName());
}
}
然后可以点击运行一个函数、或一整个test类
点击左边的按钮,就只运行testToString()一个方法,结果为
代码覆盖率的html页面为:
测试结果html页面:
也可以运行整个test类